#!/usr/bin/python

from twisted.internet import reactor
import smb, nmb, sys

BROADCAST = '192.168.0.255'

class ListShares(smb.SMB):
    def sessionEstablished(self):
        print 'session established'
        d = self.listShares()
        d.addCallback(gotShares)
        d.addErrback(disaster)

def gotShares(shares):
    print shares
    reactor.stop()

def disaster(failure):
    print "FAILURE"
    print failure
    reactor.stop()

def lookup(remoteName):
    d = nmb.lookup(remoteName, BROADCAST, broadcast=True)
    d.addCallback(listShares, remoteName)
    d.addErrback(disaster)

def listShares(remoteIP, remoteName):
    f = smb.SMBFactory(remoteName)
    f.protocol = ListShares
    reactor.connectTCP(remoteIP[0].ip, 139, f)

def main(remoteName):
    lookup(remoteName)
    reactor.run()

if __name__ == '__main__':
    main(sys.argv[1])

