判斷網口是否有連結-python__python

來源:互聯網
上載者:User
部分代碼
import osimport timeimport psutilimport sysimport atexitimport datetimeimport reimport arrayimport socketimport structimport fcntldef check_linked(eth):    """check hw(eth0) net physics conection"""    buff = array.array('i', [0x0000000a, 0x00000000])    addr, length = buff.buffer_info()    arg = struct.pack("Pi", addr, length)    data = (eth + '\0'*16)[:16] + arg    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)    SIOCETHTOOL = 0x8946    try:        fcntl.ioctl(sock.fileno(), SIOCETHTOOL, data)    except IOError, e:        print("check %s lined failed , and error is %s" %(eth,str(e)))        sock.close()        return False    sock.close()    return bool(buff.tolist()[1])#function of net statedef GetNetState():    dir_net_info = psutil.net_if_addrs()    list_net_key = list(dir_net_info.keys())    #dir_net_stats= psutil.net_if_stats()    list_net_key.reverse()    net_information = psutil.net_io_counters(pernic=True)    net_info=""    for str_i in list_net_key:if str_i == 'lo':continue        list_get_value=dir_net_info.get(str_i)        list_get_value2=list(list_get_value[0:1])        #list_get_net = dir_net_stats.get(str_i)        list_get_net_info = net_information.get(str_i)        #if list_get_net_info !=None:        #    str_get_net_info = str(list_get_net_info[0])        #    str_get_net_info2 = str(list_get_net_info[1])        str_re=re.sub(".*address='","",str(list_get_value2))        str_re2=re.sub("'.*","",str_re)if (re.search(":",str_re2))!=None:str_re2=""        if check_linked(str_i) == False:            str_3="stoped"        else:            str_3="running"        net_info += str_i + " " + str_re2 + " " +str_3+ "|"        #net_info +="-"    return (net_info)

代碼複製出來,格式可能不對

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.