python局域网alive ip侦听
作者:vpoet
mails:vpoet_sir@163.com
注:写着玩,欢迎copy
1 # -*- coding: cp936 -*- 2 # coding = utf-8 3 4 import os 5 import re 6 import thread 7 import time 8 import socket 9 import sys 10 11 def Ping_Ip(Curr_Ip): 12 13 global Count_Thread,lock,ThreadsNum 14 15 #print "*****************Chile_Thread-Begin****************"+"\n" 16 17 ping_cmd = "ping" +" "+Curr_Ip 18 19 20 Ping_Answer = os.popen(ping_cmd).readlines() 21 patt = r'TTL=([0-9]{2})' 22 TTL_Str=re.findall(patt,Ping_Answer[2]) 23 24 if len(TTL_Str) == 0: 25 #print Curr_Ip+"is Not Alive" 26 pass 27 #print "*****************Chile_Thread-Over****************"+"\n" 28 else: 29 HostInfo = socket.gethostbyaddr(Curr_Ip) 30 Mac_address=Get_Mac_Addr(Curr_Ip) 31 #print "Mac_address"+Mac_address 32 print "\n"+"Alive Host-----> "+"HostComputerName:"+HostInfo[0]+" Mac_address:"+Mac_address+"\t"+"Ip:"+Curr_Ip 33 34 #print "*****************Chile_Thread-Over****************"+"\n" 35 36 37 lock.acquire() 38 Count_Thread = Count_Thread+1 39 if Count_Thread ==ThreadsNum: 40 print "*****************NetWork_End***************" 41 lock.release() 42 43 44 45 def Get_Mac_Addr(Curr_Ip): 46 47 Mac_cmd = "nbtstat -A "+Curr_Ip 48 Mac_Info = os.popen(Mac_cmd).readlines() 49 50 Mac_Info_Sum="" 51 52 for index in range(0,len(Mac_Info)): 53 Mac_Info_Sum=Mac_Info_Sum+Mac_Info[index] 54 55 56 patt_mac = r'= (.{2}-.{2}-.{2}-.{2}-.{2}-.{2})' 57 mac_addr= re.findall(patt_mac,Mac_Info_Sum) 58 return mac_addr[0] 59 60 61 62 def GetAliveIp(Net_iP_Init,IpBegin,IpEnd): 63 SplitIp = Net_iP_Init.split(".") 64 Ip1=SplitIp[0] 65 Ip2=SplitIp[1] 66 Ip3=SplitIp[2] 67 68 Prefix_Ip = Ip1+"."+Ip2+"."+Ip3+"." 69 70 71 for Ip_Last in range(IpBegin,IpEnd+1): 72 Curr_Ip=Prefix_Ip+str(Ip_Last) 73 74 thread.start_new_thread(Ping_Ip, (Curr_Ip,)) 75 time.sleep(2) 76 77 78 79 def GetNetGate(): 80 Netgate_cmd = "ipconfig /all" 81 Netgate_info = os.popen(Netgate_cmd).readlines() 82 83 Netgate_info_Str = "" 84 for index in range(0,len(Netgate_info)): 85 Netgate_info_Str=Netgate_info_Str+Netgate_info[index] 86 #print Netgate_info_Str 87 #print type(Netgate_info_Str) 88 89 patt_hn = r'主机名 . . . . . . . . . . . . . : (.+)' 90 Host_Name_Local = re.findall(patt_hn,Netgate_info_Str) 91 92 Rent_Ip_Begin=r'获得租约的时间 . . . . . . . . . : (.+)' 93 Rent_Ip_Begins=re.findall(Rent_Ip_Begin,Netgate_info_Str) 94 95 Rent_Ip_End=r'租约过期的时间 . . . . . . . . . : (.+)' 96 Rent_Ip_Ends=re.findall(Rent_Ip_End,Netgate_info_Str) 97 98 patt_ipv6 = r'本地链接 IPv6 地址. . . . . . . . : ([a-z0-9]{3,4}::[a-z0-9]{3,4}:[a-z0-9]{3,4}:[a-z0-9]{3,4}:[a-z0-9]{3,4}%[0-9]{2})' 99 ipv6 = re.findall(patt_ipv6,Netgate_info_Str)100 101 patt_ipv4 = r'IPv4 地址 . . . . . . . . . . . . : ([0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3})'102 ipv4 = re.findall(patt_ipv4,Netgate_info_Str)103 104 YanMas= r'子网掩码 . . . . . . . . . . . . : ([0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3})'105 YM = re.findall(YanMas,Netgate_info_Str)106 107 Netgates = r'默认网关. . . . . . . . . . . . . : ([0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3})'108 Ng = re.findall(Netgates,Netgate_info_Str)109 110 Patt_dhcp = r'DHCP 已启用 . . . . . . . . . . . : (.{1,2})'111 dhcp_Is=re.findall(Patt_dhcp,Netgate_info_Str)112 113 Patt_dhcp_server = r'DHCP 服务器 . . . . . . . . . . . : ([0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3})'114 dhcp_server = re.findall(Patt_dhcp_server,Netgate_info_Str)115 116 print "本地主机名: "+Host_Name_Local[0]117 print "本机IPv6地址: "+ipv6[0]118 print "本机IPv4地址: "+ipv4[0]119 print "子网掩码: "+YM[0]120 print "默认网关: "+Ng[0]121 print "是否启用DHCP: "+dhcp_Is[0]122 print "DHCP服务器IP: "+dhcp_server[0]123 print "动态租约起始时间: "+Rent_Ip_Begins[0]124 print "动态租约过期时间: "+Rent_Ip_Ends[0]125 126 return Ng[0]127 128 129 if __name__ == "__main__":130 131 Count_Thread = 0132 lock = thread.allocate_lock() 133 print "*****************NetWork_Begin**************"134 135 Net_iP_Init=GetNetGate()136 137 IpBegin = raw_input("请输入侦听起始IP:")138 IpEnd = raw_input("请输入侦听结束IP:")139 140 IntIpBegin = int(IpBegin)141 IntIpEnd = int(IpEnd)142 143 ThreadsNum = IntIpEnd+1-IntIpBegin144 145 GetAliveIp(Net_iP_Init,IntIpBegin,IntIpEnd)146
运行截图: