我想从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代替
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)