<s> from __future__ import print_function <EOL> import array , fcntl , struct <EOL> from pycopia import socket <EOL> class IfConfig ( object ) : <EOL> """<STR_LIT>""" <EOL> SIOCGIFNAME = <NUM_LIT> <EOL> SIOCGIFCONF = <NUM_LIT> <EOL> SIOCGIFFLAGS = <NUM_LIT> <EOL> SIOCGIFADDR = <NUM_LIT> <EOL> SIOCGIFBRDADDR = <NUM_LIT> <EOL> SIOCGIFNETMASK = <NUM_LIT> <EOL> SIOCGIFCOUNT = <NUM_LIT> <EOL> IFF_UP = <NUM_LIT> <EOL> IFF_BROADCAST = <NUM_LIT> <EOL> IFF_DEBUG = <NUM_LIT> <EOL> IFF_LOOPBACK = <NUM_LIT> <EOL> IFF_POINTOPOINT = <NUM_LIT> <EOL> IFF_NOTRAILERS = <NUM_LIT> <EOL> IFF_RUNNING = <NUM_LIT> <EOL> IFF_NOARP = <NUM_LIT> <EOL> IFF_PROMISC = <NUM_LIT> <EOL> IFF_ALLMULTI = <NUM_LIT> <EOL> IFF_MASTER = <NUM_LIT> <EOL> IFF_SLAVE = <NUM_LIT> <EOL> IFF_MULTICAST = <NUM_LIT> <EOL> IFF_PORTSEL = <NUM_LIT> <EOL> IFF_AUTOMEDIA = <NUM_LIT> <EOL> def __init__ ( self ) : <EOL> self . sockfd = socket . socket ( socket . AF_INET , socket . SOCK_DGRAM ) <EOL> def __del__ ( self ) : <EOL> self . close ( ) <EOL> def close ( self ) : <EOL> if self . sockfd is not None : <EOL> sock = self . sockfd <EOL> self . sockfd = None <EOL> sock . close ( ) <EOL> def _fcntl ( self , func , args ) : <EOL> return fcntl . ioctl ( self . sockfd . fileno ( ) , func , args ) <EOL> def _getaddr ( self , ifname , func ) : <EOL> ifreq = ( ifname + '<STR_LIT>' * <NUM_LIT:32> ) [ : <NUM_LIT:32> ] <EOL> try : <EOL> result = self . _fcntl ( func , ifreq ) <EOL> except IOError : <EOL> return None <EOL> return socket . inet_ntoa ( result [ <NUM_LIT:20> : <NUM_LIT> ] ) <EOL> def getInterfaceList ( self ) : <EOL> """<STR_LIT>""" <EOL> buffer = array . array ( '<STR_LIT:c>' , '<STR_LIT>' * <NUM_LIT> ) <EOL> ifconf = struct . pack ( "<STR_LIT>" , buffer . buffer_info ( ) [ <NUM_LIT:1> ] , buffer . buffer_info ( ) [ <NUM_LIT:0> ] ) <EOL> result = self . _fcntl ( self . SIOCGIFCONF , ifconf ) <EOL> iflist = [ ] <EOL> size , ptr = struct . unpack ( "<STR_LIT>" , result ) <EOL> for idx in range ( <NUM_LIT:0> , size , <NUM_LIT:32> ) : <EOL> ifconf = buffer . tostring ( ) [ idx : idx + <NUM_LIT:32> ] <EOL> name , dummy = struct . unpack ( "<STR_LIT>" , ifconf ) <EOL> name , dummy = name . split ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> iflist . append ( name ) <EOL> return iflist <EOL> def getFlags ( self , ifname ) : <EOL> """<STR_LIT>""" <EOL> ifreq = ( ifname + '<STR_LIT>' * <NUM_LIT:32> ) [ : <NUM_LIT:32> ] <EOL> try : <EOL> result = self . _fcntl ( self . SIOCGIFFLAGS , ifreq ) <EOL> except IOError : <EOL> return <NUM_LIT:0> <EOL> flags , = struct . unpack ( '<STR_LIT:H>' , result [ <NUM_LIT:16> : <NUM_LIT> ] ) <EOL> return flags <EOL> def getAddr ( self , ifname ) : <EOL> """<STR_LIT>""" <EOL> return self . _getaddr ( ifname , self . SIOCGIFADDR ) <EOL> def getMask ( self , ifname ) : <EOL> """<STR_LIT>""" <EOL> return self . _getaddr ( ifname , self . SIOCGIFNETMASK ) <EOL> def getBroadcast ( self , ifname ) : <EOL> """<STR_LIT>""" <EOL> return self . _getaddr ( ifname , self . SIOCGIFBRDADDR ) <EOL> def isUp ( self , ifname ) : <EOL> """<STR_LIT>""" <EOL> return ( self . getFlags ( ifname ) & self . IFF_UP ) != <NUM_LIT:0> <EOL> def get_myaddress ( iface = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> return IfConfig ( ) . getAddr ( iface ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> ifc = IfConfig ( ) <EOL> print ( "<STR_LIT>" , ifc . getAddr ( "<STR_LIT>" ) ) <EOL> ifaces = ifc . getInterfaceList ( ) <EOL> for name in ifaces : <EOL> print ( "<STR_LIT>" % ( name , ( '<STR_LIT>' , '<STR_LIT>' ) [ ifc . isUp ( name ) ] , <EOL> ifc . getAddr ( name ) , ifc . getMask ( name ) , ifc . getBroadcast ( name ) ) ) </s>
