import sys, time
from concurrent.futures import ThreadPoolExecutor
from impacket.smbconnection import SMBConnection

ips = [l.split('\t')[0].strip() for l in open('/root/recon/scan1.tsv') if l.split('\t')[1].strip() == '445']
ips = sorted(set(ips))
out = open('/root/recon/loot/smb_null.tsv', 'w')


def probe(ip):
    res = []
    for user, pwd, tag in (('', '', 'null'), ('guest', '', 'guest')):
        try:
            c = SMBConnection('*SMBSERVER', ip, sess_port=445, timeout=8)
            c.login(user, pwd)
        except Exception as e:
            res.append('%s\t%s\tERR\t%s' % (ip, tag, type(e).__name__))
            continue
        try:
            osname = '%s %s' % (c.getServerOS(), c.getServerName())
        except Exception:
            osname = ''
        shares = []
        try:
            for s in c.listShares():
                shares.append(s['shi1_netname'][:-1])
        except Exception as e:
            shares.append('SHARE_ERR:' + type(e).__name__)
        res.append('%s\t%s\tOK\t%s\t%s' % (ip, tag, osname.replace('\t', ' '), ','.join(shares)))
        try:
            c.logoff()
        except Exception:
            pass
        break
    return res


done = 0
with ThreadPoolExecutor(max_workers=12) as ex:
    for r in ex.map(probe, ips):
        done += 1
        for line in r:
            out.write(line + '\n')
        if done % 25 == 0:
            out.flush()
            print('done', done, flush=True)
out.close()
print('finished', done, flush=True)
