creating separate output file for each input file in python
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)
Code:
a,b,c
43,17104773,3
45,17104234,4
File 2(2.2.2.2.csv)
Code:
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 :
Code:
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
Code:
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 :
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 :
Code:
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.
Code:
Building configuration...
Current configuration : 107 bytes
!
interface GigabitEthernet3
no ip address
shutdown
negotiation auto
no mop enabled
no mop sysid
end
How would I echo the same results to two files? One is a running log, and the other is a cache, or sort.
echo "Hello World" >> /Logs/File1 & /tmp/file2
I would just copy it from one place to the other, but the Log keeps history, where I want the /tmp to hold only stuff per session. I tried... (2 Replies)
I have a file where i have files name with absolute path.
Ex: files.dat and contents are:
/root/xy/yz/zz/abc.txt
/root/xx/yy/zz/ac.txt
/root/xz/yz/zx/bc.txt
/root/xy/yz/zx/abcd.txt
now i want to create all above files(dummy files and can be 0 byte). i thought of using touch but it doesn't... (10 Replies)
Hi,
I have two files , one file with data file with attributes that need to be sent to another file to generate a predefined format.
Example:
File.txt
AP|{SSHA}VEEg42CNCghUnGhCVg==
APVG3|{SSHA}XK|"password"
AP3|{SSHA}XK|"This is test"
....
etc
---------
test.sh has... (1 Reply)
unix program to which a directory name will be passed as
parameter. This directory will contain files with various
extensions. This script will create directories with the names of the
extention of the files and then put the files in the
corresponding folder. All files which do not have any... (2 Replies)
Hi,
i want to extract the 'CREATE INDEX' or 'CREATE UNIQUE INDEX' statements from a ddl file and output each match to a separate file. i was looking around the net but couldnīt find anything.
a possible sed-script could be:
sed -n '/CREATE*INDEX*/,/COMMIT/p' filename.ddlbut i couldnīt find out... (11 Replies)
Hello, I have a pretty simple question, but I am new to Python and am trying to write a simple program. Put simply, I want to take a text file that looks like this:
11111 22222
33333 44444
55555 66666
77777 88888
and produce two lists, one containing the contents of the left column, one the... (0 Replies)
Hi,
Need some help with creating a file from input steam. Meaning from following command myfunc should be able to store the input stream to a file.
echo a b c | myfunc
The file thus created should have -
a
b
c
Here's what I've tried in myfunc() but didn't help -
myfunc() { cat... (3 Replies)
I have two files:
smw:/working/iso_testing # cat a
QConvergeConsoleCLI-1.1.03-49.x86_64.rpm
aaa_base-13.2+git20140911.61c1681-1.3.i586.rpm
acpica-20140724-2.1.2.i586.rpm
test.rpm
smw:/working/iso_testing # cat b
QConvergeConsoleCLI-1.1.03-49.x86_64.rpm... (12 Replies)
Excuses for the long descriptive title.
I am working with Sindhi and developing a database of all verbal conjugations in that language.
I have generated 2 files:
Verbs.dic contains all the verbs, one verb per line
Inflections.dic contains the verbal conjugations which need to be appended to... (6 Replies)
Hi Experts,
I am writing a python script to grep string from file and display output in csv file as in attached screenshot
https://drive.google.com/file/d/1gfUUdfmQma33tz65NskThYDhkZUGQO0H/view
Input file(result_EPFT_config_device)
Below is the python script i have prepared as of... (1 Reply)