I am developing a Python script that tests one networking application. As part of the test, it needs to move networking configuration (IP address, routes …) from one interface (physical interface) to another interface (bridge), and after the test is done, restore the system back to its original state. What is the most elegant approach to accomplish this in Python?
Some ideas I have thought about:
- To not unassign the IP address from physical interface during the test, so that routes are not lost. But this would mean that the same IP address would coexist on the bridge during the test. Would this be a problem on some particular Linux Kernels? Although, it seems to work just fine on my system…
- Assign the IP address to bridge and unassign from physical interface. Easy to implement in python, because this requires to do simple
ifconfigcalls and parsing. But if the default route was through the physical interface, then it would disappear at the same time, when I unassigned the IP address from physical interface. -
Parse the
ip route lsoutput and move the routes together with the IP config. This seems the only reasonable approach, but would require quite a lot of coding. -
Maybe there is something more elegant? Like
iptables-save eth0>eth0_conf,iptables-restore eth0_conf? Any other suggestions?
This test tool must be portable, and be able to run on different Linux Kernels.
I would suggest the following approach:
ifconfig eth0 down && ifconfig br0 upAnd to restore:
ifconfig br0 down && ifconfig eth0 upNow for the routes it depends on what kind of routes you have. If you defined static routes with explicit interfaces, your only choice seems to be parsing
ip route lsand translating them to the new interface.You can also toy around with the order of the up & down commands as well as multiple routing tables:
But this can get tricky, so my suggestion would be to stick to the simple solution, even if it includes some more coding.
Here is another example from xend’s
network-bridgescript to achieve this: