我想从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
答案 0 :(得分: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)
答案 1 :(得分: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
顺便说一下,尝试部分
中不需要中断