简体   繁体   中英

Pexpect: Not able to redirect outputs to a file

Problem: Outputs from the device not getting redirected to a file. Program: According to my program I have logged into one device and trying to collect few outputs.Here is the program.

     #!/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')

Please help!

Note: I am new here.

I am using Python 2.7.6 (default, Oct 26 2016, 20:30:19) [GCC 4.8.4] on linux2 Type "help", "copyright", "credits" or "license" for more information.

I fixed it myself. Using a+ in open function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM