Below is the file 'get.py' from this revision. You can also download the file.

#!/usr/bin/env python

# connects to a windows box, produces a list of shares and
# then lets you connect to them

# useful for security scanning for open shares

import sys
import os
import getopt
import pipes
import popen2
import string
from getpass import getpass
from goatpy import smbwrapper

def retrieve_share(share):
	cmd = smbwrapper.build_basic_smbclient(target, workgroup, username=user, password=password) + ' ' + pipes.quote('//%s/%s' % (node, share))
	os.system(cmd)

def print_share_menu():
	print
	print "IP address:", target
	print "Node:", node
	print "Workgroup:", workgroup
	print
	for i, share in enumerate(shares):
		print "%2d." % i, share
	print
	print "Select share to connect to: ",
	share_id = sys.stdin.readline()
	try:
		i = int(share_id)
		retrieve_share(shares[i])
	except: pass

if __name__ == "__main__":
	share = None
	user = None
	password = None

	matched, remain = getopt.getopt(sys.argv[1:], "u:s:")
	for opt, value in matched:
		if opt == '-u': user = value
		elif opt == '-s': share = value
	if len(remain) != 1:
		sys.stderr.write("usage: %s [OPTION..] target\n" % (sys.argv[0]))
		sys.exit(1)
	target = remain[0]

	if user != None: password = getpass('Enter password (warning; will appear in process list): ')
	node, workgroup = smbwrapper.netbios_lookup(target)
	if not node or not workgroup:
		sys.stderr.write("Netbios lookup failed.\n")
		sys.exit(1)

	if not share:
		shares = map(lambda x: x[0], smbwrapper.list_shared_resources(target, node, workgroup, username=user, password=password))
		shares.sort()
		while True:
			print_share_menu()
	else:
		retrieve_share(share)