#!/usr/bin/env python
from multiprocessing import *
import sqlite3
import nmap
import time
def scanner (s_arg):
nm = nmap.PortScanner ()
nm.scan (hosts=s_arg, arguments="-n -sS -T4 -p 80,81,1080,1081,3128,8080,8081")
for host in nm.all_hosts ():
for proto in nm[host].all_protocols ():
for port in nm[host][proto].keys ():
if nm[host][proto][port]['state'] == 'open':
queue.put ([host, port])
def updater (db_cursor):
while True:
u_arg = queue.get ()
if u_arg:
db_cursor.execute ('INSERT INTO open VALUES (?,?,?)', (u_arg[0], u_arg[1], int(time.time())))
#added a commit here
db_cursor.commit()
else:
break
sqlite3.enable_callback_tracebacks (True)
conn = sqlite3.connect ('proxy.db')
#db = conn.cursor () #gone to process creation
networks = []
for row in db.execute ('SELECT network FROM cidr WHERE country in (\'CN\')'):
networks.append (row[0])
queue = Queue ()
#new cursor for each process
updater_p = Process (target = updater, args=(conn.cursor ()))
updater_p.daemon = True
updater_p.start ()
pool = Pool (5)
pool.map (scanner, networks)
pool.close ()
pool.join ()
queue.put ([])
updater_p.join ()
conn.commit ()
conn.close ()
IyEvdXNyL2Jpbi9lbnYgcHl0aG9uCiAKZnJvbSBtdWx0aXByb2Nlc3NpbmcgaW1wb3J0ICoKIAppbXBvcnQgc3FsaXRlMwppbXBvcnQgbm1hcAppbXBvcnQgdGltZQogCmRlZiBzY2FubmVyIChzX2FyZyk6CiAgICBubSA9IG5tYXAuUG9ydFNjYW5uZXIgKCkKICAgIG5tLnNjYW4gKGhvc3RzPXNfYXJnLCBhcmd1bWVudHM9Ii1uIC1zUyAtVDQgLXAgODAsODEsMTA4MCwxMDgxLDMxMjgsODA4MCw4MDgxIikKICAgIGZvciBob3N0IGluIG5tLmFsbF9ob3N0cyAoKToKICAgICAgICBmb3IgcHJvdG8gaW4gbm1baG9zdF0uYWxsX3Byb3RvY29scyAoKToKICAgICAgICAgICAgZm9yIHBvcnQgaW4gbm1baG9zdF1bcHJvdG9dLmtleXMgKCk6CiAgICAgICAgICAgICAgICBpZiBubVtob3N0X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