5

我想从 Ubuntu 服务器(在 win 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
4

2 回答 2

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)
于 2011-05-02T13:10:28.480 回答
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

顺便说一句,在尝试部分不需要休息

于 2011-05-02T14:26:57.443 回答