我想用最少的代码从服务器获取多个日志

5 投票
2 回答
2751 浏览
提问于 2025-04-16 16:49

我想从一台Ubuntu服务器上获取多个日志文件(我是在Windows 7的机器上用Python 2.7)。我不想写那些冗长又重复的代码。我知道可以用循环来实现这个,但我就是想不出一个有效的解决办法(我还是个编程小白)。我需要一些更有经验的人的指导。提前感谢大家的帮助。下面是我用来登录服务器并获取一个文件的代码。还有我想同时获取的文件路径示例:

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

我还有很多其他的路径,但我想你们应该明白我的意思。下面是用来登录服务器的代码:

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

2 个回答

1

那是 ?? :

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

我明白你想把获取的内容记录到一个路径为 'C:\remote\NewFile.log' 的文件里。

我不确定把指令 sftp.get(filepath, localpath) 和指令 lf.write() 混合在一起使用是否可以。

.

编辑

现在我明白了你的目的,我可以提供一个更正确的代码:

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

顺便说一下,在 try 部分不需要 break

6

不要用下面这个:

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

我建议用这个:

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)

撰写回答