-1

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.

Ghanima
  • 15,958
  • 17
  • 65
  • 125
lgeoghegan
  • 7
  • 1
  • 1
  • 2

2 Answers2

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,882
  • 3
  • 17
  • 22