python编写IP地址与十进制IP转换脚本
IP地址與十進制IP轉換
#!/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 '''\
This program is 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 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()
轉載于:https://blog.51cto.com/gongniue/1825855
總結
以上是生活随笔為你收集整理的python编写IP地址与十进制IP转换脚本的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Bitbucket Pipelines在
- 下一篇: directive多指令之间的异步调用