python判断子网掩码的合法性和子网掩码的网络号的合法性

python判断子网掩码的合法性和子网掩码的网络号的合法性

#! /usr/bin/env python3
import sys
import os
import re

def check_network_address(address):
    if not address:
        return False
    #re of ip
    #pattern = "^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$"
    #re of ip/mask
    pattern = "^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)(\/([0-9]$|[0-2][0-9]|3[0-2]))$"
    if not re.match(pattern, address):
        print("{0} error, ip address must like 192.168.1.110/24".format(address))
        return False
    else:
        print("True")
        return True
        
if __name__ == '__main__':
    while True:
        int_ip = input("input ip address like [192.168.1.110/24]:")
        if not check_network_address(int_ip):
            continue
        else:
            continue

执行结果

root@1:~/HCCB/nlp/tools# ./check_address_mask.py
input ip address like [192.168.1.110/24]:11.11.11.11/16
True
input ip address like [192.168.1.110/24]:11.11.11.11/33
11.11.11.11/33 error, ip address must like 192.168.1.110/24
input ip address like [192.168.1.110/24]:11.11.11.11
11.11.11.11 error, ip address must like 192.168.1.110/24
input ip address like [192.168.1.110/24]:11.11.11.11/1
True
input ip address like [192.168.1.110/24]:11.11.11/24
11.11.11/24 error, ip address must like 192.168.1.110/24

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值