Below is the file 'goatpy/smbwrapper.py' from this revision. You can also download the file.

#!/usr/bin/python

import sys
import os
import pipes
import re
import scan

from goatpy.utility import run_command

# a netbios name can include:
# ! @ # $ % ^ & ( ) - _ ' { } . ~
# REF: http://support.microsoft.com/default.aspx?scid=kb;EN-US;188997
netbios_name_re = '[\w_\-\!\@\#\$\%\^\&\(\)\'\{\}\.\~]+'
share_name_re = '[ \w_\-\!\@\#\$\%\^\&\(\)\'\{\}\.\~]+'
# matches a line from an smbclient DIR
# matches filename, attrs, size
listing_re = '^  (.*) +([A-Z]+) +(\d+)  \w\w\w \w\w\w +[0-9][0-9]? [0-9][0-9]:[0-9][0-9]:[0-9][0-9] [0-9][0-9][0-9][0-9]$'

def netbios_lookup(host, timeout=5):
	node = None
	workgroup = None
	results = run_command('/usr/bin/nmblookup -A %s' % (pipes.quote(host)), timeout)
	lines = results['fromchild'].split('\n')
	for line in lines:
		if workgroup and node: break
		if not workgroup:
			m = re.match('^\t(' + netbios_name_re + ') .*<GROUP>.*<ACTIVE>', line)
			if m:
				workgroup = m.groups()[0]
				continue
		if not node:
			m = re.match('^\t(' + netbios_name_re + ') .*<ACTIVE>', line)
			if m: node = m.groups()[0]
	return (node, workgroup)

def build_basic_smbclient(host, workgroup=None, username=None, password=None):
	options = []
	if not username or not password: options.append('-N')
	else: options.append('-U ' + pipes.quote(username + '%' + password))
	if workgroup: options.append('-W ' + pipes.quote(workgroup))
	if host: options.append('-I ' + pipes.quote(host))
	return "/usr/bin/smbclient " + ' '.join(options)

def list_shared_resources(host, node=None, workgroup=None, username=None, password=None, timeout=5):
	smbclient = build_basic_smbclient(host, workgroup, username, password)
	smbclient += ' -L ' + pipes.quote(node)
	results = run_command(smbclient, timeout)
	lines = results['fromchild'].split('\n')
	shares = []
	for line in lines:
		m = re.match(r'\t(' + share_name_re + ') +(Disk) +(\w?)', line)
		if not m: continue
		name, type, descr = m.groups()[0].strip(), m.groups()[1].strip(), m.groups()[2]
		shares.append((name, type, descr))
	return shares

if __name__ == '__main__':
	print "Please do not press this button again."
	sys.exit(1)