Python Automatic operation and maintenance of IP address processing module details

  • 2020-06-15 09:37:55
  • OfStack

Practical IP address processing module IPy

In IP address planning, it involves calculating a large number of IP addresses, including network segment, network mask, broadcast address, number of subnets, IP type, etc

Don't worry, the Ipy module will save you. Ipy module can well assist us to efficiently complete the planning of IP.

Function: Assist us to finish the planning of IP efficiently

Installation:


wget https://pypi.python.org/packages/source/I/IPy/IPy-0.81.tar.gz --no-check-certificate
tar -zxvf Ipy-0.81.tar.gz
cd IPy-0.81
python setup.py install

Basic processing of IP address and network segment:

Usage:


#from IPy import IP
#ip_1 = IP('192.168.1.0/24')
#print(ip_1.len()) #  The output 192.168.1.0/24 The network segment IP The number of 
#for a in ip_1: 
# print(a) #  The output 192.168.1.0/24 All of the segments IP listing 

Conversion of IP address:


#from IPy import IP 
#ip_2 = IP('192.168.1.1') 
#ip_2.reverseNames() #  Echo parsing address format 
#ip_2.iptype() #  To view IP Address type 
#ip_2.int() #  Convert the format to an integer format 
#ip_2.strHex() #  Convert the format to 16 Hexadecimal format 
#ip_2.strBin() #  Convert the format to 2 Hexadecimal format  
#print(IP(0x8188808)) #  will 16 The base is converted to IP format 

Conversion of IP segment:


#from IPy import IP
#  The output 192.168.1.0/24 
#print(IP('192.168.1.0').make_net('255.255.255.0')) 
#print(IP('192.168.1.0/255.255.255.0',make_net=True)) 
#print(IP('192.168.1.0-192.168.1.255',make_net=True)) 
#  through strNormal()  Method specifies the above 3 Output in various formats:  
#print(IP('192.168.1.0/24').strNormal(0)) #  Parameters ( wantprefixlen ) for 0 , no return, output 192.168.1.0
#print(IP('192.168.1.0/24').strNormal(1)) #  Parameters ( wantprefixlen ) for 1 . prefix Format, output 192.168.1.0/24 
#print(IP('192.168.1.0/24').strNormal(2)) #  Parameters ( wantprefixlen ) for 2 . decimalnetmask Format, output 192.168.1.0/255.255.255.0 
#print(IP('192.168.1.0/24').strNormal(3)) #  Parameters ( wantprefixlen ) for 3 . lastIP Format, output 192.168.1.0-192.168.1.255

Multi-network computing method:

Function: Compare whether two network segments contain or overlap, such as 192.168.1.0/24 and 192.168.1.0/25; 192.168.0.0/24 and 192.168.1.0/24

Usage:


#from IPy import IP
#IP('192.168.0.0/24')<IP('192.168.1.0/24')
#  judge IP Whether the address and network segment are contained in another 1 In a network segment 
#'192.168.1.1' in IP('192.168.1.0/24') 
#  Judge whether there is overlap between two network segments and use overlaps() methods  
#IP('192.168.0.0/23').overlaps('192.168.1.0/24') #  return 1 , represents overlap  
#IP('192.168.1.0/24').overlaps('192.168.2.0/24') #  return 0 , means there is no overlap 

Return network address, broadcast address, address echo resolution, number of subnets, IP type and other information according to the input IP address or network segment address:


#from IPy import IP 
#ip_inp=raw_input(' The input IP Address or network segment address ')
#ip=IP(ip_inp)
#if len(ip)>1: #  is 1 A network segment 
# print(' The network address is: %s' %ip.net())
# print(' The subnet mask is: %s' %ip.netmask())
# print(' Broadcast address: %s' %ip.broadcast())
# print(' Address reverse resolution: %s' %ip.reverseName()[0])
# print(' The number of network subnets is: %s' %sip.len()) 
#else: #  is 1 a IP address  
# print('IP Address reverse resolution: %s' %ip.reverseName()[0])
#
#print('16 Base address: %s' %ip.strHex())
#print('2 Base address: %s' %ip.strBin())
#print('IP Address Type: %s' %ip.iptype())

conclusion


Related articles: