On Linux, how can I find the default gateway for a local ip address/interface using python?
I saw the question \"How to get internal IP, external IP and default gate
You can get it like this (Tested with python 2.7 and Mac OS X Capitain but should work on GNU/Linux too): import subprocess
def system_call(command):
p = subprocess.Popen([command], stdout=subprocess.PIPE, shell=True)
return p.stdout.read()
def get_gateway_address():
return system_call("route -n get default | grep 'gateway' | awk '{print $2}'")
print get_gateway_address()