您的位置:首页 > 理论基础 > 计算机网络

根据输入的IP或子网返回网络、掩码、广播、反向解析、子网数、IP类型等信息

2016-09-07 15:12 483 查看
根据输入的IP或子网返回网络、掩码、广播、反向解析、子网数、IP类型等信息需要的模块为IPy模块 此处为python3版本
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from IPy import IP

ip_s = input('Please input an IP or net-range: ')    #参数为IP或网段
ips = IP(ip_s)
if len(ips) > 1:                #网络地址个数
print('net: %s' % ips.net())      #输出网络地址
print('netmask: %s' % ips.netmask())   #掩码地址
print('broadcast: %s' % ips.broadcast())  #广播
print('reverse address: %s' % ips.reverseNames()[0])   #输出地址反向解析
print('subnet: %s' % len(ips))     #输出网络子网数
else:         #若上面Input的值为一个IP地址
print('reverse address: %s' % ips.reverseNames()[0])   #输出IP地址反向解析

print('hexadecimal: %s' % ips.strHex())    #输入16进制地址
print('binary ip: %s' % ips.strBin())      #2进制
print('iptype: %s' % ips.iptype())         #输出IP地址类型
运行结果:
[root@www python]# python IP1.pyPlease input an IP or net-range: 192.168.1.0/24net: 192.168.1.0netmask: 255.255.255.0broadcast: 192.168.1.255reverse address: 1.168.192.in-addr.arpa.subnet: 256hexadecimal: 0xc0a80100binary ip: 11000000101010000000000100000000iptype: PRIVATEYou have new mail in /var/spool/mail/root[root@www python]# python IP1.pyPlease input an IP or net-range: 192.168.1.102reverse address: 102.1.168.192.in-addr.arpa.hexadecimal: 0xc0a80166binary ip: 11000000101010000000000101100110iptype: PRIVATE
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