IP子网计算

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

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值