将 IP MASK(如 192.168.0.0 255.255.0.0) 格式的的IP子网转换为 IP/掩码位(如192.168.0.0/16) 格式。
使用python处理,执行 python ipmask2.py
ipmaks2.py
# -^- coding: utf-8 -*-
import os
def main():
fileRead = open("demo.txt")
fileWrite = open("demo1.txt",'w')
content = fileRead.readline()
nexthop = "192.168.1.1"
while ('' != content):
ip = ""
netmask = ""
s = content.split()
ip = s[0]
netmask = s[1]
result = ""
for num in netmask.split('.'):
temp = str(bin(int(num)))[2:]
result = result + temp
mask = len("".join(str(result).split('0')[0:1]))
string = 'ip route ' + s[0] + '/' + str(mask) +' ' + nexthop + '\n'
fileWrite.write(string)
content = fileRead.readline()
fileRead.close()
fileWrite.close()
if __name__ == "__main__":
main()
demo.txt
1.48.0.0 255.252.0.0
1.56.0.0 255.248.0.0
1.68.0.0 255.252.0.0
1.80.0.0 255.240.0.0
1.116.0.0 255.252.0.0
1.128.0.0 255.128.0.0
14.0.0.0 255.255.240.0
14.1.0.0 255.255.252.0
14.16.0.0 255.240.0.0
14.96.0.0 255.224.0.0
14.130.0.0 255.254.0.0
14.134.0.0 255.254.0.0