IP Address processing module IPY
the IPY module can help us to efficiently complete the planning of IP.
Reference: Https://github.com/autocracy/python-ipy
Installing the IPY module
[Email protected] ~]# python36-m easy_install-i http://pypi.douban.com/simple/IPy
Import Module
In [all]: from IPy import IP
1, ip address, basic handling of network segments
differentiate IPv4 and IPv6 by version:
in [+]: Testip = (' 10.0.0.0/8 ', ':: 1 ') in []: IP (Testip[0]). Version () out[14]: 4In [[]: IP (testip[1]). Version () out[15] : 6
Outputs the number of IP addresses and all IP address lists of the network segment through the specified network segment:
in [+]: Testip1 = IP (' 192.168.1.0/24 ') in [+]: print (Testip1.len ()) 256In [+]: for x in Testip1: ...: print (x) ...: 192 .168.1.0192.168.1.1192.168.1.2.
ip class:
in [32]: testip2 = ip (' 192.168.100.10 ') In [33 ]: testip2.reversenames () #反向解析地址格式Out [33]: [' 10.100.168.192.in-addr.arpa. '] In [34]: testip2.iptype () #返回IP类型, 192.168.100.10 for private address out[34]: ' private ' in [35]: ip (' 8.8.8.8 '). Iptype () #返回IP类型, 8.8.8.8 for common address out[35]: ' public ' in [36 ]: testip2.int () #转换成整型格式Out [36]: 3232261130in [37]: testip2.strhex () #转换成十六进制格式Out [37]: ' 0xc0a8640a ' In [38]: testip2.strbin () # Convert to binary format out[38]: ' 11000000101010000110010000001010 ' in [39]: print (IP (0xc0a8640a)) #十六进制转成IP格式192.168.100.10
network address translation, based on IP and Mask generation network segment format:
in [+]: print (' 192.168.1.0 '). Make_net (' 255.255.255.0 ') 192.168.1.0/24in []: Print (' 192.168.1.0/ 255.255.255.0 ', make_net=true)) 192.168.1.0/24in []: Print (' 192.168.1.0-192.168.1.255 ', make_net=true)) 192.168.1.0/24
Use the Strnormal method to specify different Wantprefixlen parameter values to customize the network segments for different output types. The output type is a string:
Wantprefixlen Value and Meaning:
-
wantprefixlen=0, No return, such as 192.168.1.0
-
wantprefixlen=1,prefix format, such as 192.168.1.0/24
-
wantprefixlen=2,decimalnetmask format, such as 192.168.1.0/255.255.255.0
-
wantprefixlen=3,lastip format, such as 192.168.1.0-192.168.1.255
in [52]: ip (' 192.168.1.0/24 '). Strnormal (wantprefixlen=0) Out[ 52]: ' 192.168.1.0 ' in [53]: ip (' 192.168.1.0/24 '). Strnormal (wantprefixlen=1) out[53]: ' 192.168.1.0/24 ' #省略wantprefixlenIn [54]: ip (' 192.168.1.0/24 '). Strnormal (0) out[54]: ' 192.168.1.0 ' In [55]: ip (' 192.168.1.0/24 '). Strnormal (1) out[55]: ' 192.168.1.0/24 ' In [56]: ip (' 192.168.1.0/24 '). Strnormal (2) out[56]: ' 192.168.1.0/255.255.255.0 ' in [57]: ip (' 192.168.1.0/24 '). Strnormal (3) out[57]: ' 192.168.1.0-192.168.1.255 '
2. Multi-Network Computing method
IPY supports comparisons similar to numeric data to help IP objects compare
in [+]: IP (' 10.0.0.0/24 ') > IP (' 12.0.0.0/24 ') out[67]: Falsein [[]: IP (' 10.0.0.0/24 ') < IP (' 12.0.0.0/24 ') out[68] : True
Determine if the IP address and the network segment are contained in another network segment:
In [the]: ' 192.168.1.100 ' in IP (' 192.168.1.0/24 ') out[69]: Truein [+]: ' 192.168.2.100 ' in IP (' 192.168.1.0/24 ') out[70]: Falsein [[]: ' 192.168.1.0/24 ' in IP (' 192.168.0.0/16 ') out[71]: True
Determine if there is overlap in two segments, using the overlaps method provided by ipy:
In [All]: IP (' 192.168.0.0/23 '). overlaps (' 192.168.1.0/24 ') out[74]: 1 #返回1代表存在重叠In []: IP (' 192.168.1.0/24 '). Overlaps ( ' 192.168.2.0 ') out[75]: 0# returns 0 means no overlap exists
Example: Returns information such as Network, mask, broadcast, reverse resolution, subnet number, IP type, and so on, based on the IP or subnet entered:
[Email protected] pkg1]# vim testip.py
#!/usr/bin/python#coding:utf-8#from IPy Import ipimport sys reload (SYS) sys.setdefaultencoding (' utf-8 ') ip_test = Raw_ Input (' Please enter IP address or network segment address: ') ips = IP (ip_test) If Len (IPs) > 1:print (' Network address:%s '% ips.net ()) Print (' Mask:%s '% ips.netmask ()) p Rint (' Network broadcast address:%s '% ips.broadcast ()) print (' Address reverse resolution:%s '% ips.reversenames ()) Print (' Network subnets:%s '% len (IPs)) Else:print (' I P Reverse Parse%s '% ips.reversenames ()) print (' This IP address translates to 16 binary:%s '% Ips.strhex ()) print (' This IP address is converted to binary:%s '% Ips.strbin ()) print (' This I P Address type:%s '% Ips.iptype ())
Execution Result:
[[email protected] pkg1]# python testip.py Please enter IP address or network segment address: 192.168.1.0/ 24 Network Address: 192.168.1.0 Mask: 255.255.255.0 network broadcast address: 192.168.1.255 address reverse resolution: [' 1.168.192.in-addr.arpa. '] Number of network subnets: 256 This IP address translates to 16 binary: 0xc0a80100 This IP address is converted to binary: 11000000101010000000000100000000 This IP address type: Private[[email protected] pkg1]# python testip.py Please enter IP address or segment address: 192.168.1.1IP reverse parsing [' 1.1.168.192.in-addr.arpa. '] This IP address is converted to 16 binary: 0xc0a80101 This IP address is converted to binary: 11000000101010000000000100000001 This IP address type: PRIVATE
This article is from the "Shei" blog, make sure to keep this source http://kurolz.blog.51cto.com/11433546/1935068
Python learning note-ip address processing module IPY