Julien Greard
Julien Greard

Reputation: 989

Change IP settings using Python on Linux

I need to provide a Python program allowing the user to change dynamically the network parameters of his device (IP, mask, DNS & Gateway). For example, changing his IP from 192.168.1.10/24 to 192.168.1.15/24. I don't want to run my program as root - but I can give to my user some specifics rights (for instance add the user to a group which can modify the /usr/network/interfaces file).

What I found is the pynetlinux library but it only provides IP and mask modifications and needs to be run as root (or maybe I missused it).

I could probably do it by parsing the interfaces file and running some bash/shell commands but I don't want to re-invent the wheel. I'm pretty sure some people already had my issue and did something great about it.

I'm running on Ubuntu 12.04. I would need my programm to work also on Ubuntu 14.04, CentOS and RedHat

Upvotes: 2

Views: 3939

Answers (1)

Michael Jaros
Michael Jaros

Reputation: 4681

You should try to create a complete list of actions your program will have to execute. I found that in a similar setting, in response to user interaction we had to:

  • modify config files (interfaces, host, resolv.conf, ...)
  • echo to sysfs to change some settings
  • start/stop certain network services
  • switch firewall configuration (we did that via init script too)

We ended up creating a Bash script with a well-defined interface that a certain non-privileged user could call with sudo from any other program.

However our dialog-based frontend was written in Bash already so we did not spend much time in looking for existing libraries which surely is worth the effort in your case.

Upvotes: 2

Related Questions