I used Netmiko to connect to a device and ran 2 commands and saved them into 2 separate text files. Content of first file look something like this:
Physical interface: xe-0/0/0
Laser output power : 0.5290 mW / -2.77 dBm
Laser receiver power : 0.4988 mW / -3.02 dBm
Physical interface: xe-0/0/1
Laser output power : 0.5390 mW / -2.68 dBm
Laser receiver power : 0.5807 mW / -2.36 dBm
Physical interface: xe-1/0/1
Laser output power : 0.6020 mW / -2.20 dBm
Laser receiver power : 0.5307 mW / -2.75 dBm
Physical interface: xe-1/0/2
Laser output power : 0.7740 mW / -1.11 dBm
Laser receiver power : 0.0000 mW / - Inf dBm
Physical interface: xe-1/0/3
Laser output power : 0.5550 mW / -2.56 dBm
Laser receiver power : 0.5472 mW / -2.62 dBm
Physical interface: xe-1/0/5
Laser output power : 0.6280 mW / -2.02 dBm
Laser receiver power : 0.4580 mW / -3.39 dBm
and content of 2nd text file:
Interface Admin Link Description
xe-1/0/1 up up test-desc-101
xe-1/0/3 up up CAN-1-OLT
xe-1/0/5 up up test-desc-105
xe-1/0/5.100 up up CANALU-UPLINK
xe-1/0/6 up down test-desc-106
ae40 up up CAN-1-OLT-LAG
ae40.19 up up CAN-1-OLT-MGMT
I opened them with "with open()" and iterate every line just like the following:
with open("toutput_des.txt", "r") as meal_no_empty:
for line in meal_no_empty:
stripped_ifdes = line.strip()
splitted_ifdes = stripped_ifdes.split(" ")
and for the second file:
with open("toutput_sfp.txt", "r") as file2:
print("Interface ", " Optic out pw ", " optic rec pw")
for line in file2:
x = 0
if "xe-" in line:
stripped_ifname = line.strip()
splitted_ifname = stripped_ifname.split(" ")
x = 1
elif "output" in line:
stripped_output_power = line.strip()
splitted_output_power = stripped_output_power.split (" ")
x = 1
elif "receiver" in line:
stripped_rec_power = line.strip()
splitted_rec_power = stripped_rec_power.split (" ")
x = 0
else:
pass
if x == 0:
print(splitted_ifname," ",splitted_output_power," ",splitted_rec_power)
My goal is capturing description of the interfaces in one file and add them to the second file. I thought if I could make a global list from values generated in each for loop iteration, then I could use call members of that global list in the second for loop.
I'm a beginner in programming/scripting world, so maybe I'm not able to ask my question very clearly or in a right way. So as a simple summary, I can ask, how to create a list and populate the items of the list based on the data generated via for loop iteration and then use the list inside another for loop?
Thanks.