python3实现telnet查看IP地址段端口开放情况
1 #!/usr/bin/python3 2 #-*- coding:utf-8 -*- 3 4 #编写环境 windows 7 x64 + Python3.7.0 5 #python3实现IP地址段打印 6 7 #ip转换方法 8 def ip2num(ip): 9 ip=[int(x) for x in ip.split('.')] 10 return ip[0] <<24 | ip[1]<<16 | ip[2]<<8 |ip[3] 11 def num2ip(num): 12 return '%s.%s.%s.%s' %( (num & 0xff000000) >>24, 13 (num & 0x00ff0000) >>16, 14 (num & 0x0000ff00) >>8, 15 num & 0x000000ff ) 16 17 #把输入的ip范围组合成一个list形式 18 def get_ip(ip): 19 start,end = [ip2num(x) for x in ip.split('-') ] 20 return [ num2ip(num) for num in range(start,end+1) if num & 0xff ] 21 22 23 if __name__ == '__main__': 24 iplist = get_ip('10.10.10.1-10.10.10.10') 25 print(iplist)
1 #!/usr/bin/python3 2 #-*- coding:utf-8 -*- 3 4 #编写环境 windows 7 x64 + Python3.7.0 5 #python3实现telnet查看IP地址段端口开放情况 6 7 import telnetlib 8 import threading 9 import time 10 11 #ip转换方法 12 def ip2num(ip): 13 ip=[int(x) for x in ip.split('.')] 14 return ip[0] <<24 | ip[1]<<16 | ip[2]<<8 |ip[3] 15 def num2ip(num): 16 return '%s.%s.%s.%s' %( (num & 0xff000000) >>24, 17 (num & 0x00ff0000) >>16, 18 (num & 0x0000ff00) >>8, 19 num & 0x000000ff ) 20 21 #把输入的ip范围组合成一个list形式 22 def get_ip(ip): 23 start,end = [ip2num(x) for x in ip.split('-') ] 24 return [ num2ip(num) for num in range(start,end+1) if num & 0xff ] 25 26 27 def scan(ip,port): 28 29 try: 30 #server = telnetlib.Telnet(ip, port, timeout=10) 31 server = telnetlib.Telnet() # 创建一个Telnet对象 32 33 server.open(ip,port) # 利用Telnet对象的open方法来创建TCP连接测试端口是否开放 34 35 #a表示以追加的形式将端口开放的ip列出来 36 file = open('thefile.txt', 'a') 37 file.write(ip+" "+port+"\n") 38 file.close() 39 print('{0} port {1} is open'.format(ip, port)) 40 except Exception as e: 41 print('{0} port {1} is off'.format(ip, port)) 42 print(e) 43 44 45 if __name__ == '__main__': 46 iplist = get_ip('10.10.10.1-10.10.10.10') 47 print(iplist) 48 49 starttime = time.time() 50 portlist=['22','80'] 51 threads = [] 52 for port in portlist: 53 for ip in iplist: 54 t = threading.Thread(target=scan,args=(ip,port)) 55 t.start() 56 threads.append(t) 57 58 for t in threads: 59 t.join() 60 endtime = time.time() 61 print('一共运行了{0}秒'.format((endtime-starttime)))
1 #!/usr/bin/python3 2 #-*- coding:utf-8 -*- 3 4 #编写环境 windows 7 x64 + Python3.7.0 5 #python3实现telnet查看IP地址段端口开放情况 6 7 import telnetlib 8 import gevent 9 import time 10 11 #ip转换方法 12 def ip2num(ip): 13 ip=[int(x) for x in ip.split('.')] 14 return ip[0] <<24 | ip[1]<<16 | ip[2]<<8 |ip[3] 15 def num2ip(num): 16 return '%s.%s.%s.%s' %( (num & 0xff000000) >>24, 17 (num & 0x00ff0000) >>16, 18 (num & 0x0000ff00) >>8, 19 num & 0x000000ff ) 20 21 #把输入的ip范围组合成一个list形式 22 def get_ip(ip): 23 start,end = [ip2num(x) for x in ip.split('-') ] 24 return [ num2ip(num) for num in range(start,end+1) if num & 0xff ] 25 26 27 def scan(ip,port): 28 29 try: 30 #server = telnetlib.Telnet(ip, port, timeout=10) 31 server = telnetlib.Telnet() # 创建一个Telnet对象 32 33 server.open(ip,port) # 利用Telnet对象的open方法来创建TCP连接测试端口是否开放 34 35 #a表示以追加的形式将端口开放的ip列出来 36 file = open('thefile.txt', 'a') 37 file.write(ip+" "+port+"\n") 38 file.close() 39 print('{0} port {1} is open'.format(ip, port)) 40 except Exception as e: 41 print('{0} port {1} is off'.format(ip, port)) 42 print(e) 43 44 45 if __name__ == '__main__': 46 iplist = get_ip('10.10.10.1-10.10.10.10') 47 print(iplist) 48 49 portlist=['22','80'] 50 starttime = time.time() 51 52 #单线程模式 53 ''' 54 for port in portlist: 55 for ip in iplist: 56 print(scan(ip,port)) 57 ''' 58 59 #协程模式,多并发 60 #下面这句的意思就是循环portlist列表里面的port,循环iplist列表里面的ip,全部加到协程(gevent)里面,一起并发启动执行 61 threads = [gevent.spawn(scan, ip,port) for ip in iplist for port in portlist] 62 gevent.joinall(threads) 63 64 endtime = time.time() 65 print('一共运行了{0}秒'.format((endtime-starttime)))
python批量Telnet远程主机查看端口开放情况
https://blog.51cto.com/mapengfei/1926039?cid=695986
python实现指定ip端口扫描