美文网首页
1. 检查IP地址ipaddress/netaddr/tabul

1. 检查IP地址ipaddress/netaddr/tabul

作者: 薛东弗斯 | 来源:发表于2024-06-09 08:41 被阅读0次
# 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主机,看哪些可以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

# 该函数功能是对每个IP地址发起ping命令,并记录ping的结果。能ping通的地址保存到reachable列表,
# 不能ping通的地址保存到unreachable列表。
def ping_ip_addresses(ip_addresses):
    reachable = []
    unreachable = []

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

# 该函数功能是将输入的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)

# 将地址段转成一个一个的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']

相关文章

网友评论

      本文标题:1. 检查IP地址ipaddress/netaddr/tabul

      本文链接:https://www.haomeiwen.com/subject/jepvqjtx.html