You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何批量读取多文件并关联查询IP、MAC与端口信息

实现IP-MAC-Port映射查询

现有文件内容

file1.txt(待查询IP列表)

1.1.1.1
1.1.1.2
1.1.1.3
1.1.1.6
1.1.1.11

file2.txt(IP与MAC对应表)

Protocol  Address   Age (min)  Addr            Type   Interface
Internet  1.1.1.1         5    6026.aa11.1111  A      Ethernet1/49
Internet  1.1.1.2         -    0006.f2d2.2d2f  A      Vlan1
Internet  1.1.1.3         -    6026.aa33.3333  A      Vlan1
Internet  1.1.1.4         0    Incomplete      A
Internet  1.1.1.5         0    Incomplete      A
Internet  1.1.1.6         64   fa16.6edb.6666  A      Vlan1
Internet  1.1.1.11        23   fa16.7e7d.7777  A      Vlan1

file3.txt(MAC与端口对应表)

Unicast Entries
 vlan     mac address     type        protocols               port
---------+---------------+--------+---------------------+-------------------------
 1        6026.aa11.1111   static  ip,ipx,assigned,other Switch
 1        0006.f2d2.2d2f   dynamic ip,ipx,assigned,other Ethernet1/24
 1        6026.aa33.3333   dynamic ip,ipx,assigned,other Ethernet1/12
 1        fa16.6edb.6666   dynamic ip,ipx,assigned,other Ethernet1/8
 1        fa16.7e7d.7777   dynamic ip,ipx,assigned,other Ethernet1/10

需求描述

需要实现以下逻辑:

  • 遍历file1中的每个IP,检查是否存在于file2中
  • 若存在,提取该IP对应的file2第4列(Addr)的MAC地址
  • 再在file3中查找该MAC地址,提取对应行的最后一列(port)信息
  • 最终按ip {ip} addr {addr} port {port}格式输出结果

初始代码框架

with open('file1.txt', 'r') as file1:
    with open('file2.txt', 'r') as file2:
        with open('file3.txt', 'r') as file3:
            # if ip in file1 found in file2,
            # then store Add value of that ip in a variable
            # then find that value in file3,
            # if it's found, then store the last column of that 'mac address' into another variable
            # finally print(f'ip {ip} addr {addr} port {port}')

完整实现代码

# 构建IP到MAC的映射字典
ip_mac_map = {}
with open('file2.txt', 'r') as file2:
    # 跳过表头行
    next(file2)
    for line in file2:
        # 按空格分割并过滤空字符串,处理多行空格的情况
        parts = list(filter(None, line.strip().split()))
        if len(parts) >= 4:
            ip = parts[1]
            mac = parts[3]
            # 跳过MAC为Incomplete的条目
            if mac != 'Incomplete':
                ip_mac_map[ip] = mac

# 构建MAC到端口的映射字典
mac_port_map = {}
with open('file3.txt', 'r') as file3:
    # 跳过前3行(标题、表头、分隔线)
    for _ in range(3):
        next(file3)
    for line in file3:
        parts = list(filter(None, line.strip().split()))
        if len(parts) >= 5:
            mac = parts[1]
            port = parts[-1]
            mac_port_map[mac] = port

# 遍历file1的IP,查询并输出结果
with open('file1.txt', 'r') as file1:
    for ip in file1:
        ip = ip.strip()
        if ip in ip_mac_map:
            mac = ip_mac_map[ip]
            if mac in mac_port_map:
                port = mac_port_map[mac]
                print(f'ip {ip} addr {mac} port {port}')

预期输出

ip 1.1.1.1 addr 6026.aa11.1111 port Switch
ip 1.1.1.2 addr 0006.f2d2.2d2f port Ethernet1/24
ip 1.1.1.3 addr 6026.aa33.3333 port Ethernet1/12
ip 1.1.1.6 addr fa16.6edb.6666 port Ethernet1/8
ip 1.1.1.11 addr fa16.7e7d.7777 port Ethernet1/10

内容的提问来源于stack exchange,提问作者NomeEoleSem

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 23:47:33