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