0

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.

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • You need to store the values of the first file you iterate in an array, or better a dictionary, and then refer use it when you iterate the second. What output do you want? Where do you want it? –  Apr 15 '22 at 16:39
  • Yes. But don't know how feed a global list or dictionary from a For loop and use it inside another For loop. because on each iteration (which is done on each line of a text file), a different value is generated. – Adam Freemotion Apr 16 '22 at 19:53
  • Look at the answers to [Populating a dictionary with a for loop](https://stackoverflow.com/q/30280856/16653700). – Alias Cartellano Apr 16 '22 at 19:56

1 Answers1

0

There is a way to use data in two for loops within each other. First you need to create two def functions. First you have to create two def functions and make your list usable in a different function with "global x(whatever)" inside each function.

  1. The output list of the for loop is "vlan_list" as in the example below.

First def example:

def switch_connection(device_ip):

    global vlan_list # !! this is the list name you want to exclude from the function 

    vlan_list = [] # I created a list

    for line_fnk2 in output_list_fnk2:
        if int(port_vlan2) > 200 and int(port_vlan2) < 4096:
            vlan_list.append(port_vlan2) 

Second def example:

def connectionbekci(ip):

    command.send("display int des | i {}".format(vlan))
    command.expect(UPE_prompt)
    routing = command.current_output_clean
    routing = routing.splitlines()
    result = [x for x in routing if "up" and "100" in x]
    result2 = result[0]
    result3 = ''.join(result2)

Here we use the variables or lists in the functions, one inside the other. In order to use vlan_list in my second function, I made a new for under the for and defined it as "vlan".

for host in hosts:
    switch_connection(host)
    for vlan in vlan_list: # 
        description(connectionbekci(host),vlan) 
Mert Kulac
  • 294
  • 2
  • 19
  • Hi @mert . Thanks for the answer. But as a complete novice in programming, I didn't grasp the point you tried to clarify. I create a list for each line of the text file and want to add the values generated on each iteration to a dictionary, for example, and then use that dic later. – Adam Freemotion Apr 18 '22 at 20:05
  • Two for loops are a bit confusing yes :) There is another method. First put the values ​​in function 1 into a list. You can save this in an excel. Afterwards, you can read the new excel you created, use the data here and continue with your other operations. – Mert Kulac Apr 20 '22 at 12:38