python 實(shí)現(xiàn)IP子網(wǎng)計(jì)算
0. 前言
IP地址目前存在兩個(gè)版本:IPv4和IPv6,平常我們見到最多的就是IPv4了,如192.168.1.1/24,當(dāng)然,IPv4地址池資源緊缺,IPv6已悄然大量部署了。
我們?cè)谠O(shè)計(jì)網(wǎng)絡(luò)架構(gòu)時(shí)必須要對(duì)設(shè)備互聯(lián)地址、環(huán)回地址、業(yè)務(wù)地址進(jìn)行規(guī)劃,那怎么規(guī)劃?給你一個(gè)A類地址你怎么辦?最重要是不是得計(jì)算?口算怕不準(zhǔn)確吧?心算行不行,就不怕你沒這本事,哈哈!
下面請(qǐng)用python幫你搞定這一切吧!
1. ipaddress模塊介紹
1.1 IP主機(jī)地址
說明:不帶掩碼
怎么判斷是ipv4地址,還是ipv6地址呢?使用ipaddress.ip_address() 函數(shù)可以來知曉:
>>> ipaddress.ip_address('192.168.1.1')
IPv4Address('192.168.1.1')
>>> ipaddress.ip_address('192.168.1.1').version
4
>>> ipaddress.ip_address('fe80::1')
IPv6Address('fe80::1')
>>> ipaddress.ip_address('fe80::1').version
6
如果帶上掩碼就會(huì)報(bào)錯(cuò):
>>> ipaddress.ip_address('192.168.1.1/32')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.5/ipaddress.py", line 54, in ip_address
address)
ValueError: '192.168.1.1/32' does not appear to be an IPv4 or IPv6 address
1.2 定義網(wǎng)絡(luò)
說明:表示網(wǎng)段
一個(gè)IP地址,通常由網(wǎng)絡(luò)號(hào)+網(wǎng)絡(luò)前綴組成,如192.168.1.0/24,可以通過ipaddress.ip_network函數(shù)來表示,缺省情況下,python只能識(shí)別網(wǎng)絡(luò)號(hào),如果是IP主機(jī)就會(huì)報(bào)錯(cuò),當(dāng)然你可以通過strict=False來避免。
>>> ipaddress.ip_network('192.168.1.0/24')
IPv4Network('192.168.1.0/24')
#缺省,輸入主機(jī)位就會(huì)報(bào)錯(cuò)
>>> ipaddress.ip_network('192.168.1.1/24')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.5/ipaddress.py", line 74, in ip_network
return IPv4Network(address, strict)
File "/usr/lib/python3.5/ipaddress.py", line 1536, in __init__
raise ValueError('%s has host bits set' % self)
ValueError: 192.168.1.1/24 has host bits set #提示是主機(jī)IP
#修改位非嚴(yán)格模式,缺省為strict=True
>>> ipaddress.ip_network('192.168.1.1/24' , strict=False)
IPv4Network('192.168.1.0/24') #返回網(wǎng)絡(luò)號(hào)
1.3 主機(jī)接口
說明:表示接口地址(ip/掩碼)
一般在路由器、交換機(jī)、防火墻接口上配置IP地址,格式如192.168.1.1/24,如果使用以上ipaddress.ip_address()和ipaddress.ip_network函數(shù)的話,就不太好表示,那么可以通過ipaddress.ip_interface()函數(shù)類表示。
>>> ipaddress.ip_interface('192.168.1.1/24')
IPv4Interface('192.168.1.1/24')
1.4 檢查address/network/interface對(duì)象
1.4.1 檢查IP版本(v4或者v6):
>>> ipaddress.ip_address('192.168.1.1').version
4
>>> ipaddress.ip_address('fe80::1').version
6
1.4.2 從接口IP獲取網(wǎng)段
>>> ipaddress.ip_interface('192.168.1.1/24').network
IPv4Network('192.168.1.0/24')
>>> ipaddress.ip_interface('fe80::/64').network
IPv6Network('fe80::/64')
1.4.3 計(jì)算網(wǎng)段有多少個(gè)IP地址
>>> ipaddress.ip_network('192.168.1.0/24').num_addresses
256
>>> ipaddress.ip_network('fe80::/64').num_addresses
18446744073709551616
1.4.4 計(jì)算網(wǎng)段有多少個(gè)可用IP地址
>>> net = ipaddress.ip_network('192.168.1.0/24')
>>> for x in net.hosts():
... print(x)
...
192.168.1.1
192.168.1.2
...
192.168.1.100
192.168.1.101
...
192.168.1.254
>>> [x for x in net.hosts()][0] #獲取第一個(gè)可用IP
IPv4Address('192.168.1.1')
>>> [x for x in net.hosts()][-1] #獲取最后一個(gè)可用IP
IPv4Address('192.168.1.254')
1.4.5 獲取掩碼與反掩碼
>>> ipaddress.ip_network('192.168.1.1/24' , strict=False).netmask
IPv4Address('255.255.255.0') #獲取掩碼
>>> ipaddress.ip_network('192.168.1.1/24' , strict=False).hostmask
IPv4Address('0.0.0.255') #獲取反掩碼
1.6 獲取網(wǎng)絡(luò)號(hào)與廣播地址
>>> ipaddress.ip_network('192.168.1.1/24' , strict=False).network_address
IPv4Address('192.168.1.0') #獲取網(wǎng)絡(luò)號(hào)
>>> ipaddress.ip_network('192.168.1.1/24' , strict=False).broadcast_address
IPv4Address('192.168.1.255') #獲取廣播地址
1.7 異常處理
如果遇到IP地址格式不符合要求等這些情況,那怎么處理呢?
#錯(cuò)誤顯示,報(bào)"ValueError"
>>> ipaddress.ip_network('192.168.1.1/24')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.5/ipaddress.py", line 74, in ip_network
return IPv4Network(address, strict)
File "/usr/lib/python3.5/ipaddress.py", line 1536, in __init__
raise ValueError('%s has host bits set' % self)
ValueError: 192.168.1.1/24 has host bits set
#通過try-except語句來處理異常情況
>>> import ipaddress
>>> def cal_ip(net):
... try:
... net = ipaddress.ip_network(net)
... print(net)
... except ValueError:
... print('您輸入格式有誤,請(qǐng)檢查!')
...
>>> cal_ip(net = '192.168.1.1/24')
您輸入格式有誤,請(qǐng)檢查!
2. 計(jì)算IP子網(wǎng)代碼演示
2.1 完整代碼
#!/usr/bin/env python3
#-*- coding:UTF-8 -*-
#歡迎關(guān)注微信公眾號(hào):點(diǎn)滴技術(shù)
import ipaddress
def cal_ip(ip_net):
try:
net = ipaddress.ip_network(ip_net, strict=False)
print('IP版本號(hào): ' + str(net.version))
print('是否是私有地址: ' + str(net.is_private))
print('IP地址總數(shù): ' + str(net.num_addresses))
print('可用IP地址總數(shù): ' + str(len([x for x in net.hosts()])))
print('網(wǎng)絡(luò)號(hào): ' + str(net.network_address))
print('起始可用IP地址: ' + str([x for x in net.hosts()][0]))
print('最后可用IP地址: ' + str([x for x in net.hosts()][-1]))
print('可用IP地址范圍: ' + str([x for x in net.hosts()][0]) + ' ~ ' + str([x for x in net.hosts()][-1]))
print('掩碼地址: ' + str(net.netmask))
print('反掩碼地址: ' + str(net.hostmask))
print('廣播地址: ' + str(net.broadcast_address))
except ValueError:
print('您輸入格式有誤,請(qǐng)檢查!')
if __name__ == '__main__':
ip_net = '192.168.1.1/24'
cal_ip(ip_net)
2.2 運(yùn)行結(jié)果
IP版本號(hào): 4 是否是私有地址: True IP地址總數(shù): 256 可用IP地址總數(shù): 254 網(wǎng)絡(luò)號(hào): 192.168.1.0 起始可用IP地址: 192.168.1.1 最后可用IP地址: 192.168.1.254 可用IP地址范圍: 192.168.1.1 ~ 192.168.1.254 掩碼地址: 255.255.255.0 反掩碼地址: 0.0.0.255 廣播地址: 192.168.1.255
3. 碎碎語
怎么樣,學(xué)完之后是不是很亢奮,不需要借助其他工具進(jìn)行計(jì)算了吧,用python就幫你搞定了。
3.1 官方參考文檔
https://docs.python.org/3.8/howto/ipaddress.html
以上就是python 實(shí)現(xiàn)IP子網(wǎng)計(jì)算的詳細(xì)內(nèi)容,更多關(guān)于python IP子網(wǎng)計(jì)算的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python之兩種模式的生產(chǎn)者消費(fèi)者模型詳解
今天小編就為大家分享一篇Python之兩種模式的生產(chǎn)者消費(fèi)者模型詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-10-10
python 爬取古詩(shī)文存入mysql數(shù)據(jù)庫(kù)的方法
這篇文章主要介紹了python 爬取古詩(shī)文存入mysql數(shù)據(jù)庫(kù)的方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01
DRF之請(qǐng)求與響應(yīng)的實(shí)現(xiàn)
本文主要介紹了DRF請(qǐng)求與響應(yīng)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-07-07
python 動(dòng)態(tài)遷移solr數(shù)據(jù)過程解析
這篇文章主要介紹了python 動(dòng)態(tài)遷移solr數(shù)據(jù)過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
Python調(diào)用系統(tǒng)底層API播放wav文件的方法
這篇文章主要介紹了Python調(diào)用系統(tǒng)底層API播放wav文件的方法,涉及Python使用pywin32調(diào)用系統(tǒng)底層API讀取與播放wav文件的相關(guān)操作技巧,需要的朋友可以參考下2017-08-08
Python數(shù)據(jù)類型--字典dictionary
這篇文章主要介紹了Python數(shù)據(jù)類型字典dictionary,字典是另一種可變?nèi)萜髂P?,且可存?chǔ)任意類型對(duì)象。下面詳細(xì)內(nèi)容需要的小伙伴可以參考一下,希望對(duì)你有所幫助2022-02-02
Python中實(shí)現(xiàn)單例模式的n種方式和原理
這篇文章主要介紹了Python中實(shí)現(xiàn)單例模式的n種方式和原理,需要的朋友可以參考下2018-11-11

