【问题标题】:Paramiko Expect - TailingParamiko Expect - 尾矿
【发布时间】:2014-10-28 02:55:21
【问题描述】:

我正在尝试跟踪一个日志文件,它可以工作。但我还需要能够分析输出并记录错误等。我正在使用 Paramiko-expect github 页面上的基本示例,但我不知道该怎么做。

import traceback
import paramiko
from paramikoe import SSHClientInteraction

def main():

    # Set login credentials and the server prompt
    hostname = 'server'
    username = 'username'
    password = 'xxxxxxxx'
    port = 22

    # Use SSH client to login
    try:

        # Create a new SSH client object
        client = paramiko.SSHClient()

        # Set SSH key parameters to auto accept unknown hosts
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

        # Connect to the host
        client.connect(hostname, port, username, password)

        # Create a client interaction class which will interact with the host
        interact = SSHClientInteraction(client, timeout=10, display=False)

        # Send the tail command
        interact.send('tail -f /var/log/log')

        # Now let the class tail the file for us
        interact.tail(line_prefix=hostname+': ')




    except KeyboardInterrupt:
        print 'Ctrl+C interruption detected, stopping tail'
    except Exception:
        traceback.print_exc()
    finally:
        try:
            client.close()
        except:
            pass

if __name__ == '__main__':
    main()

这可以在运行它的控制台中打印日志实时时间,但我从 paramiko 期望的方式来看,我无法弄清楚如何能够迭代输出并查找值?

帮助?

https://github.com/fgimian/paramiko-expect

这也有助于将日志的输出推送到本地文件,以便对事件进行历史备份。

更新: 所以我看到这个信息的显示方式是使用sys.stdout。我对如何将输出从其中提取到可交互的方式或如何将其修改为仍然可以工作的不同类型的输出不是很老练。我曾尝试通过电子邮件向该模块的创建者发送电子邮件,但没有取得多大成功。

这个模块非常接近成为一个非常强大的工具,如果我能弄清楚实际监控输出的能力,它将使该模块成为一个非常非常好的工具。请帮忙!

Paramiko-Expect 的输出部分:尾函数:

        # Read the output one byte at a time so we can detect \n correctly
        buffer = self.channel.recv(1)

        # If we have an empty buffer, then the SSH session has been closed
        if len(buffer) == 0:
            break

        # Strip all ugly \r (Ctrl-M making) characters from the current
        # read
        buffer = buffer.replace('\r', '')

        # Add the currently read buffer to the current line output
        current_line += buffer

        # Display the last read line in realtime when we reach a \n
        # character
        if current_line.endswith('\n'):
            if line_counter and line_prefix:
                sys.stdout.write(line_prefix)
            if line_counter:
                sys.stdout.write(current_line)
                sys.stdout.flush()
            line_counter += 1
            current_line = ''

Paramiko Expect 模块

#
# Paramiko Expect
#
# Written by Fotis Gimian
# http://github.com/fgimian
#
# This library works with a Paramiko SSH channel to provide native SSH
# expect-like handling for servers.  The library may be used to interact
# with commands like 'configure' or Cisco IOS devices or with interactive
# Unix scripts or commands.
#
# You must have Paramiko installed in order to use this library.
#
import sys
import re
import socket
# Windows does not have termios
try:
    import termios
    import tty
    has_termios = True
except ImportError:
    import threading
    has_termios = False

import select


