小编Sum*_*uma的帖子

重定向到文件的标准输出显示与实际不同

在我的下面的程序中,输出被重定向到文件test1.txt,但是当我打开文件时,我有三个问题:

  1. 我看到像ls,pwd这样的命令在提示符下面(sw0:FID128:root>).
  2. 提示符应显示"sw0:FID128:root>",但它显示"sw0:FID128:root"
  3. 如果实际输出有2个选项卡,则该文件仅显示1个选项卡.

我基本上希望它与另一个文件进行比较,因此如果选项卡的数量不同,它将失败.

 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 io shell

5
推荐指数
1
解决办法
110
查看次数

期望在python3中抛出错误为"必须在str中,而不是字节"

我正在将我的代码迁移到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)

python python-3.x

5
推荐指数
2
解决办法
4236
查看次数

标签 统计

python ×2

io ×1

python-3.x ×1

shell ×1