0

I'm creating a network testing tool (educational project) at work which is to be used by non technical staff. I have created a simple GUI with buttons for preconfigured IP addresses, of which I can manipulate the static IP address and the subnet mask of the Pi.

The only problem is that I can't find a way to change the default gateway in the same command (or at all using Python).

I'm currently using:

def setIP_XXX():

    os.system('sudo ifconfig eth0 down')

    os.system('sudo ifconfig eth0 192.xxx.xxx.xx/24')

    os.system('sudo ifconfig eth0 up')

Any help in this area would be great as it's starting to become a very stressful experience.

Brick
  • 1,367
  • 2
  • 12
  • 19
lgeoghegan
  • 9
  • 1
  • 1
  • 2
  • Do these commands work if you issue them directly on the command line (i.e. without python)? If not, they are not going to work from python since you are just making a system call here anyway. If yes, then you probably have a path issue where the system call from python does not get the same environment as you get on your command line. (That might be an issue with python itself or with the user / environment that started python via your GUI.) – Brick Mar 17 '18 at 14:49

3 Answers3

0

If your network doesn't provide you gateway for some reason, then your route table will be empty. Use below command to check it after connecting to a network

$ route -n

If, it's empty then do this

$ route add default gw {gateway-ip} {interface-name}

You can also set netmask along with IP

Example:

$ sudo ifconfig {interface-name} 192.168.1.XX netmask 255.255.255.0
Arpit Agarwal
  • 581
  • 6
  • 10
0

Try out the pyroute2 library that has the functions you are looking for.

https://github.com/svinota/pyroute2

MatsK
  • 2,478
  • 3
  • 12
  • 20
0

Firstly, you do not specify what OS you are using, I assume Raspbian.

Secondly, your script may or may not modify Debian networking settings, but unless you have configured Debian networking (or you are running a VERY old OS) it is unlikely to be.

I suggest you take 2 steps backward, and first figure out HOW to configure networking, BEFORE trying to script it.

See How to set up networking/WiFi

Milliways
  • 54,718
  • 26
  • 92
  • 182