class SSHClientInteraction:
    """This class allows an expect-like interface to Paramiko which allows
    coders to interact with applications and the shell of the connected
    device.
    """

    def __init__(self, client, timeout=60, newline='\r', buffer_size=1024,
                 display=False):
        """The constructor for our SSHClientInteraction class.

        Arguments:
        client -- A Paramiko SSHClient object

        Keyword arguments:
        timeout -- THe connection timeout in seconds
        newline -- The newline character to send after each command
        buffer_size -- The amount of data (in bytes) that will be read at a
                       time after a command is run
        display -- Whether or not the output should be displayed in real-time
                   as it is being performed (especially useful when debugging)

        """
        self.channel = client.invoke_shell()
        self.newline = newline
        self.buffer_size = buffer_size
        self.display = display
        self.timeout = timeout
        self.current_output = ''
        self.current_output_clean = ''
        self.current_send_string = ''
        self.last_match = ''

    def __del__(self):
        """The destructor for our SSHClientInteraction class."""
        self.close()

    def close(self):
        """Attempts to close the channel for clean completion."""
        try:
            self.channel.close()
        except:
            pass

    def expect(self, re_strings=''):
        """This function takes in a regular expression (or regular expressions)
        that represent the last line of output from the server.  The function
        waits for one or more of the terms to be matched.  The regexes are
        matched using expression \n<regex>$ so you'll need to provide an
        easygoing regex such as '.*server.*' if you wish to have a fuzzy match.

        Keyword arguments:
        re_strings -- Either a regex string or list of regex strings that
                      we should expect.  If this is not specified, then
                      EOF is expected (i.e. the shell is completely closed
                      after the exit command is issued)

        Returns:
        - EOF: Returns -1
        - Regex String: When matched, returns 0
        - List of Regex Strings: Returns the index of the matched string as
                                 an integer
        """

        # Set the channel timeout
        self.channel.settimeout(self.timeout)

        # Create an empty output buffer
        self.current_output = ''

        # This function needs all regular expressions to be in the form of a
        # list, so if the user provided a string, let's convert it to a 1
        # item list.
        if len(re_strings) != 0 and isinstance(re_strings, str):
            re_strings = [re_strings]

        # Loop until one of the expressions is matched or loop forever if
        # nothing is expected (usually used for exit)
        while (
            len(re_strings) == 0 or
            not [re_string
                 for re_string in re_strings
                 if re.match('.*\n' + re_string + '$',
                             self.current_output, re.DOTALL)]
        ):

            # Read some of the output
            buffer = self.channel.recv(self.buffer_size)

            # If we have an empty buffer, then the SSH session has been closed
            if len(buffer) == 0:
                break

            # Strip all ugly \r (Ctrl-M making) characters from the current
            # read
            buffer = buffer.replace('\r', '')

            # Display the current buffer in realtime if requested to do so
            # (good for debugging purposes)
            if self.display:
                sys.stdout.write(buffer)
                sys.stdout.flush()

            # Add the currently read buffer to the output
            self.current_output += buffer

        # Grab the first pattern that was matched
        if len(re_strings) != 0:
            found_pattern = [(re_index, re_string)
                             for re_index, re_string in enumerate(re_strings)
                             if re.match('.*\n' + re_string + '$',
                                         self.current_output, re.DOTALL)]

        self.current_output_clean = self.current_output

        # Clean the output up by removing the sent command
        if len(self.current_send_string) != 0:
            self.current_output_clean = (
                self.current_output_clean.replace(
                    self.current_send_string + '\n', ''))

        # Reset the current send string to ensure that multiple expect calls
        # don't result in bad output cleaning
        self.current_send_string = ''

        # Clean the output up by removing the expect output from the end if
        # requested and save the details of the matched pattern
        if len(re_strings) != 0:
            self.current_output_clean = (
                re.sub(found_pattern[0][1] + '$', '',
                       self.current_output_clean))
            self.last_match = found_pattern[0][1]
            return found_pattern[0][0]
        else:
            # We would socket timeout before getting here, but for good
            # measure, let's send back a -1
            return -1

    def send(self, send_string):
        """Saves and sends the send string provided"""
        self.current_send_string = send_string
        self.channel.send(send_string + self.newline)

    def tail(self, line_prefix=None):
        """This function takes control of an SSH channel and displays line
        by line of output as \n is recieved.  This function is specifically
        made for tail-like commands.

        Keyword arguments:
        line_prefix -- Text to append to the left of each line of output.
                       This is especially useful if you are using my
                       MultiSSH class to run tail commands over multiple
                       servers.

        """

        # Set the channel timeout to the maximum integer the server allows,
        # setting this to None breaks the KeyboardInterrupt exception and
        # won't allow us to Ctrl+C out of teh script
        self.channel.settimeout(sys.maxint)

        # Create an empty line buffer and a line counter
        current_line = ''
        line_counter = 0

        # Loop forever, Ctrl+C (KeyboardInterrupt) is used to break the tail
        while True:

            # Read the output one byte at a time so we can detect \n correctly
            buffer = self.channel.recv(1)

            # If we have an empty buffer, then the SSH session has been closed
            if len(buffer) == 0:
                break

            # Strip all ugly \r (Ctrl-M making) characters from the current
            # read
            buffer = buffer.replace('\r', '')

            # Add the currently read buffer to the current line output
            current_line += buffer

            # Display the last read line in realtime when we reach a \n
            # character
            if current_line.endswith('\n'):
                if line_counter and line_prefix:
                    sys.stdout.write(line_prefix)
                if line_counter:
                    sys.stdout.write(current_line)
                    sys.stdout.flush()
                line_counter += 1
                current_line = ''

    def take_control(self):
        """This function is a better documented and touched up version of the
        posix_shell function found in the interactive.py demo script that
        ships with Paramiko"""

        if has_termios:
            # Get attributes of the shell you were in before going to the
            # new one
            original_tty = termios.tcgetattr(sys.stdin)
            try:
                tty.setraw(sys.stdin.fileno())
                tty.setcbreak(sys.stdin.fileno())

                # We must set the timeout to 0 so that we can bypass times when
                # there is no available text to receive
                self.channel.settimeout(0)

                # Loop forever until the user exits (i.e. read buffer is empty)
                while True:
                    select_read, select_write, select_exception = (
                        select.select([self.channel, sys.stdin], [], []))
                    # Read any output from the terminal and print it to the
                    # screen.  With timeout set to 0, we just can ignore times
                    # when there's nothing to receive.
                    if self.channel in select_read:
                        try:
                            buffer = self.channel.recv(self.buffer_size)
                            if len(buffer) == 0:
                                break
                            sys.stdout.write(buffer)
                            sys.stdout.flush()
                        except socket.timeout:
                            pass
                    # Send any keyboard input to the terminal one byte at a
                    # time
                    if sys.stdin in select_read:
                        buffer = sys.stdin.read(1)
                        if len(buffer) == 0:
                            break
                        self.channel.send(buffer)
            finally:
                # Restore the attributes of the shell you were in
                termios.tcsetattr(sys.stdin, termios.TCSADRAIN, original_tty)
        else:
            def writeall(sock):
                while True:
                    buffer = sock.recv(self.buffer_size)
                    if len(buffer) == 0:
                        break
                    sys.stdout.write(buffer)
                    sys.stdout.flush()

            writer = threading.Thread(target=writeall, args=(self.channel,))
            writer.start()

            try:
                while True:
                    buffer = sys.stdin.read(1)
                    if len(buffer) == 0:
                        break
                    self.channel.send(buffer)
            # User has hit Ctrl+Z or F6
            except EOFError:
                pass

