Python writes IP address and decimal IP translation scripts

Source: Internet
Author: User

IP address and decimal IP conversion

#!/usr/bin/env python

#encoding =utf-8


Import re

Import Sys

Import OS


def ten_to_two (Ten_num):

Two_str = ' '


While Ten_num! = 1:

A = ten_num% 2

TWO_STR = Two_str + str (a)

Ten_num = TEN_NUM/2

Else

TWO_STR = Two_str + str (1)

TWO_STR = Two_str[::-1]

If Len (TWO_STR) < 8:

Cover = (8-len (TWO_STR)) * STR (0)

TWO_STR = cover + two_str


Return TWO_STR


def two_to_ten (Two_num):

Ten_int = 0

Indexlist = Range (len (two_num))

For I in Indexlist:

Ten_int + = Int (Two_num[i]) * 2 * * indexlist[-(I+1)]


Return str (ten_int)


def int_to_ipaddr (Intnum):

int_addr = Int (intnum)

Twonum = Ten_to_two (int_addr)


If Len (Twonum) < 32:

Twonum = ' 0 ' * (32-len (twonum)) + Twonum


ipaddr = ' '

For j in range (0, 32, 8):

K = j + 8

If j = = 24:

IPAddr + = Two_to_ten (Twonum[j:k])

Else

IPAddr + = Two_to_ten (Twonum[j:k]) + '. '


return ipaddr


def ipaddr_to_int (IPAddress):

IPList = Ipaddress.split ('. ')

Ipstr = ' '

For I in IPList:

Ipstr + = ten_to_two (int (i))

If Ipstr.startswith (' 0 '):

Ipstr = re.sub (' ^0* ', ' ', IPSTR)


Tennum = Two_to_ten (IPSTR)

Return Tennum




If Len (SYS.ARGV) < 2:

print ' No action specified. '

Sys.exit ()

Elif len (sys.argv) = = 2:

If Sys.argv[1].startswith ('--'):

option = sys.argv[1][2:]

if option = = ' Version ':

print ' Version 1.1 '

elif option = = ' help ':

print ' \

Used to convert the IP address.

\t-i Ipaddress\n\t\tconvert the IP address.

\t\tlike./iptrans.py 192.168.1.1

\t-if Filename\n\t\tconvert The IP address ' s file.

\t\tlike./iptrans.py./ipfile

\t-t int Ipaddress\n\t\tconvert the int IP address.

\t\tlike./iptrans.py 3232235777

\T-TF Filename\n\t\tconvert The IP address ' s file.

\t\tlike./iptrans.py./intipfile

\t--help\n\t\tdisplay this help.

\t--version\n\t\tprints the version number. "

Else

print ' Unknown option. '

Sys.exit ()

Elif len (sys.argv) = = 3:

If Sys.argv[1].startswith ('-'):

option = sys.argv[1][1:]

if option = = ' I ':

If Sys.argv[2].count ('. ') = = 3:

IPAddress = sys.argv[2]

Intkey = Ipaddr_to_int (ipaddress)

print '%s\t%s '% (IPAddress, Intkey)

Else

Print '%s is error. '% sys.argv[2]

Sys.exit ()

elif option = = ' If ':

If Os.path.isfile (sys.argv[2]) = = True:

filename = sys.argv[2]

f = open (filename, ' R ')

For line in F:

IPAddress = Line.replace (' \ n ', ')

Intkey = Ipaddr_to_int (ipaddress)

print '%s\t%s '% (IPAddress, Intkey)

Else

Print ' The%s is not exist. '% sys.argv[2]

Sys.exit ()

elif option = = ' t ':

Intkey = sys.argv[2]

if int (Intkey) < 16777217:

Print '%s is the error, must be great than 16777217 '% sys.argv[2]

Else

IPAddress = Int_to_ipaddr (Intkey)

print '%s\t%s '% (Intkey, IPAddress)

elif option = = ' TF ':

If Os.path.isfile (sys.argv[2]) = = True:

filename = sys.argv[2]

f = open (filename, ' R ')

For line in F:

Intkey = Line.replace (' \ n ', ')

IPAddress = Int_to_ipaddr (Intkey)

print '%s\t%s '% (Intkey, IPAddress)

Else

Print ' The%s is not exist. '% sys.argv[2]

Sys.exit ()

Else

print ' Unknown option. '

Sys.exit ()

Else

print ' Unknown option. '

Sys.exit ()


This article is from the "Operations" blog, please be sure to keep this source http://gongniue.blog.51cto.com/7073997/1825855

Python writes IP address and decimal IP translation scripts

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.