I hope to retrieve multiple logs from the server with the least code line.

I want to retrieve multiple log files from an Ubuntu server (using Python 2.7 on a win 7 machine) without having to write detailed repetitive code. I am sure I can use a loop to achieve this Goal, but I can’t come up with any effective solution (novice programmer). I need the guidance of someone more experienced than me. In the advanced stage, I appreciate help. Below is the code I used in the script, Used to log in to the server and retrieve a file. Here is an example path of the files I want to retrieve at the same time:

/var/log/apache/a.log
/var/log/ apache/e.log
/var/opt/smart/log/me.log
/var/opt/smart/log/se.log

I have more paths, but I think you got the idea. Here is the code for logging into the server:

def do_siteserver(self, line):
import paramiko
< br />

paramiko.util.log_to_file('c:\Python27\paramiko-wininst.log')

host = '10.5.48.65'
port = 22
transport = paramiko.Transport((host,port))


while True:
try:
print'\n'
passW = raw_input("Enter the SiteServer weekly password: ")
password = passW
username ='gilbert'
print'\n'
print'Establishing SFTP connection to :', host +':' + str(port),'...'
transport.co nnect(username = username, password = password)
sftp = paramiko.SFTPClient.from_transport(transport)
print'Authorization Successful!!!'

filepath ='/var/ log/apache2/error.log'
localpath ='C:\\remote\\NewFile.log'
sftp.get(filepath, localpath)
sftp.close()
transport.close()
break


except:
print'\n'
print "Authorization Failed!!!"
break

instead of

filepath ='/var/log/apache2/error.log'
localpath ='C:\\remote\\NewFile.log'
sftp.get(filepath, localpath)

I suggest this:

log_names = {
"/var/log/apache2/error.log":'C:\\remote\\NewFile.log' ,
"/var/log/apache/a.log":'C:\\remote\\NewFile_a.log',
} # add here all the log files you want to retrieve
for log_file, local_name in log_names.iteritems():
sftp.get(log_file, local_name)

I want to download from Ubuntu server (on win 7 machine Using Python 2.7) to retrieve multiple log files without having to write detailed repetitive code. I am sure I can use loops to achieve this, but I cannot come up with any effective solutions (novice programmers). I need a ratio I’m more experienced people’s guidance. In advanced, I appreciate the help. Below is the code I use in the script to log in to the server and retrieve a file. Below is an example path of the file I want to retrieve at the same time :

/var/log/apache/a.log
/var/log/apache/e.log
/var/opt/smart/log/me.log< br>/var/opt/smart/log/se.log

I have more paths, but I think you got the idea. Here is the code for logging into the server:

def do_siteserver(self, line):
import paramiko



paramiko.util.log_to_file('c:\ Python27\paramiko-wininst.log')

host = '10.5.48.65'
port = 22
transport = paramiko.Transport((host,port))


while True:
try:
print'\n'
passW = raw_input("Enter the SiteServer weekly password: ")
password = passW
username ='gilbert'
print'\n'
print'Establishing S FTP connection to:', host +':' + str(port),'...'
transport.connect(username = username, password = password)
sftp = paramiko.SFTPClient.from_transport( transport)
print'Authorization Successful!!!'

filepath ='/var/log/apache2/error.log'
localpath ='C:\\remote\\ NewFile.log'
sftp.get(filepath, localpath)
sftp.close()
transport.close()
break


except:
print'\n'
print "Authorization Failed!!!"
break

instead of

filepath ='/var/log/apache2/error.log'
localpath ='C:\\remote\\NewFile.log'
sftp.get(filepath, localpath)

I suggest this:

log_names = {
"/var/log/apache2/error .log":'C:\\remote\\NewFile.log',
"/var/log/apache/a.log":'C:\\remote\\NewFile_a.log',
} # add here all the log files you want to retrieve
for log_file, local_name in log_names.iteritems():
sftp.get(log_file, local_name)

Leave a Comment

Your email address will not be published.