Experts,
Need your help for this. Please support
My motive is to create seperate output file for each Input Files(File 1 and File2) in another folder say(/tmp/finaloutput)
Input files
File 1(1.1.1.1.csv)
a,b,c
43,17104773,3
45,17104234,4
File 2(2.2.2.2.csv)
a,b,c
43,17104773,1
45,17104234,2
I am greping column 3 values from File1 and File2 and trying to use this column3 values in below code for this command :
stdin, stdout, stderr = open_channel.exec_command("show run interface gigabitEthernet %s\r\n" %data)
My motive is to create seperate output file for each Input Files(File 1 and Fil2) in another folder
Below is File where all node Ip's are placed to be used in below script for logging into node
IP File(testin.csv)
1.1.1.1
2.2.2.2
So first i have to login to node with IP(1.1.1.1) and then have to grep File1(1.1.1.1.csv) and will grep File 1 3rd column value(3,4) and want to pass values one by one to above commands and need to store data generated(from the 3rd column values 3 and 4) in output file with same name(1.1.1.1.csv) in another folder say(/tmp/finaloutput)
Again have to login to node with IP(2.2.2.2) and have to grep File2(2.2.2.2.csv) and will grep File 2 3rd column value(1,2) and want to paas values one by one to above commands and need to store data generated in output file with same name(2.2.2.2.csv) in another folder say(/tmp/finaloutput)
My code :
#!/usr/bin/env python
import sys
import telnetlib
import os
import subprocess
import paramiko
import csv
import os
import logging
open_channel = paramiko.SSHClient()
# This is the main logger object to be used throughout the script
logger = None
# Create the necessary log handles
# A log file is created with the filename specified
# Other log messages are also printed on the console as per the level
def createLogHandlers(logfile):
global logger
logging.basicConfig(filename=logfile ,
filemode='a',
level=logging.DEBUG,
format='%(asctime)s %(levelname)-8s %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S')
logger = logging.getLogger('Telnet')
# Create handler which logs messages on console
log_console = logging.StreamHandler()
log_console.setLevel(logging.DEBUG)
log_console.setFormatter(logging.Formatter('%(asctime)s %(levelname)-8s %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S'))
logger.addHandler(log_console)
# Global Objects
COLUMN_DATA = []
PATHSTR = "/tmp/input"
os.chdir (PATHSTR)
#computer_list = open("/tmp/testin.csv", 'r')
#i = []
'''
This will extract data from all the files with filename
pattern "aa<number>" present in the PATHSTR
'''
def extract_column_data():
global COLUMN_DATA
for filename in os.listdir(PATHSTR):
if filename.endswith(".csv"):
f = open(filename)
next(f)
reader = csv.reader(f, delimiter=",")
for row in reader:
print row[2]
COLUMN_DATA.append(row[2])
x=filename.rsplit('.',1)[0]
f1=open(x+str('_v1.txt'),"w")
f1.write(str(COLUMN_DATA))
f1.close()
continue
else:
continue
def my_function(i):
global COLUMN_DATA
for data in COLUMN_DATA:
saved_ip = open('/tmp/outputcommand'"%s.txt"%i, "a+")
stdin, stdout, stderr = open_channel.exec_command("show run interface gigabitEthernet %s\r\n" %data)
output = stdout.readlines()
output = "". join(output)
print(output)
saved_ip.write(output)
saved_ip.close()
### MAIN FUNCTION ###
if __name__ == "__main__":
createLogHandlers('telnet_a.log')
extract_column_data()
computer_list = open("/tmp/testin.csv", 'r')
for i in computer_list.readlines():
open_channel = paramiko.SSHClient()
open_channel.set_missing_host_key_policy(paramiko.AutoAddPolicy())
open_channel.connect(i, port = 22, username = 'abc',
password = 'abc')
my_function(i)
Error :
Traceback (most recent call last):
File "./telnet3bkpbkp3107.py", line 94, in <module>
my_function(i)
File "./telnet3bkpbkp3107.py", line 72, in my_function
stdin, stdout, stderr = open_channel.exec_command("show run interface gigabitEthernet %s\r\n" %data)
File "/usr/lib/python2.6/site-packages/paramiko/client.py", line 349, in exec_command
chan = self._transport.open_session()
File "/usr/lib/python2.6/site-packages/paramiko/transport.py", line 648, in open_session
return self.open_channel('session')
File "/usr/lib/python2.6/site-packages/paramiko/transport.py", line 738, in open_channel
raise e
EOFError
Only able to get the below data in one file, only for One value(3) for File 1.
Building configuration...
Current configuration : 107 bytes
!
interface GigabitEthernet3
no ip address
shutdown
negotiation auto
no mop enabled
no mop sysid
end