#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    25
    Rep Power
    0

    Problem with Pexpect and the for loop.


    I created a script that will ssh into cisco device, input like 20 command, will grab the output and write the output into a file. What I was ancount is that each command output is suppose to have a small block on the file with a title, but for some reason the output on the file are all mess up. My script was like this:

    Code:
    for x in list_commands:
                child.logfile_read = f_write
                f_write.write(Command '+x+'\n'+'\n'+'\n'+'\n'+'\n'+'\n')
                print "Sending command " " " + x
                child.sendline(x)
                child.expect('#')
                results = child.before
                print "#########################"
                print ""
                print ""
                print ""
                print results
    I wonder what I'm doing wrong? Some of the command output are show ok, but not the long output. Any sugestion?
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,714
    Rep Power
    480
    I've saved your code as file r.py and try to run it in python2 and in python3. It works not.
    Code:
    $ python r.py
      File "r.py", line 3
        f_write.write(Command '+x+'\n'+'\n'+'\n'+'\n'+'\n'+'\n')
                                  ^
    SyntaxError: invalid syntax
    $ python3 r.py
      File "r.py", line 3
        f_write.write(Command '+x+'\n'+'\n'+'\n'+'\n'+'\n'+'\n')
                                  ^
    SyntaxError: invalid syntax
    But seriously, what is f_write? What is Command? And what confidence have I that any of the commands in list_commands are valid? What is child? I think whomever can help you will need more information please.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo