Dialog based sample tool for configuring network interfaces with pythondialog and debinterface
Jul 1, 2015
So, what do you do when you want to give out a Linux based virtual machine image to someone and want a cool config tool
which allows non-seasoned console Jockeys (regular users) to easily configure a static ip address for their network interface?
We know, use a docker container. And we will. That is the best option, period. But a docker container probably also needs a
static ip change by the user (or not, but we have not much docker experience yet. This is still a todo).
You could ask anyone in attached documentation to modify a couple of lines in etc/network/interfaces, but hey, we can do much
better than that. First, we ship console only Ubuntu Servers (no gui), and sometimes it is a pain to let someone without enough
Linux skills to hack around with vim and the interfaces file. Well. Let’s create a dialog based config tool!
Dialog is a great tool for this, it is based on the ncurses library, dating all the way back to 1993 ;). Great tools are
ageless. Originally it is a simple command line program, making it very easy to include into shell scripts. You can display full screen
message boxes, selection dialogs, forms, tree views, tail log displays, and much more. However, if you are like
us, you also don’t really want to write anything longer than 5-10 lines in bash. Python is the way to go!
Fortunately, there is a wrapper above the dialog program, called pythondialog. It is available for both python3 and
python2.7. Code can be much more pythonic this way, you don’t need to call subprocess.Popen all the time for your dialog code (the lib will call it for you :)). Check out the documentation.
Okay, what do we want to achieve?
The user should type simply sudo run-config.py, and let the dialogs drive the configuration process.
Display a list of available network interfaces to chose from
Select either dhcp or static address configuration for the selected interface.
If the user selects static configuration, display a form where ipv4 address, netmask and gateway settings could be entered.
Handle input errors
Write or update the selected network interface configuration data into /etc/network/interfaces file
Restart the interface to apply changes
As you can see, this is a real life example. We like real life examples. Hope you do as well. One thing which could be boring is to
figure out ways to correctly parse (and write) the /etc/network/interfaces file for a specific adapter, without overwriting the rest of the file. This could be quite boring when your main task is about to drive dialogs. Correctly parsing the file is just an obstacle, yet it must be
Take a good look around when you have situations like this. The Python community is big. We could not image this is was not solved by somebody
earlier. Turns out we were right, here it is, it is called debinterface. Great, now we can simply depend on this library,
instead of trying to write clueless print statements to our interfaces file (Note: the import statements, as documented in debinterface’s
README.md, did not work for us. Check our scripts for the correct import).
In order to deploy this script to your Linux server, you should:
sudo apt-get install python-pip
sudo apt-get install dialog
sudo pip install python2-pythondialog
clone debinterface repository, and copy the contents to ~/.local/lib/python2.7/site-packages (Note your local
site package folder may be different. Check the output of python -m site --user-site to see where is yours.
Now, here is the script. Don’t forget to mark it as executable with chmod +x pydialog-interfaces.py and run with sudo. Also available on