【问题标题】:Pexpect: Not able to redirect outputs to a filePexpect:无法将输出重定向到文件
【发布时间】:2017-10-30 20:49:12
【问题描述】:

问题:设备的输出未重定向到文件。 程序:根据我的程序,我已登录一台设备并尝试收集少量输出。这是程序。

     #!/usr/bin/python
     import pexpect
     import time
     import sys


     IP_Address= '10.197.225.226'
     Username = 'hey'
     Password= 'hey'

     session = pexpect.spawn("telnet " + IP_Address)
     result= session.expect(["Username:"])

     if result!=0:
      print("-----Failure connection")
      exit()
     session.sendline(Username)
     result= session.expect(["Password:"])
     session.sendline(Password)


     result= session.expect(["#"])
     print('success')

     C2=input('How many commands you want to enter:')
     for i in range (0,C2):
       C1=raw_input('Enter the commands:' )
       session.expect('#')
       session.sendline(C1)
       session.logfile= open("Result1.txt","w")

     print('Done')

请帮忙!

注意:我是新来的。

我正在使用 Python 2.7.6(默认,2016 年 10 月 26 日,20:30:19) [GCC 4.8.4] 在 linux2 上 输入“help”、“copyright”、“credits”或“license”了解更多信息。

【问题讨论】:

    标签: python python-2.7


    【解决方案1】:

    我自己修好了。 在 open 函数中使用 a+。

    【讨论】:

    • 能否请您提供您的解决方案,以便其他有相同问题的人可以轻松理解...
    猜你喜欢
    • 1970-01-01
    • 2015-10-30
    • 2011-06-04
    • 1970-01-01
    • 2013-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-19
    相关资源
    最近更新 更多