Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to retrieve multiple logs from a server with minimum lines of code

I would like to retrieve multiple log files from an Ubuntu server (using Python 2.7 on win 7 machine) without having to write verbose, repetitive code. I'm sure I can use a loop to accomplish this, but I can't come up with any valid solutions (neophyte programmer). I need the direction of someone more seasoned than I. In advanced, I appreciate the help. Below is the code I'm using in my script to log into a server and retrieve one file. Below is a sample path of files I would like 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 several more paths, but I imagine you get the idea. Below is the code used to log 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 SFTP 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
like image 768
suffa Avatar asked May 02 '11 13:05

suffa


2 Answers

Instead of

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

I propose 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)
like image 97
Cédric Julien Avatar answered Sep 21 '22 00:09

Cédric Julien


That ?? :

def do_siteserver(self, line):
   import paramiko

   host = '10.5.48.65'
   port = 22
   username = 'gilbert'
   password = raw_input("\nEnter the SiteServer weekly password: ") 

   localpath = 'C:\\remote\\NewFile.log'
   paramiko.util.log_to_file('c:\Python27\paramiko-wininst.log')

   with open(localpath,'w') as lf:

       for filepath in ('/var/log/apache/a.log',
                        '/var/log/apache/e.log',
                        '/var/opt/smart/log/me.log'
                        '/var/opt/smart/log/se.log'):
           try:
               print '\nEstablishing SFTP connection to: {}: {}...'.format(host,port)
               transport = paramiko.Transport((host,port))
               transport.connect(username = username, password = password)
               sftp = paramiko.SFTPClient.from_transport(transport)
               print 'Authorization Successful!!!'

               lf.write("Content of server's file :   "+filepath+'\n\n')
               sftp.get(filepath, localpath)
               # or sftp.get(filepath, lf) ? 
               sftp.close()
               transport.close()
               lf.write("\n\n\n")

            except:
               print "\nAuthorization Failed!!!"
               break

I understood that you want to record the got contents in only one file of path 'C:\remote\NewFile.log'

I don't know if mixing instruction sftp.get(filepath, localpath) and instruction lf.write() is authorized.

.

EDIT

Now I have understood the aim I can propose a more correct code:

def do_siteserver(self, line):
   import paramiko

   host = '10.5.48.65'
   port = 22
   username = 'gilbert'
   password = raw_input("\nEnter the SiteServer weekly password: ") 

   localpath = 'C:\\remote\\NewFile'
   paramiko.util.log_to_file('c:\Python27\paramiko-wininst.log')

   for filepath in ('/var/log/apache/a.log',
                    '/var/log/apache/e.log',
                    '/var/opt/smart/log/me.log'
                    '/var/opt/smart/log/se.log'):
       try:
           print '\nEstablishing SFTP connection to: {}: {}...'.format(host,port)
           transport = paramiko.Transport((host,port))
           transport.connect(username = username, password = password)
           sftp = paramiko.SFTPClient.from_transport(transport)
           print 'Authorization Successful!!!'

           sftp.get(filepath, localpath + filepath.replace('/','_'))
           sftp.close()
           transport.close()

        except:
           print "\nAuthorization Failed!!!"
           break

BY the way, there is no need of break in the try portion

like image 44
eyquem Avatar answered Sep 21 '22 00:09

eyquem