【问题讨论】:

  • 为什么不直接遍历scping /var/log/log 到本地主机并在这里分析呢?您可以使用diffutils 查找新部件。

标签: python logging ssh paramiko tail


【解决方案1】:

我是 paramiko-expect 的作者。

我刚刚在我的模块的 0.2 中实现了一个新功能,它允许您指定对 tail 方法的回调,以便您可以随意处理当前行。您可以使用它来 grep 输出或在显示之前进一步处理它。预计回调函数将在完成对当前行的操作后返回要发送到 sys.stdout.write 的字符串。

这是一个例子:

import traceback
import paramiko
from paramikoe import SSHClientInteraction


def process_tail(line_prefix, current_line):
    if current_line.startswith('hello'):
        return current_line
    else:
        return ''


def main():

    # Set login credentials and the server prompt
    hostname = 'localhost'
    username = 'fots'
    password = 'password123'
    port = 22

    # Use SSH client to login
    try:

        # Create a new SSH client object
        client = paramiko.SSHClient()

        # Set SSH key parameters to auto accept unknown hosts
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

        # Connect to the host
        client.connect(hostname, port, username, password)

        # Create a client interaction class which will interact with the host
        interact = SSHClientInteraction(client, timeout=10, display=False)

        # Send the tail command
        interact.send('tail -f /home/fots/something.log')

        # Now let the class tail the file for us
        interact.tail(line_prefix=hostname+': ', callback=process_tail)

    except KeyboardInterrupt:
        print 'Ctrl+C interruption detected, stopping tail'
    except Exception:
        traceback.print_exc()
    finally:
        try:
            client.close()
        except:
            pass

if __name__ == '__main__':
    main()

【讨论】:

  • 不错!以编程方式停止 tail 的正确方法是什么?在回调中引发异常?
  • @monkut 好问题。目前,唯一的方法是引发异常以中断 tail 函数中的 while 循环。但是,如果您这样做,您需要确保关闭连接。我认为我可以实现一些更优雅的东西来做到这一点,如果你发现引发异常不够好,欢迎你在github.com/fgimian/paramiko-expect/issues 创建一个问题:)
  • P.S.:很明显,当我编写 paramiko expect 时,tail 函数有一个非常具体的用例,并且所有内容最终都写入了 stdout。但是,如果你们希望找到某种方法来避免这种情况发生,以便您可以在一个或多个远程服务器上实时处理文件的输出,请提出问题。我认为这将是一个非常好的主意,只是我开始编写模块时所做的并不是这样。
  • 我昨天在玩它,并进行了一些更新以尝试让.tail() 与 python3 一起工作。我已经打开了一个拉取请求,但可以随意拒绝它。我认为它可以更清洁,我需要在这里重新考虑我的用例。我正在寻找使用 tail() 或其变体来允许进度更新。所以我想我想要一些“尾直到”的效果,从某种意义上说,行将返回到回调,直到达到预期值。
【解决方案2】:

因此,这个问题在 3 个不同的网站上已经持续了将近 24 小时,并且没有一个与此相关的真实答案。我有点震惊。我尝试将脚本作为子进程运行,然后将输出通过管道传输到标准输出。成功了:

import subprocess
proc = subprocess.Popen(['python','tail_try.py'],stdout=subprocess.PIPE)
for line in iter(proc.stdout.readline, ''):
        print line

我现在可以控制每行打印proc.stdout.readline 的输出。

真是一个简单的答案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-19
    相关资源
    最近更新 更多