diff --git a/calculate/lib/utils/ip.py b/calculate/lib/utils/ip.py index 669c490..8ac17e6 100644 --- a/calculate/lib/utils/ip.py +++ b/calculate/lib/utils/ip.py @@ -41,6 +41,9 @@ SIOCGIFADDR = 0x8915 SIOCGIFNETMASK = 0x891B SIOCGIFHWADDR = 0x8927 +# Resources allocated +IFF_RUNNING = 0x40 + # ip digit from 0|1-255|254 (template) IP_DIG = "[%s-9]|(?:1[0-9]|[1-9])[0-9]|2[0-4][0-9]|25[0-%s]" # ip net 0-32 @@ -195,6 +198,17 @@ def getIp(iface): ip = struct.unpack('16sH2x4s8x', res)[2] return socket.inet_ntoa(ip) +def getPlugged(iface): + sockfd = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + ifreq = struct.pack('16sH14s', iface, socket.AF_INET, '\x00'*14) + try: + res = fcntl.ioctl(sockfd, SIOCGIFFLAGS, ifreq) + except IOError: + return False + finally: + sockfd.close() + return bool(struct.unpack('16sH2x4s8x', res)[1] & IFF_RUNNING) + def getMask(iface): """ Get mask for interface