我想用最少的代码行从服务器检索多个日志

发布于 2024-11-04 12:57:58 字数 1345 浏览 0 评论 0原文

我想从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏九 2024-11-11 12:58:02

那 ?? :

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

That ?? :

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

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 instruction lf.write() is authorized.

.

EDIT

Now I have understood the aim I can propose a more correct code:

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

BY the way, there is no need of break in the try portion

夏夜暖风 2024-11-11 12:58:01

我建议

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)

Instead of

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

I propose this :

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文