Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pexpect:无法写入日志文件_Python_Pexpect_Logfile - Fatal编程技术网

Python Pexpect:无法写入日志文件

Python Pexpect:无法写入日志文件,python,pexpect,logfile,Python,Pexpect,Logfile,无法将pexpect.spawn实例的输入/输出记录到日志文件和sys.stdout中。 我正在编写一个函数“connect()”来生成到linux服务器的ssh连接。 从另一个函数get_build()调用connect()函数。在创建bld_服务器_套接字实例之后,我尝试将子对象的o/p记录到文件和标准o/p中。我在标准件和文件上都没有看到任何o/p def connect(dev_ip, user, password): child = pexpect.spawn('ssh {}@

无法将pexpect.spawn实例的输入/输出记录到日志文件和sys.stdout中。 我正在编写一个函数“connect()”来生成到linux服务器的ssh连接。 从另一个函数get_build()调用connect()函数。在创建bld_服务器_套接字实例之后,我尝试将子对象的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')
def connect(开发ip、用户、密码):
child=pexpect.spawn('ssh{}@{}'。格式(user,self.dev_ip))
尝试=0
当尝试<5时:
i=child.expect(['[pP]assword:','\(yes/no\)',“>”,“[\$]”,pexpect.EOF,pexpect.TIMEOUT])
如果i==0:
child.sendline(密码)
如果i==1:
child.sendline('yes')
如果i==2:
sendline('enable')
如果i==3:
child.sendline(“\n”)
打破
尝试次数+=1
打印类型(子项)
返回儿童
def get_build(版本、构建服务器ip、mod):
bld\u服务器\u套接字=连接(生成\u服务器\u ip,'根','密码')
file=open('sshlog.txt','a')
bld_server_socket.logfile=文件
bld_server_socket.logfile=sys.stdout
bld_服务器_套接字。预期(“$”)
bld_服务器_套接字.sendline('ls'))

connect()
中是否应该有
spawn
?噢,是的。Connect()已生成。对不起,忘了在描述中包括类似的内容。child=pexpect.spawn('ssh{}@{}.format(user,self.dev_ip))是否应该在
connect()
中有
spawn
。Connect()已生成。对不起,忘了在描述中包括类似的内容。child=pexpect.spawn('ssh{}@{}'。格式(user,self.dev_ip))