Determine if the network port has links-python__python

Source: Internet
Author: User
Part of the Code
Import OS import time import psutil import sys import atexit import datetime import re import array import socket import s Truct Import fcntl def check_linked (ETH): "" "" Check HW (eth0) NET physics conection "" "Buff = Array.array (' i ', [0 x0000000a, 0x00000000]) addr, length = Buff.buffer_info () arg = Struct.pack ("Pi", addr, length) data = (ETH + ' *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 Soc K.close () return bool (Buff.tolist () [1]) #function to net State def getnetstate (): Dir_net_info = psutil.net_i F_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_info
        Rmation.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)

Code copied, the format may not be correct

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.