1. 檢查IP地址ipaddress/netaddr/tabulate

# check_ip_func.py
import ipaddress

def check_ip(ip):
    try:
        ipaddress.ip_address(ip)
        return True
    except ValueError as err:
        return False
    
ip1='10.1.1.1'
ip2='101.1'
print('Check IP...')
print(ip1,check_ip(ip1))
print(ip2,check_ip(ip2))

def return_correct_ip(ip_addr):
    correct = []
    for ip in ip_addr:
        if check_ip(ip):
            correct.append(ip)
    return correct

print("Checking list of ip addressed")
ip_list=['1.1.1.1','8.8.8.8','2.2.2']
correct=return_correct_ip(ip_list)
print(correct)
Check IP...
10.1.1.1 True
101.1 False
Checking list of ip addressed
['1.1.1.1', '8.8.8.8']

from netaddr import IPAddress
ip = IPAddress("192.168.1.1")
print(ip)
print(ip.version)
print(ip.bits())
print(ip.bin)
print(ip.words)
192.168.1.1
4
11000000.10101000.00000001.00000001
0b11000000101010000000000100000001
(192, 168, 1, 1)
from netaddr import IPNetwork
ip=IPNetwork("192.168.1.1/24")
print(ip)
print(ip.cidr)
print(ip.hostmask)
print(ip.network)
print(ip.netmask)
from netaddr import cidr_merge
summary_list=[IPNetwork('192.168.0.0/24'),IPNetwork('192.168.1.0/24')]
print(cidr_merge(summary_list))
from netaddr import EUI
mac=EUI("4c1fcce52d7d")
print(mac)
print(bin(mac))
from tabulate import tabulate
dis_ip_in_br=[("FatstEthernet0/0","15.0.15.1","up","up"),
              ("FatstEthernet0/1","15.0.12.1","up","up"),
              ("FatstEthernet0/2","15.0.13.1","up","up"),
              ("Loopback0","10.1.1.1","up","up"),
              ("Loopback100","100.0.0.1","up","up")]
print(tabulate(dis_ip_in_br))
----------------  ---------  --  --
FatstEthernet0/0  15.0.15.1  up  up
FatstEthernet0/1  15.0.12.1  up  up
FatstEthernet0/2  15.0.13.1  up  up
Loopback0         10.1.1.1   up  up
Loopback100       100.0.0.1  up  up
----------------  ---------  --  --
columns=['Interface','IP','Stats','Protocol']
print(tabulate(dis_ip_in_br,headers=columns))
Interface         IP         Stats    Protocol
----------------  ---------  -------  ----------
FatstEthernet0/0  15.0.15.1  up       up
FatstEthernet0/1  15.0.12.1  up       up
FatstEthernet0/2  15.0.13.1  up       up
Loopback0         10.1.1.1   up       up
Loopback100       100.0.0.1  up       up
columns=['Interface','IP','Stats','Protocol']
print(tabulate(dis_ip_in_br,headers=columns,tablefmt="grid"))
+------------------+-----------+---------+------------+
| Interface        | IP        | Stats   | Protocol   |
+==================+===========+=========+============+
| FatstEthernet0/0 | 15.0.15.1 | up      | up         |
+------------------+-----------+---------+------------+
| FatstEthernet0/1 | 15.0.12.1 | up      | up         |
+------------------+-----------+---------+------------+
| FatstEthernet0/2 | 15.0.13.1 | up      | up         |
+------------------+-----------+---------+------------+
| Loopback0        | 10.1.1.1  | up      | up         |
+------------------+-----------+---------+------------+
| Loopback100      | 100.0.0.1 | up      | up         |
+------------------+-----------+---------+------------+

ping主機(jī),看哪些可以ping通,哪些不行

# net.txt
8.8.8.8
8.8.4.4
1.1.1.1-3
192.168.0.100-192.168.0.160
# Ping_ip_address.py
import ipaddress
import subprocess

