Forum Discussion

JRahm's avatar
JRahm
Icon for Admin rankAdmin
Jul 16, 2009

pyControl and IP Sorting

Just an FYI...I'm learning about python right now and how dictionaries themselves cannot be sorted. But lists of dictionaries can be, and this is good, because if you have things like routes that are returned as a dictionary...

{'netmask': '255.255.255.0', 'destination': '192.168.1.0'}

...you can group these into a list of all your routes (I like to append the gateway to this dictionary personally before sorting)

Once you have your list of dictionaries, you can use a sort function to get your table in correct IP order. Here's the process:

Routing table as a list as returned in dictionary format by iControl:

   
 rt_dict = [{'netmask': '255.255.255.0', 'destination': '192.168.1.0'},   
 {'netmask': '255.255.255.0', 'destination': '192.168.2.0'},   
 {'netmask': '255.255.255.0', 'destination': '192.168.10.0'}]   
 

Function to sort a dictionary by IP (key is 'destination'):

   
 def sort_ip_dict(ip_list):   
      from IPy import IP   
      ipl = [ (IP(i['destination']).int(),  i) for i in ip_list]   
      ipl.sort()   
      return [ip[1] for ip in ipl]   
 

If you have a simple list (list of IP's only, not in a dictionary, the function is simplified a little:

   
 def sort_ip_list(ip_list):   
      from IPy import IP   
      ipl = [ (IP(ip).int(),  ip) for ip in ip_list]   
      ipl.sort()   
      return [ip[1] for ip in ipl]   
 

Here's an example carried out in a python shell:

   
 >>> rtl   
 [{'netmask': '255.255.255.0', 'destination': '192.168.10.0', 'gateway': '10.10.20.245'},  
 {'netmask': '255.255.255.0', 'destination': '192.168.20.0', 'gateway': '10.10.20.245'},  
 {'netmask': '255.255.255.0', 'destination': '192.168.0.0', 'gateway': '10.10.20.245'},  
 {'netmask': '0.0.0.0', 'destination': '0.0.0.0', 'gateway': '10.10.20.245'},  
 {'netmask': '255.255.255.0', 'destination': '192.168.1.0', 'gateway': '10.10.20.245'},  
 {'netmask': '255.255.255.0', 'destination': '192.168.2.0', 'gateway': '10.10.20.245'},  
 {'netmask': '255.255.255.0', 'destination': '192.168.3.0', 'gateway': '10.10.20.245'}]   
 >>> sort_ip_dicts(rtl)   
 [{'netmask': '0.0.0.0', 'destination': '0.0.0.0', 'gateway': '10.10.20.245'},  
 {'netmask': '255.255.255.0', 'destination': '192.168.0.0', 'gateway': '10.10.20.245'},  
 {'netmask': '255.255.255.0', 'destination': '192.168.1.0', 'gateway': '10.10.20.245'},  
 {'netmask': '255.255.255.0', 'destination': '192.168.2.0', 'gateway': '10.10.20.245'},  
 {'netmask': '255.255.255.0', 'destination': '192.168.3.0', 'gateway': '10.10.20.245'},  
 {'netmask': '255.255.255.0', 'destination': '192.168.10.0', 'gateway': '10.10.20.245'},  
 {'netmask': '255.255.255.0', 'destination': '192.168.20.0', 'gateway': '10.10.20.245'}]   
 

2 Replies

  • I just released a tech tip on grabbing the routes from the BIG-IP and sorting with this handy python function. Check it out!

     

     

    http://devcentral.f5.com/Default.aspx?tabid=63&articleType=ArticleView&articleId=368 Click here