Python Paramiko multi threading to capture all output for command applied in loop

My issue :
I am getting only last command output data in ouput file. Though comamnd "print(output)" displays data for all 3rd column values but the data saved in file is not what required it hs to be the same which is being printed by command"print(output)".
Could you please help me to fix this,
i think multi threading is required in this, but i dnt know how to apply in this scenario

My code :

import os
import csv
import sys
import telnetlib
import time
import datetime
import re
import os
import subprocess
#import pandas as pd
import paramiko
import csv
import os
import logging
import socket
import threading

#outlock = threading.Lock()
#directory = '/tmp/'
directory1 = '/tmp/input/'

f = open(directory1+str('universal_file.txt'))
reader = csv.reader(f, delimiter=",")
second_col = []

for row in reader:

    str1_1 = ''.join(str(e) for e in row)
    print str1_1

    for filename in os.listdir(directory1):
        if filename==str1_1:
           # print str1
            f = open(directory1+str(filename))
            reader = csv.reader(f, delimiter=",")
            second_col = []
            x=[]
            for row in reader:
                str1 = ''.join(str(e) for e in row[2])
                print row[2]
                second_col.append(row[2])
                if "c" in str(second_col):
                       second_col.remove("c")
                for data in second_col:
                    ssh = paramiko.SSHClient()
                    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
                    ssh.connect(str1_1, username='abc', password='abc')
                    stdin, stdout, stderr = ssh.exec_command('show run interface gigabitEthernet %s\r\n' %data)
                    stdin.write('xy\n')
                    stdin.flush()

#                    with outlock:
                    output = stdout.readlines()
                    output = "". join(output)
                    print(output)
#                       saved_ip.write(output)

#            print second_col

#mylist = list(dict.fromkeys(second_col))
            file_name = str1_1.rsplit('.', 1)[0]
            with open(directory1+'output/'+str(file_name)+'.csv', 'a') as f:
#                print second_col
                f.write(output)
#                for item in second_col:
#                    f.write("\n%s" % item)
        continue

Output File(10.106.222.96):

Building configuration...

Current configuration : 107 bytes
!
interface GigabitEthernet2
 no ip address
 shutdown
 negotiation auto
 no mop enabled
 no mop sysid
end

Suppose we have 3 input files with data with filename(out_10.106.222.96.csv, out_10.106.222.97.csv, out_10.106.222.98.csv) at path /tmp/input
In these input files there can be any number of rows

out_10.106.222.96.csv

a,b,c
43,17104773,1
45,17104234,2

out_10.106.222.97.csv

a,b,c
43,17104773,3
45,17104234,4 

out_10.106.222.99.csv

a,b,c
43,17104773,5
45,17104234,6

Now have to login to node one by one with IP(present in testin.csv file)
These IP matches with the above filenames(out_10.106.222.96.csv, out_10.106.222.97.csv, out_10.106.222.98.csv)
There can be case when file name is not present that matches with below IP, so we need to ignore or continue with other files.

Ip File(testin.csv)

10.106.222.96
10.106.222.97
10.106.222.99

Now have to login to node one by one.
suppose we login to node 10.106.222.96, then have to search file at path(/tmp/input) that has name with this IP(10.106.222.96)
Want to pick column 3rd values from file and have to pass in below command present in above code on line number

stdin, stdout, stderr = ssh.exec_command('show run interface gigabitEthernet %s\r\n' %data)

Like

stdin, stdout, stderr = ssh.exec_command('show run interface gigabitEthernet 1' %data)
stdin, stdout, stderr = ssh.exec_command('show run interface gigabitEthernet 2' %data)

...so on.

generate one more output file at different path say(/tmp/output) with the data gathered from above command(show run interface gigabitEthernet %s\r\n' %data)
with the name(f_10.106.222.96) or any other name but should have IP in it.

Similarly,
have to login to another node say(10.106.222.97)
suppose we login to node 10.106.222.97, then have to search file at path(/tmp/input) that has name with this IP(10.106.222.97)
Want to pick column 3rd values from file and have to pass in below command present in above code on line number

stdin, stdout, stderr = ssh.exec_command('show run interface gigabitEthernet %s\r\n' %data)

Same step have to repeat until we have logged into every node and collected the data for each file column 3 value by passing into command

Like

stdin, stdout, stderr = ssh.exec_command('show run interface gigabitEthernet 3' %data)
stdin, stdout, stderr = ssh.exec_command('show run interface gigabitEthernet 4' %data).

..so on.

generate one more output file at different path say(/tmp/output) with the data gathered from above command(show run interface gigabitEthernet %s\r\n' %data)
with the name(f_10.106.222.97) or any other name but should have IP in it.

1 Like