python3 achieve telnet port open to view IP addresses situations

1, python3 implement IP addresses Print

. 1  # ! / Usr / bin / to python3 
2  # - * - Coding: UTF-. 8 - * - 
. 3  
. 4  # authoring environment. 7 x64-+ Python3.7.0 Windows 
. 5  # to python3 IP address segment to achieve the print 
. 6  
. 7  # IP conversion method 
. 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 is      return  ' ...% S% S% S% S ' % ((NUM & 0xFF000000) >> 24,
 13 is                              (NUM & 0x00ff0000) >> 16 ,
 14                              (NUM & 0x0000ff00) >>. 8 ,
 15                              NUM & 0x000000ff )
 16  
. 17  # the ip range input into a list in the form of combination 
18 is  DEF get_ip (ip):
 . 19      Start, End = [ip2num (X) for X in ip.split ( ' - ' )]
 20 is      return [num2ip (NUM) for NUM in Range (Start, End +. 1) IF NUM & 0xFF ]
 21 is      
22 is      
23 is if __name__ == '__main__':
24     iplist = get_ip('10.10.10.1-10.10.10.10')
25     print(iplist)

2, python3 achieve telnet port open to view IP addresses situations

1  # ! / Usr / bin / python3 
2  # - * - Coding: UTF-8 - * - 
3  
4  # authoring environment 7 x64 + Python3.7.0 Windows 
5  # python3 achieve telnet port open to see the IP address of the segment 
6  
7  Import telnetlib
 . 8  Import GEVENT
 . 9  
10  # IP conversion method 
. 11  DEF ip2num (IP):
 12 is      IP = [int (X) for X in ip.split ( ' . ' )]
 13 is      return IP [0] << 24 | IP [. 1] 16 << | IP [2]. 8 << | IP [. 3 ]
 14  DEFnum2ip (NUM):
 15      return  ' .% S% S% S% S.. ' % ((NUM & 0xFF000000) >> 24 ,
 16                              (NUM & 0x00ff0000) >> 16 ,
 . 17                              (NUM & 0x0000ff00). 8 >> ,
 18 is                              NUM & 0x000000ff )
 . 19  
20 is  # the combination of inputs into a list ip range form 
21 is  DEF get_ip (ip):
 22 is      Start, End = [ip2num (X) for X in ip.split ( ' - ')]
 23      return [num2ip (whether) for whetherin Range (Start, End +. 1) IF NUM & 0xFF ]
 24      
25  
26 is  DEF Scan (ip):
 27      # calling system telnetlib, designated port designated ip of telnet, if successful, 
28      the try :
 29          Port = 22 is
 30          Server telnetlib.Telnet = (ip, port, timeout = 10 )
 31 is  
32          # to later add a newline ip, which is used to wrap txt display 
33 is          new_ip + ip = ' \ n- ' 
34 is  
35          # for appending the form of the opening port the listed ip 
36          File Open = ( ' thefile.txt ' , ' a')
37         file.write(new_ip)
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      # the single thread mode 
50      # for IP in IPLIST: 
51 is      #      Print Scan (IP) 
52 is  
53 is  
54 is      # coroutine mode, multiple concurrent 
55      # following phrase means that inside the circular list IP, all added coroutine (GEVENT) which, together with the concurrent execution start 
56 is      Threads = [gevent.spawn (Scan, IP) for IP in IPLIST]
 57 is      gevent.joinall (Threads)

3, reference

https://blog.51cto.com/mapengfei/1926039?cid=695986

Guess you like

Origin www.cnblogs.com/wmiot/p/11565394.html