本文實(shí)例講述了Python掃描IP段查看指定端口是否開放的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
#!/usr/local/bin/python #-*- coding: UTF-8 -*- #################################################################### ################################################## #BLOG:http://hi.baidu.com/alalmn # Python 掃描IP段 指定端口是否開放 ################################################## import socket import threading,time socket.setdefaulttimeout( 10 ) #設(shè)置了全局默認(rèn)超時時間 #查看IP端口是否開放 class socket_port(threading.Thread): def __init__( self ,cond, name): super (socket_port, self ).__init__() self .cond = cond self .cond. set () #將標(biāo)識位設(shè)為Ture self .HOST = name def run( self ): #time.sleep(1) #確保先運(yùn)行Seeker中的方法 try : PORT = 21 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(( self .HOST,PORT)) print "" print self .HOST,u ":" ,PORT,u "端口開放" #self.cond.wait()#堵塞線程,直到Event對象內(nèi)部標(biāo)識位被設(shè)為True或超時(如果提供了參數(shù)timeout)。 self .cond. set () #將標(biāo)識位設(shè)為Ture return 1 except : print "." , #print self.HOST,u":",PORT,u"端口未開放" #self.cond.wait()#堵塞線程,直到Event對象內(nèi)部標(biāo)識位被設(shè)為True或超時(如果提供了參數(shù)timeout)。 self .cond. set () #將標(biāo)識位設(shè)為Ture return 0 ## #socket_port("192.168.2.1") #if socket_port("192.168.2.100"): # print "開放" #else: # print "未開放" def ip2num(ip): ip = [ int (x) for x in ip.split( '.' )] return ip[ 0 ]<< 24 | ip[ 1 ]<< 16 | ip[ 2 ]<< 8 | ip[ 3 ] def num2ip(num): #time.sleep(0.05) #50ms #time.sleep(0.1) #s # data='%s.%s.%s.%s' % ( (num & 0xff000000) >> 24, # (num & 0x00ff0000) >> 16, # (num & 0x0000ff00) >> 8, # num & 0x000000ff ) # #socket_port(data) #查看IP端口是否開放 if num> = IPend: print u "IP導(dǎo)入數(shù)組完成" return '%s.%s.%s.%s' % ( (num & 0xff000000 ) >> 24 , (num & 0x00ff0000 ) >> 16 , (num & 0x0000ff00 ) >> 8 , num & 0x000000ff ) def gen_ip(ip1,ip2): #返回數(shù)組 # ip # global IPend # start, IPend = [ip2num(x) for x in ip.split('-')] global IPend IPend = ip2 return [num2ip(num) for num in range (ip1,ip2 + 1 ) if num & 0xff ] import ini if __name__ = = '__main__' : ini.ini_get() #讀取INI list_ip = gen_ip(ip2num(ini.IP1),ip2num(ini.IP2)) I1 = 0 #得到list的第一個元素 print u "開始掃描IP" ip = 0 while I1 < len (list_ip): #print list_ip[I1] time.sleep( 0.3 ) #確保先運(yùn)行Seeker中的方法 cond = threading.Event() hider = socket_port(cond,list_ip[I1]) hider.start() if ip> = 255 : ini.ini_write(list_ip[I1],ini.IP2) #修改INI print ip ip = 0 ip = ip + 1 I1 = I1 + 1 #一層 |
ini.py:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
#!/usr/local/bin/python #-*- coding: UTF-8 -*- ################################################## #qq:316118740 #BLOG:http://hi.baidu.com/alalmn # Python 操作ini文件 # 剛學(xué)寫的不好請大家見諒 ################################################## IP1 = "" #掃描IP IP2 = "" #當(dāng)前已經(jīng)掃到的IP INITXT = "IP.ini" #INI文件名字 import ConfigParser def ini_get(): #讀取INI try : global IP1 global IP2 global INITXT config = ConfigParser.ConfigParser() config.readfp( open (INITXT)) IP1 = config.get( "ipdata" , "ip1" ) IP2 = config.get( "ipdata" , "ip2" ) except : print "讀取INI錯誤" ini_add(" "," ") #寫入INI def ini_add(ip1,ip2): #寫入INI try : global INITXT config = ConfigParser.ConfigParser() config.add_section( "ipdata" ) # 設(shè)置section段及對應(yīng)的值 config. set ( "ipdata" , "ip1" ,ip1) config. set ( "ipdata" , "ip2" ,ip2) config.write( open (INITXT, "w" )) # 寫入文件 except : print "寫入INI錯誤" def ini_write(ip1,ip2): #修改INI try : global INITXT config = ConfigParser.ConfigParser() config.read(INITXT) if not config.has_section( "ipdata" ): #看是否存在該Section,不存在則創(chuàng)建 temp = config.add_section("") config. set ( "ipdata" , "ip1" ,ip1) config. set ( "ipdata" , "ip2" ,ip2) config.write( open (INITXT, "r+" )) except : print "修改INI錯誤" ini_add(" "," ") #寫入INI #if __name__=='__main__': ## ini_get() #讀取INI ## print IP1 ## print IP2 # ## ini_add("222222222","3333333333333") #寫入INI ## ini_get() #讀取INI ## print IP1 ## print IP2 # # ini_write("999999999","0000000000") #修改INI # ini_get() #讀取INI # print IP1 # print IP2 |
運(yùn)行:
希望本文所述對大家的Python程序設(shè)計(jì)有所幫助。