python區域網路alive ip偵聽

來源:互聯網
上載者:User

標籤:python   ping   

python區域網路alive ip偵聽


作者:vpoet

日期:夏季

注:寫著玩,歡迎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_cmd = "ping" +" "+Curr_Ip        Ping_Answer = os.popen(ping_cmd).readlines()    patt = r'TTL=([0-9]{2})'    TTL_Str=re.findall(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----->   "+"HostComputerName:"+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 = Net_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_Ip, (Curr_Ip,))           time.sleep(2)        def GetNetGate():    Netgate_cmd = "ipconfig /all"    Netgate_info = os.popen(Netgate_cmd).readlines()    Netgate_info_Str = ""    for index in range(0,len(Netgate_info)):        Netgate_info_Str=Netgate_info_Str+Netgate_info[index]        #print Netgate_info_Str        #print type(Netgate_info_Str)    patt_hn = r'主機名稱  . . . . . . . . . . . . . : (.+)'    Host_Name_Local = re.findall(patt_hn,Netgate_info_Str)    Rent_Ip_Begin=r'獲得租約的時間  . . . . . . . . . : (.+)'    Rent_Ip_Begins=re.findall(Rent_Ip_Begin,Netgate_info_Str)    Rent_Ip_End=r'租約到期的時間  . . . . . . . . . : (.+)'    Rent_Ip_Ends=re.findall(Rent_Ip_End,Netgate_info_Str)    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})'    ipv6 = re.findall(patt_ipv6,Netgate_info_Str)    patt_ipv4 = r'IPv4 地址 . . . . . . . . . . . . : ([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'子網路遮罩  . . . . . . . . . . . . : ([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'預設閘道. . . . . . . . . . . . . : ([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 已啟用 . . . . . . . . . . . : (.{1,2})'    dhcp_Is=re.findall(Patt_dhcp,Netgate_info_Str)    Patt_dhcp_server = r'DHCP 伺服器 . . . . . . . . . . . : ([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 "本地主機名稱:  "+Host_Name_Local[0]    print "本機IPv6地址:  "+ipv6[0]    print "本機IPv4地址:  "+ipv4[0]    print "子網路遮罩:  "+YM[0]    print "預設閘道:  "+Ng[0]    print "是否啟用DHCP:  "+dhcp_Is[0]    print "DHCP伺服器IP:  "+dhcp_server[0]    print "動態租約起始時間:  "+Rent_Ip_Begins[0]    print "動態租約到期時間:  "+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("請輸入偵聽起始IP:")    IpEnd = raw_input("請輸入偵聽結束IP:")    IntIpBegin = int(IpBegin)    IntIpEnd = int(IpEnd)    ThreadsNum = IntIpEnd+1-IntIpBegin    GetAliveIp(Net_iP_Init,IntIpBegin,IntIpEnd)    

運行:


python區域網路alive ip偵聽

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.