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

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


def probe(ip):
    try:
        c = SMBConnection('*SMBSERVER', ip, sess_port=445, timeout=8)
        c.login('', '')
    except Exception as e:
        return '%s\tERR\t%s' % (ip, type(e).__name__)
    try:
        out = '%s\t%s\t%s\t%s\t%s' % (ip, c.getServerName(), c.getServerDomain(), c.getServerDNSDomainName(), c.getServerDNSHostName())
    except Exception as e:
        out = '%s\tERR2\t%s' % (ip, type(e).__name__)
    try:
        c.logoff()
    except Exception:
        pass
    return out


with ThreadPoolExecutor(max_workers=16) as ex:
    for r in ex.map(probe, ips):
        f.write(r + '\n')
        f.flush()
f.close()
print('done', len(ips), flush=True)
