#!/usr/bin/python
#This is WNM with kick. Instead of searching the network the "legit" way with nmblookup 
#and smbclient (they are still used), this script relies on nmap to find all the 
#computers in a specified IP range. The program takes one argument which is the IP
#range as nmap takes. EX. for 192.168.0.1 to 192.168.3.255 would be 'bwnm 192.168.0-3.1-255'

import commands, string,  getopt, sys, re, os

tmp, args = getopt.getopt(sys.argv[1:], '')
broadcast = ''

def findbroadcast():
    path = '/sbin/ifconfig eth0'
    status, output = commands.getstatusoutput(path)
    if status:
	print 'Unable to find broadcast with ifconfig.'
    broadcast = re.findall("Bcast:\d*\.\d*\.\d*\.\d*", output)[0][6:]
    return broadcast


#using nmap scan the range of given ips and find the ones with netbios port open
def getips():
    ip_range = args[0]
    ip_list = []
    
    status, output = commands.getstatusoutput('nmap -p 139 ' + ip_range)
    if status:
	raise 'There was a problem with running nmap.'
    
    nmapoutput = string.split(output, '\n')
    
    for tmp in nmapoutput:
	ipaddr = re.findall("\d*\.\d*\.\d*\.\d*", tmp)
	if ipaddr:
	    ip_list.append(ipaddr[0])

    return ip_list

def getnames(ips):
    comp_names = []
    
    for ip in ips:
	status, output = commands.getstatusoutput('nmblookup -B %s -A %s' % (broadcast, ip))
	if status:
	    print 'Error looking up: %s' % (ip)
    	nmblookupoutput = string.split(output, '\n');
	
	for tmp in nmblookupoutput:
	    if re.search('<00> -         ', tmp):
		point = string.find(tmp, '<00>')
		comp_names.append(string.strip(tmp[1:point]))
    		
    return comp_names

def getshares(compname):
    
    sharelist = []
    status, output = commands.getstatusoutput('smbclient -n "noone" -N -L "' + compname + '"')
    if status:
	print 'Error looking up shares on: %s' % (compname)
    smbclientoutput = string.split(output, '\n')
	
    for tmp in smbclientoutput:
	if string.find(tmp, 'PRINTER$') == -1:
	    if re.search('Disk', tmp):
		point = string.find(tmp, 'Disk')
		sharelist.append(string.strip(tmp[1:point]))
	    if re.search('Server               Comment', tmp):
		continue
	
    return sharelist

def mountshares(comp_name):
    comp_name = string.lower(comp_name)
    comp_name = string.strip(comp_name)
    sharecount = 0
    
    shares = getshares(comp_name)
    
    if not len(shares):
	return sharecount
    
    command = 'mkdir "./' + comp_name + '"'
    os.system(command)
    
    for ashare in shares:
	
	ashare = string.lower(ashare)
	ashare = string.strip(ashare)
	
	os.system('mkdir "./' + comp_name + '/' + ashare + '"')
	command = 'smbmount "//' + comp_name + '/' + ashare + '" "./' + comp_name + '/' + ashare + '" -o guest, netbiosname=bob, username=bob, uid=bob, gid=bob'
	output = commands.getstatusoutput(command)[1]
	if output:
	    print 'Error: could not mount //' + comp_name + '/' + ashare
	    os.system('rmdir  "./' + comp_name + '/' + ashare + '"')
	else:
	    print 'Mounting //' + comp_name + '/' + ashare
	    sharecount = sharecount + 1

    return sharecount
#--------------------------------------------------------------
broadcast = findbroadcast()


if args == []:
    print 'Usage: bwnm <ip range>'
else:    

    computer_names = getnames(getips())
    mountedcount = 0

    for name in computer_names:
    	mountedcount = mountedcount + mountshares(name)
    
    print "Mounted " + str(mountedcount) + " shares."
