Reputation: 133
Hi I am using pexpect in Python to read ssh device information.
expObject = pexpect.spawn('/usr/bin/ssh %s@%s' % (username, device))
expObject.sendline(password)
After giving the password I have show some device information and in command prompt it will ask press any key to continue; Once I press any key the info disappears.
I use below logic to capture other data coming after giving command like show version
expObject.expect(CLI_PROMPT)
data = expObject.before
So how do I capture the data which is displayed after giving password and before pressing any key to comtinue using "expObject".
Upvotes: 0
Views: 1145
Reputation: 6408
I had a similar problem where I needed the handle the text output line by line. To get this to work, you have to be aware that pexpect configures regexp such that .* pattern includes linefeeds, so instead of .* you have to use [^\n]* . Something like this should work in your situation:
child = pexpect.spawn('ssh command goes here')
child.expect('password prompt text\r\n')
child.sendline(password)
data = ""
while True:
i = child.expect(['press any key to continue', '[^\n]*\r\n'])
if i == 0:
break
data += child.before
print data
This should work with a command that outputs the following:
password propt text
<start of data captured> - 1st line
a second line
a third line
last line <end of data that will be captured>
press any key to continue
Upvotes: 2
Reputation: 4154
http://ubuntuforums.org/showthread.php?t=220139
Is an excellent guide for exactly what you want to do. I suspect you do not actually need to use expect and can accomplish everything you want with just ssh command execution and ssh keys. For example:
hostA:~ jdizzle$ ssh hostB hostname
hostB
Here is another tutorial on ssh keys: http://pkeck.myweb.uga.edu/ssh/
Upvotes: 1