Pexpect:无法写入日志文件

时间:2019-09-03 22:48:38

标签: python pexpect logfile

无法将pexpect.spawn实例的输入/输出记录到日志文件和sys.stdout。   我正在编写一个函数“ connect()”来生成与Linux服务器的ssh连接。    从另一个函数get_build()中,我正在调用connect()函数。创建bld_server_socket实例后,我尝试将子代的o / p记录到文件和标准o / p中。我没有在标准输出或文件上看到任何o / p。

def connect(dev_ip, user, password):
    child = pexpect.spawn('ssh {}@{}'.format(user, self.dev_ip))

    tries = 0

    while tries < 5:


        i = child.expect(['[pP]assword:', '\(yes/no\)', ">", "[#$]", pexpect.EOF, pexpect.TIMEOUT])

        if i == 0:
            child.sendline(password)
        if i == 1:
            child.sendline('yes')
        if i == 2:
            child.sendline('enable')
        if i == 3:
            child.sendline('\n')
            break

        tries += 1
    print type(child)

    return child


def get_build(version, build_server_ip, mod):


    bld_server_socket = connect(build_server_ip, 'root', 'password')
    file = open('sshlog.txt', 'a')
    bld_server_socket.logfile = file
    bld_server_socket.logfile = sys.stdout

    bld_server_socket.expect('$')
    bld_server_socket.sendline('ls')

0 个答案:

没有答案