12

In my Python script I need to retrieve both the IP address of the machine the script is running on and its network address and its network bytes.

As for the IP address, I found the solution in the archive:

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("www.google.com",80))
myAddress = (s.getsockname()[0])
s.close()

But how should I go about finding network address and network bytes? I need to put this information into a filter for tcpdump in the format $NetworkAddress/$NetworkBytes, if that helps at all.

Example:

128.1.2.0/20

I can actually find it under inet when I run ip addr. Any easy way to get this information in Python?

6
  • NetworkBytes or network mask? Commented Jul 12, 2012 at 13:58
  • +1 for showing what you have so far Commented Jul 12, 2012 at 13:59
  • What do you mean by "network address" and "network bytes"? Commented Jul 12, 2012 at 14:01
  • OK, I added more details to my original post. Commented Jul 12, 2012 at 14:23
  • Your example seems to be an IP-address/prefix-length. I still have no idea what you mean by "NetworkBytes" because if that's supposed to mean the same thing as "prefix length" then NetworkBytes is a VERY odd name to call that... Anyway, if you do mean IP address/prefix length, then, do you mean the IP address and prefix length assigned to one of the local interfaces on the machine the script is running on? Commented Jul 12, 2012 at 19:57

3 Answers 3

18
+50

For Linux try

iface = "eth0"
socket.inet_ntoa(fcntl.ioctl(socket.socket(socket.AF_INET, socket.SOCK_DGRAM), 
                             35099, struct.pack('256s', iface))[20:24])

or http://github.com/rlisagor/pynetlinux

(as suggested here: Retrieving network mask in Python)

For Linux, Windows and MacOS consider http://alastairs-place.net/projects/netifaces/

Update:

If you need cidr (like '128.1.2.0/20'), you can use any of the related libs: http://pypi.python.org/pypi?%3Aaction=search&term=cidr&submit=search

For example netaddr:

>> from netaddr import IPNetwork
>> print str(IPNetwork('1.2.3.4/255.255.255.0').cidr)
1.2.3.0/24
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. Actually I need something in the form given in my question, that is "address/PrefixLength" instead of the actual network mask. Is there any fast way to do this?
Which OS are you interested in?
Thank you. I can't find package netstat. Could you please point it to me? Thanks again!
3

You can retrieve any ip-related info with pyroute2 module:

from pyroute2 import IPDB
ip = IPDB()
print(ip.interfaces['em1'].ipaddr)
ip.release()

Or as a variant:

from pyroute2 import IPRoute
ip = IPRoute()
info = [{'iface': x['index'],
         'addr': x.get_attr('IFA_ADDRESS'),
         'mask': x['prefixlen']} for x in ip.get_addr()]
ip.close()

Comments

0

Heyo! Late response, but you could use ipadress to get the network address (assuming you have the network mask and an ip address of a device on the same subnet)

import ipaddress
device_ip = "192.168.1.20"
network_subnet = "255.255.255.0"
network_address = ipaddress.Ipv4Network(f"{device_ip}/{network_subnet}", strict=False)
print(network_address)

Which in my case resulted in this:

192.168.1.0/24

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.