Python real-combat IP address sorting problem (02)

Source: Internet
Author: User

1. Background notes

The IP address of the virtual machine is obtained from the Nova list of OpenStack, but none of these IPs are sorted and grouped, in order to further enhance readability, to perform a sort operation on the IP address, under the shell, the sort command can be performed with the following actions:

[email protected] ~]# Cat Ip.txt | Sort-t "."-k1,1n-k2,2n-k3,3n-k4,4n10.1.104.7510.1.104.8710.1.104.14910.1.104.15110.1.105.110.1.105.7

The above way, can realize the sort of IP address, the following Python way to achieve the sort of IP address, to carry the way the file, the IP address read into a list, through the list of the sort () method, the IP address of the sort


2. Script Content

#!/usr/bin/env python#-*- coding:utf8 -*-"Author:happyfrom: happy Cloud Lab welcomes the Exchange" ' import  Sysimport os.pathdef ipsort (filepath= "Ip.txt"):         "          defines a function for sorting IP addresses,  The default file path is the Ip.txt file in the current directory, returning the sorted IP address list          '         try:                 ip = []                 f =  File (filepath, ' R ')                  fcontext = f.readlines ()                  for ips in fcontext:                          ips = ips.strip ()                           ip.append (IPs)                  ip.sort (LAMBDA X,Y: CMP (". Join" ( [ i.rjust (3,  ' 0 ')  for i in x.split ('. ')]  ),  ". Join ( [ i.rjust (3,  ' 0 ')  for i in y.split ('. ')]))         #排序                  f.close ()         except  exception,msg:                 print  "error:%s"  %  (msg)            &NBsp;    sys.exit (1)         return ipdef  Main ():         '          Check that the script carries parameters, must carry a parameter--file name, and check whether the file name exists, call the sort function ipsort ()          "'          try:                 if len (SYS.ARGV)  != 2:                          print  "usage: %s %s"  %  (sys.argv[0],sys.argv[1])                   else:                         if  os.path.exists (SYS.ARGV[1]):                                 FILENAME=SYS.ARGV[1]                          else:                                  print  "%s is not exists!"  %  (sys.argv[1])                                   Sys.exit (1)                 ip  = ipsort (filename)             #执行排序函数调用                  for ips in ip:                         print ips         except Exception,msg:                 print  "\033[31merror: %s\033[0m"  %  ( msg) if __name__ ==  "__main__":         main ()


Description : Run as, script + parameters in the form of the test results as follows:

[Email protected] ~]#/usr/local/sbin/ipsort.py/root/ip.txt 10.1.104.7510.1.104.8710.1.104.22210.1.104.25010.1.105.110.1.105.7


*******************************************

Learning is a long and painful process that needs perseverance!

*******************************************

This article is from the "Happy Lab" blog, so be sure to keep this source http://happylab.blog.51cto.com/1730296/1720845

Python real-combat IP address sorting problem (02)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.