我想用最少的代码行从服务器检索多个日志
我想从 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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那 ?? :
我理解你只想将获取的内容记录在路径 'C:\remote\NewFile.log' 的一个文件中,
我不知道是否混合指令
sftp.get(filepath, localpath)
并且指令lf.write()
被授权。。
编辑
现在我已经明白了我可以提出更正确的代码的目标:
顺便说一句,在 try 部分中不需要 break
That ?? :
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 instructionlf.write()
is authorized..
EDIT
Now I have understood the aim I can propose a more correct code:
BY the way, there is no need of break in the try portion
我建议
这样做:
Instead of
I propose this :