简体   繁体   English

从一个文件中读取一个IP地址,然后在另一个文件中查找该IP并打印出对应的界面

[英]Read an IP address from one file, then lookup that IP in another file and printout the correspondent interface

Please pardon my very limited knowledge on Python. 请原谅我对Python非常有限的知识。 I am totally a newbie to programming. 我完全是编程的新手。 I have two txt files. 我有两个txt文件。 One called "ips.txt" and the other one called "l2circuitconfig.txt". 一个叫做“ ips.txt”,另一个叫做“ l2circuitconfig.txt”。 "ips.txt" has 3 ip addresses and "l2circuitconfig.txt" has configurations for more than 1000 ip addresses/lt interfaces. “ ips.txt”具有3个ip地址,“ l2circuitconfig.txt”具有1000多个ip地址/ lt接口的配置。 I have to write a code to read the ips from "ip.txt", then look into "l2circuitconfig.txt" for that perticluar ip address and print out the corresponding "lt" interface listed in that file. 我必须编写代码以从“ ip.txt”中读取ip,然后在“ l2circuitconfig.txt”中查找该perticluar ip地址,并打印出该文件中列出的相应“ lt”接口。

Contents of the "ips.txt" file: “ ips.txt”文件的内容:

10.254.1.31
10.254.1.47
10.254.2.53

Contents of the "l2circuitconfig.txt" file: “ l2circuitconfig.txt”文件的内容:

set logical-systems INNI01-ls protocols l2circuit neighbor 10.254.1.31 interface lt-5/0/0.0 static incoming-label 1001031 outgoing-label 790833
set logical-systems INNI01-ls protocols l2circuit neighbor 10.254.1.31 interface lt-5/0/0.0 virtual-circuit-id 1001031
set logical-systems INNI01-ls protocols l2circuit neighbor 10.254.1.31 interface lt-5/0/0.0 protect-interface lt-7/0/0.0
set logical-systems INNI01-ls protocols l2circuit neighbor 10.254.1.31 interface lt-5/0/0.0 encapsulation-type ethernet-vlan
set logical-systems INNI01-ls protocols l2circuit neighbor 10.254.1.47 interface lt-5/0/0.1 static incoming-label 1001047 outgoing-label 790855
set logical-systems INNI01-ls protocols l2circuit neighbor 10.254.1.47 interface lt-5/0/0.1 virtual-circuit-id 1001047
set logical-systems INNI01-ls protocols l2circuit neighbor 10.254.1.47 interface lt-5/0/0.1 protect-interface lt-7/0/0.1
set logical-systems INNI01-ls protocols l2circuit neighbor 10.254.1.47 interface lt-5/0/0.1 encapsulation-type ethernet-vlan
set logical-systems INNI01-ls protocols l2circuit neighbor 10.254.2.53 interface lt-5/3/0.1 static incoming-label 1002053 outgoing-label 791123
set logical-systems INNI01-ls protocols l2circuit neighbor 10.254.2.53 interface lt-5/3/0.1 virtual-circuit-id 1002053
set logical-systems INNI01-ls protocols l2circuit neighbor 10.254.2.53 interface lt-5/3/0.1 protect-interface lt-7/3/0.1
set logical-systems INNI01-ls protocols l2circuit neighbor 10.254.2.53 interface lt-5/3/0.1 encapsulation-type ethernet-vlan

Kind Rgds, Rutvij 鲁吉(Rutvij)

As i am a newbie, i only know how to read a file. 由于我是新手,所以我只知道如何读取文件。

Have now written anything yet. 现在已经写了任何东西。

I expect out to be something like below: 我期望是这样的:

10.254.1.31 - lt-5/2/0.20

first make an ip list from ips.txt then check in l2circuitconfig.txt . 首先从ips.txt制作一个IP列表,然后检入l2circuitconfig.txt。 where regex is used to find ip from each line: regex用于从每一行查找ip:

import re
ipList = []
with open('ips.txt', "r") as f:
    for line in f:
        ipList.append(line.strip())

with open('l2circuitconfig.txt', "r") as f:
    for line in f:
        ipAddr = re.findall( r'[0-9]+(?:\.[0-9]+){3}', line )[0]
        if ipAddr in ipList:
            d = line.split()
            print(d[6], '-', d[8])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM