Advertisement
python_notes

Grab Default Gateway on Linux

Aug 15th, 2014
506
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.76 KB | None | 0 0
  1. #  Grab Default Gateway on Linux
  2.  
  3.  
  4. # Reference  ----->  http://serverfault.com/questions/31170/how-to-find-the-gateway-ip-address-in-linux
  5.  
  6.  
  7.  
  8. # Example below using subprocess
  9.  
  10. #!/usr/bin/python
  11.  
  12. import subprocess
  13.  
  14. #pipe = subprocess.Popen(ip route list | awk ' /^default/ {print $3}', stdout=subprocess.PIPE)
  15. #output = pipe.stdout
  16.  
  17. subprocess.call("ip route list | awk ' /^default/ {print $3}'", shell=True)
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25. # Using the iproute package:
  26.  
  27.  
  28.  
  29. # get the default route
  30.  
  31. Raw Commnad:      ip route list | awk ' /^default/ {print $3}'
  32.  
  33. Usage Example:      os.system("ip route list | awk ' /^default/ {print $3}'")
  34.  
  35.  
  36.  
  37.  
  38.  
  39. # get the default route but limit on eth0 (output may be empty)
  40.  
  41. ip route list dev eth0 | awk ' /^default/ {print $3}'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement