Handling of Python Network Settings

Asked

Viewed 2,098 times

1

Working in linux environment, Python 2.7, and I am in need of a module/library in Python that allows me to change the network settings (IP, Subnet, Gateway and, if possible, primary and secondary DNS).

I checked some libraries (netaddr, ipaddress, netifaces, IPy), but they only allowed me to read and do checks with the network card data (I was able to extract this information and show on the screen), but none allows me to make changes to this information.

My software should allow the user to have the option to leave the static IP on the machine, and to run the os.system("") to change via terminal, I would need to be logged in as root.

If anyone knows any way to make this kind of change, I’ll be eternally grateful.

  • 1

    I may be mistaken, but I believe that even if you find a library that does this you will still need to be logged in as root, since it will make a change in the system.

  • That’s right. But I wanted to manipulate this information from inside Python, but I guess I’ll have to call the library 'os' anyway...

2 answers

2


I believe that pyroute2 be enough to meet your needs, I’ve played with it once, it’s a very complete library, da para adicionar Routes, create vlans and several others, the script needs to be executed as root yes, since these changes require administrative privileges. on the Pypi has some examples:

https://pypi.python.org/pypi/pyroute2

follows an example:

from pyroute2 import IPRoute
ip = IPRoute()

idx = ip.link_lookup(ifname='enp1s0')[0]

ip.link('set',
        index=idx,
        state='up')


ip.addr('add',
        index=idx,
        address='10.0.0.1',
        broadcast='10.0.0.255',
        prefixlen=24)

ip.close()
  • Cool - I found the Pyroute too - I think it’s the right way. (Then I started writing some Python tips, instead of giving an example with pyroute) :-)

1

I think this one pyroute2 does what you’re asking.

If it doesn’t work, and you can identify shell commands that make the configuration you need, using Python to run these commands can be better than, for example, trying to modify the configuration files directly from Python (it will take less work and if you miss and generate a syntactically wrong file in the system settings it will take a lot more work than if Python simply sends an invalid command)

The tip that’s left is, instead of using the os.system, use the library subprocess do Python - why it allows much more control of the external process, if you want for example, read what the external program prints in the standard output and use these values in Python.

And last but not least - use Python 3 - whatever your Linux distribution, it will allow you to install Python3 in parallel to Python2 - Python2 had its last release in 2010, and no longer evolves - in 3 years it will stop being maintained, and if your code base is big, you’ll have to port before that.

(And, yes, any script that will do something similar needs to run as root. If necessary in the documentation of sudo you can find ways for other users to run only your program with root privileges).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.