Write a simple Python script to return the IP address of the mask, the number of subnets, etc.

Source: Internet
Author: User
Tags get ip python script

If we want to quickly get an IP address segment has how many IP, quickly get IP address segment subnet mask, or quickly get a binary IP address, then you can learn.
This article uses a Python IP analysis module IPY implementation, first install the IPY module

wget https://pypi.python.org/packages/88/28/79162bfc351a3f1ab44d663ab3f03fb495806fdb592170990a1568ffbf63/IPy-0.83.tar.gztar -xf IPy-0.83.tar.gz cd IPy-0.83python setup.py install

Let's take a brief look at the features

>> from IPy import IP
>> IP = IP (' 192.168.0.0/28 ')
>> Print Ip.len () # #输出ip网段的所有ip个数
16
>> for x in IP: # #将所有的ip便利出来
... print X
...
192.168.0.0
192.168.0.1
192.168.0.2
192.168.0.3
192.168.0.4
192.168.0.5
192.168.0.6
192.168.0.7
192.168.0.8
192.168.0.9
192.168.0.10
192.168.0.11
192.168.0.12
192.168.0.13
192.168.0.14
192.168.0.15

>> IP (' 192.168.1.1 '). Strbin () # #输出IP地址的二进制
' 11000000101010000000000100000001 '

>> IP (' 192.168.0.0/23 '). netmask () # #输出ip地址段的子网掩码
IP (' 255.255.254.0 ')

>> IP (' 192.168.1.1 '). Iptype () # #输出IP地址的类型公有或者私有等
' PRIVATE '
>> IP (' 8.8.8.8 '). Iptype ()
' Public '

Then write a script that returns various results based on the IP address entered:

[[email protected] python]# cat ip_test.py #!/usr/bin/python#-*- coding:utf-8 -*-from IPy import IPip_input = raw_input("请输入一个ip地址或者地址段: ")ips = IP(ip_input)if int(len(ips)) > 1 :   ##判断IP地址个数,大于1代表输入的是地址段    print "网段: %s" % ips.net()    print "ip个数: %s" % len(ips)    print "子网掩码: %s" % ips.netmask()    print "广播地址: %s" % ips.broadcast()else :   ##输入的是一个IP地址    print "二进制为: %s" % ips.strBin()    print "地址的类型: %s" % ips.iptype()

Test it:

输入IP地址测试[[email protected] python]# python ip_test.py 请输入一个ip地址或者地址段: 192.168.1.1二进制为: 11000000101010000000000100000001地址的类型: PRIVATE输入IP地址段测试[[email protected] python]# python ip_test.py 请输入一个ip地址或者地址段: 192.168.0.0/23网段: 192.168.0.0ip个数: 512子网掩码: 255.255.254.0广播地址: 192.168.1.255

There are many functions about IP address processing module IPY, you can use it as needed.

Write a simple Python script to return the IP address of the mask, the number of subnets, etc.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.