Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The easiest way to to this in my experience is to use two third party packages:</p> <ul> <li><a href="http://pypi.python.org/pypi/netifaces/" rel="noreferrer">python-netifaces</a>: Portable network interface information</li> <li><a href="http://pypi.python.org/pypi/netaddr" rel="noreferrer">python-netaddr</a>: Pythonic manipulation of IPv4, IPv6, CIDR, EUI and MAC network addresses</li> </ul> <p>So install those modules and then it's as easy as this:</p> <pre><code>import netifaces import netaddr import socket from pprint import pformat ifaces = netifaces.interfaces() # =&gt; ['lo', 'eth0', 'eth1'] myiface = 'eth0' addrs = netifaces.ifaddresses(myiface) # {2: [{'addr': '192.168.1.150', # 'broadcast': '192.168.1.255', # 'netmask': '255.255.255.0'}], # 10: [{'addr': 'fe80::21a:4bff:fe54:a246%eth0', # 'netmask': 'ffff:ffff:ffff:ffff::'}], # 17: [{'addr': '00:1a:4b:54:a2:46', 'broadcast': 'ff:ff:ff:ff:ff:ff'}]} # Get ipv4 stuff ipinfo = addrs[socket.AF_INET][0] address = ipinfo['addr'] netmask = ipinfo['netmask'] # Create ip object and get cidr = netaddr.IPNetwork('%s/%s' % (address, netmask)) # =&gt; IPNetwork('192.168.1.150/24') network = cidr.network # =&gt; IPAddress('192.168.1.0') print 'Network info for %s:' % myiface print '--' print 'address:', address print 'netmask:', netmask print ' cidr:', cidr print 'network:', network </code></pre> <p>And this outputs:</p> <pre><code>Network info for eth0: -- address: 192.168.1.150 netmask: 255.255.255.0 cidr: 192.168.1.150/24 network: 192.168.1.0 </code></pre> <p>This was done using Linux. With OSX the interface names are different but the method is the same.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload