- from scapy.all import *
- from threading import Thread, active_count
- from sys import argv, exit
- import time
- class PortScan:
- def __init__(self,host, ps, pe ,ttl):
- self.host = host
- self.ps = ps
- self.pe = pe
- self.ttl = ttl
- def scan(self):
- i = self.ps
- while i < self.pe + 1:
- if active_count() < 200:
- Thread(target=self.notsyn, args=(i,)).start()
- i = i + 1
- def notsyn(self,i):
- client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
- client.settimeout(self.ttl)
- id = client.connect_ex((self.host,i))
- if id == 0:
- print i,
- def syn_scan(self):
- i = self.ps
- while i < self.pe + 1:
- if active_count() < 200:
- Thread(target=self.syn, args=(i,)).start()
- i = i + 1
- def syn(self,i):
- ans = sr1(IP(dst=self.host)/TCP(dport=i),verbose=False,timeout=self.ttl,retry=0)
- if ans:
- print ans.sport,
- def port_scan():
- PortScan(host = argv[2],
- ps = int(argv[3]),
- pe = int(argv[4]),
- ttl = int(argv[5])).scan()
- def syn_port_scan():
- PortScan(host = argv[2],
- ps = int(argv[3]),
- pe = int(argv[4]),
- ttl = int(argv[5])).syn_scan()
- if __name__ == '__main__':
- if len(argv) < 6:
- exit(0)
- now = time.time()
- if argv[1] == '-s':
- port_scan()
- elif argv[1] == '-S':
- syn_port_scan()
- else:
- exit(0)
- while True:
- if active_count() <= 1:
- break
- now = time.time() - now
- print '\\n'+str(now),'seconds.'
- #该片段来自于http://www.codesnippet.cn/detail/120520149519.html
来源: http://www.codesnippet.cn/detail/120520149519.html