Hi all.

This is my first post and i am bit new to python. Pls dont mind if my question is dumb, kindly guide me

I am writing a pexpect script (i am using linux Centos 5.5) to access my cisco router and want to record the output of "show version" command

This is my script

import pexpect
import sys

child = pexpect.spawn("ssh homeuser@192.168.15.5")

child.expect("Password:")
child.sendline("abc123")
child.expect("HomeR1>")
child.sendline("show version")


Now i can see on my router that i have successfully logged in. But i dont know how to catch the output and save it to a file. I saw several examples like child.logfile = sys.stdout etc but i really dont know how to get it working.

Pls if someone has experience with pexpect, kindly help me out.

Note: I have already cached the ssh keys, so to avoid complexity.

Thanks in advance

The pexpect documents (here) say

send(self, s)
This sends a string to the child process. This returns the number of
bytes written. If a log file was set then the data is also written to
the log.

So it appears you can set a logfile... and I'd expect that the logfile can be anything with a 'write' method, so easy enough to use, for instance StringIO. This is all supposition on my part: I've never used pexpect.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.