1

Unable to log input/output of a pexpect.spawn instance to log file and sys.stdout. I am writing a function "connect()" to spawn ssh connection to a linux server. From another function get_build(), I am calling connect() function. After creating bld_server_socket instance, I am trying to log the o/p of the child to file and standard o/p. I do not see any o/p on the stdout nor the file.

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 Answers0