RSS category feeds

RSS site feeds

More

Get List of IP Routes PDF Print E-mail
Quick method to extract the IP routing table on a PC into a list of lists using a single list comprehension. Each route table entry is stored in a list where: [0] = Network Destination [1] = Netmask [2] = Gateway [3] = Interface [4] = Metric The active default route will always be the first list in the results [0], so the default next hop for unknown destinations will be [0][3].
 
from os import popen
from string import split, join
from re import match
 
rtr_table = [elem.strip().split() for elem in popen("route print").read().split("Metric\n")[1].split("\n") if match("^[0-9]", elem.strip())]
 
print "Active default route:", rtr_table[0]
 
print "Active default next hop:", rtr_table[0][3]
The above comprehension relies on the "Metric" header value. I later realized this may not scale to non-U.S. PCs so here's a different comprehension that should work for all regions.
 
[elem.strip().split() for elem in popen("route print").read().split("\n") if match("^[0-9]", elem.strip()) and is_IPv4_Addr(elem.strip().split()[0])]
Important: The above comprehensions don't take into account that "route print" may be invalid on some PCs...though I've never seen this in practice. So you may want to capture the popen output first and check to make sure it's not blank so the comprehension doesn't throw an exception.
 
    return_vals = popen("route print").read()
    if return_vals:
        # U.S. Version
        #rtr_table = [elem.strip().split() for elem in return_vals.split("Metric\n")[1].split("\n") if match("^[0-9]", elem.strip())]
 
        # Universal Version ?
        return [elem.strip().split() for elem in return_vals.split("\n") if match("^[0-9]", elem.strip()) and is_IPv4_Addr(elem.strip().split()[0])]
Last Updated ( Sunday, 09 April 2006 )
 
< Prev   Next >

Feedback

Advertisement

Comments

  • I have run your code on my own machine, but I just got the last paragraph of the... More...
  • Hello Gabriel, It seem that the linked site moved to github.com/.../weboutlook (... More...
  • There is no script to download, could you provide the right link More...
  • Interesting but doesn't really help me. I do not see what the 'xxxxx' in the Ses... More...
  • You can see more python editor comparison: sparkledge.com/.../ (http://sparkledg... More...

Login Form






Lost Password?

My prefered Python IDE

My prefered Python editor is Pyscripter from MMExperts. It is not only an editor. Pyscripter is a full Python IDE including (remote) debugging, a class browser, and all other nice helpers which a full featured IDE needs.

Do you have a script for me ?

Do you have an interesting Python script which does some really cool thing on Windows ? Please post them to this site. It`s very simple - simply copy&paste it to this form. No login is requiered.

Hint: For syntax highlighting and correct Python intendation place your code between html tags <pre> and </pre>.

My prefered web framework

My prefered web framework for developing web applications is Django. Django calls itself The web framework for perfectionists with deadlines. It is a really fast, scalable and (thanks Python) the sexiest web framework of the world.