# 該函數(shù)功能是對每個IP地址發(fā)起ping命令,并記錄ping的結(jié)果。能ping通的地址保存到reachable列表,
# 不能ping通的地址保存到unreachable列表。
def ping_ip_addresses(ip_addresses):
    reachable = []
    unreachable = []

    for ip in ip_addresses:
        result = subprocess.run(
            # "-n"是ping包數(shù)量,"-w"是超時時間,單位是毫秒
            ["ping","-n", "2","-w","1000", ip], capture_output=True
        )
        if result.returncode == 0:
            reachable.append(ip)
        else:
            unreachable.append(ip)
    return reachable, unreachable

# 該函數(shù)功能是將輸入的IP地址段拆分成一個一個的IP地址
def convert_ranges_to_ip_list(ip_addresses):
    ip_list = []
    for ip_address in ip_addresses:
        if "-" in ip_address:
            start_ip, stop_ip = ip_address.split("-")
            if "." not in stop_ip:
                stop_ip = ".".join(start_ip.split(".")[:-1] + [stop_ip])
            start_ip = ipaddress.ip_address(start_ip)
            stop_ip = ipaddress.ip_address(stop_ip)
            for ip in range(int(start_ip), int(stop_ip) + 1):
                ip_list.append(str(ipaddress.ip_address(ip)))
        else:
            ip_list.append(str(ip_address))
    return ip_list

#if __name__ == '__main()__':

ip_addresses = []

# 讀取文件,獲取文件中的所有行
with open("net.txt") as f:
    lines = f.readlines()
    for line in lines:
        # 去掉每行的'\n'
        ip_addresses.append(line.strip())

print("需要Ping的IP地址是:",ip_addresses)

# 將地址段轉(zhuǎn)成一個一個的IP地址
addresses = convert_ranges_to_ip_list(ip_addresses)

# 批量ping每個IP地址,reach中存放能ping通的地址,unreach中存放不能ping通的地址,
reach,unreach = ping_ip_addresses(addresses)

print("能ping通的IP地址有:\n",reach)
print("不能ping通的IP地址有:\n",unreach)
需要Ping的IP地址是: ['8.8.8.8', '8.8.4.4', '1.1.1.1-3', '192.168.0.100-192.168.0.160']
能ping通的IP地址有:
 ['8.8.8.8', '8.8.4.4', '1.1.1.1', '1.1.1.2', '1.1.1.3', '192.168.0.100', '192.168.0.101', '192.168.0.102', '192.168.0.104', '192.168.0.105', '192.168.0.106', '192.168.0.107', '192.168.0.108', '192.168.0.109', '192.168.0.111', '192.168.0.112', '192.168.0.113', '192.168.0.114', '192.168.0.115', '192.168.0.116', '192.168.0.117', '192.168.0.118', '192.168.0.119', '192.168.0.120', '192.168.0.121', '192.168.0.122', '192.168.0.123', '192.168.0.124', '192.168.0.125', '192.168.0.126', '192.168.0.127', '192.168.0.128', '192.168.0.129', '192.168.0.130', '192.168.0.131', '192.168.0.132', '192.168.0.133', '192.168.0.134', '192.168.0.135', '192.168.0.136', '192.168.0.137', '192.168.0.138', '192.168.0.139', '192.168.0.140', '192.168.0.141', '192.168.0.142', '192.168.0.143', '192.168.0.144', '192.168.0.145', '192.168.0.146', '192.168.0.147', '192.168.0.148', '192.168.0.149', '192.168.0.150', '192.168.0.151', '192.168.0.152', '192.168.0.153', '192.168.0.154', '192.168.0.155', '192.168.0.156', '192.168.0.157', '192.168.0.158', '192.168.0.159', '192.168.0.160']
不能ping通的IP地址有:
 ['192.168.0.103', '192.168.0.110']
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,488評論 6 531
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,034評論 3 414
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,327評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,554評論 1 307
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 71,337評論 6 404
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 54,883評論 1 321
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 42,975評論 3 439
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,114評論 0 286
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 48,625評論 1 332
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 40,555評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 42,737評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,244評論 5 355
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 43,973評論 3 345
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,362評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,615評論 1 280
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,343評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 47,699評論 2 370

推薦閱讀更多精彩內(nèi)容