在我的下面的程序中,输出被重定向到文件test1.txt,但是当我打开文件时,我有三个问题:
我基本上希望它与另一个文件进行比较,因此如果选项卡的数量不同,它将失败.
telconn=pexpect.spawn('telnet 10.24.12.109')
telconn.logfile = sys.stdout
telconn.expect(":")
telconn.send("user" + "\r")
telconn.expect(":")
telconn.send("pass" + "\r\r\r\r\n\n\n")
telconn.expect("key to proceed.")
telconn.send ("\003")
telconn.expect("root>")
prev_std= sys.stdout
sys.stdout=open("test1.txt","w")
print "Telnet connection is done"
telconn.sendline('\n');
telconn.expect (['>',pexpect.EOF])
ls = telconn.before
telconn.sendline('ls -al');
telconn.expect (['>',pexpect.EOF])
ls = telconn.before
telconn.sendline('pwd');
telconn.expect (['>',pexpect.EOF])
pwd = telconn.before
telconn.sendline('noscli');
telconn.expect (['#',pexpect.EOF])
nos = telconn.before
telconn.sendline('terminal length 0');
telconn.expect (['#',pexpect.EOF])
term = telconn.before
telconn.sendline('\n\n');
telconn .sendline('exit');
telconn.close()
print ls
print pwd
print nos
print term
#print "Ended session" …Run Code Online (Sandbox Code Playgroud) 我正在将我的代码迁移到python 3.4.3.这段代码在python 2.4.3中工作正常.但这里它在python 3.4.3中抛出错误.我应该使用与预期不同的东西吗?这是我的代码片段,它会收到错误:
telconn=pexpect.spawn('telnet 10.24.12.83')
telconn.logfile = sys.stdout
login=telconn.expect([":","key to proceed.",">"])
if login==0:
telconn.send("user1" + "\r")
telconn.expect(":")
telconn.send("paswd1" + "\r\r\r\r\n\n\n")
login1=telconn.expect([">","key to proceed."])
if login1==0:
print("nothing")
elif login1==1:
telconn.expect("key to proceed.")
telconn.send ("\003")
telconn.expect(">")
if login==1:
telconn.send ("\003")
telconn.expect(">")
print("ctlc")
elif login==2:
telconn.send("\n\r")
telconn.expect(">")
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:
Traceback (most recent call last):
File "cleanup1.py", line 128, in <module>
Connect()
File "cleanup1.py", line 53, in Connect
login=telconn.expect([":","key to proceed.",">"])
File "/corp/global/install-dependent/python/3.4.3/lib/python3.4/site-packages/pexpect/spawnbase.py", line 315, in expect
timeout, searchwindowsize, async)
File "/corp/global/install-dependent/python/3.4.3/lib/python3.4/site-packages/pexpect/spawnbase.py", line 339, …Run Code Online (Sandbox Code Playgroud)