Python LAN Alive IP Listener
Author: Vpoet
Date: Summer
Note: Write a play, welcome copy
#-*-coding:cp936-*-# coding = utf-8import OS import reimport threadimport timeimport socketimport sysdef Ping_Ip (Curr_ IP): Global count_thread,lock,threadsnum #print "*****************chile_thread-begin****************" + "\ n" ping_c md = "ping" + "" +curr_ip Ping_answer = Os.popen (ping_cmd). ReadLines () Patt = R ' ttl= ([0-9]{2}) ' Ttl_str=re.fin Dall (patt,ping_answer[2]) if Len (ttl_str) = = 0: #print curr_ip+ "is not Alive" pass #print "* * * chile_thread-over**************** "+" \ n "else:hostinfo = socket.gethostbyaddr (curr_ip) mac_ ADDRESS=GET_MAC_ADDR (CURR_IP) #print "mac_address" +mac_address print "\ n" + "Alive Host----->" + "Hostcomp Utername: "+hostinfo[0]+" mac_address: "+mac_address+" \ T "+" IP: "+curr_ip #print" *****************chile_thread-over* "+" \ n "lock.acquire () Count_thread = count_thread+1 if Count_thread ==threadsnum:print" * * Network_end*************** "Lock.release () def get_mac_addr (curr_ip): Mac_cmd =" Nbtstat-a "+curr_ip mac_info = Os.popen (Mac_cmd). ReadLines () mac_info_sum= "" for Index in range (0,len (mac_info)): mac_info_sum=mac_info_sum+ Mac_info[index] Patt_mac = R ' = (. { 2}-. {2}-. {2}-. {2}-. {2}-. {2}) ' Mac_addr= Re.findall (patt_mac,mac_info_sum) return mac_addr[0]def Getaliveip (net_ip_init,ipbegin,ipend): SplitIp = N Et_ip_init.split (".") Ip1=splitip[0] ip2=splitip[1] ip3=splitip[2] prefix_ip = ip1+ "." +ip2+ "." +ip3+ "." For Ip_last in range (ipbegin,ipend+1): Curr_ip=prefix_ip+str (ip_last) thread.start_new_thread (ping_i P, (CURR_IP,)) Time.sleep (2) def getnetgate (): Netgate_cmd = "Ipconfig/all" Netgate_info = Os.pope N (netgate_cmd). ReadLines () Netgate_info_str = "" For index in range (0,len (netgate_info)): Netgate_info_str=ne Tgate_info_str+netgate_info[index] #print Netgate_infO_str #print Type (netgate_info_str) Patt_hn = R ' Host name ............. : (. +) ' host_name_local = Re.findall (patt_hn,netgate_info_str) Rent_ip_begin=r ' time to obtain a lease ... . : (. +) ' Rent_ip_begins=re.findall (rent_ip_begin,netgate_info_str) rent_ip_end=r ' lease expiry time .... . : (. +) ' Rent_ip_ends=re.findall (rent_ip_end,netgate_info_str) Patt_ipv6 = R ' Local link IPv6 address ..... : ([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}) ' IPv6 = Re.findall (Patt_ipv6, NETGATE_INFO_STR) Patt_ipv4 = R ' IPv4 address ............:([0-9]{1,3}.[ 0-9]{1,3}. [0-9] {1,3}. [0-9] {1,3}) ' IPv4 = Re.findall (patt_ipv4,netgate_info_str) yanmas= R ' Subnet mask ............:([0-9]{1,3}.[ 0-9]{1,3}. [0-9] {1,3}. [0-9] {1,3}) ' YM = Re.findall (yanmas,netgate_info_str) netgates = R ' Default gateway ............. : ([0-9]{1,3}. [0-9] {1,3}. [0-9] {1,3}. [0-9] {1,3}) ' Ng = Re.findall (netgates,netgate_info_str) patt_dhcp = R ' DHCP enabled ........... : (. {()}) ' Dhcp_is=re.findall (patt_dhcp,netgate_info_str) patt_dhcp_server = R ' DHCP server ........... : ([0-9]{1,3}. [0-9] {1,3}. [0-9] {1,3}. [0-9] {1,3}) ' Dhcp_server = Re.findall (patt_dhcp_server,netgate_info_str) print "local hostname:" +host_name_local[0 "print" Native IPV6 address: "+ Ipv6[0] print "Native IPV4 address:" +ipv4[0 "print" Subnet mask: "+ym[0" print "Default gateway:" +ng[0 "print" Whether DHCP is enabled: "+dhcp_is[0] Print "DHCP server Ip: +dhcp_server[0] print" Dynamic lease start Time: "+rent_ip_begins[0" print "Dynamic lease expiration:" +rent_ip_ends[0] return ng[0]if __name__ = = "__main__": count_thread = 0 lock = thread.allocate_lock () print "************* network_begin************** "Net_ip_init=getnetgate () Ipbegin = Raw_input (" Enter listen start IP: ") Ipend = Raw_input (" Please Lose Into the listening End IP: ") intipbegin = Int (ipbegin) intipend = Int (ipend) Threadsnum = Intipend+1-intipbegin Getaliveip (net_i P_init,intipbegin,intipend)
Perform:
Python LAN Alive IP Listener