1

I was trying to automate a system where we have a linux box but customized.. to go to shell we have to pass some input. like below:

tanuj$ ssh [email protected]
Used by Tanuj

Password: 

command line interface

app > en
app # config t
app (config) # 

I have written a script using pexpect in python. I am able to login and execute the command

pexpect.sendline("ls -lrt")
pexpect.expect("#")

but when i am using pexpect.before() .. is getting nothing .. when the command output is long and also i could see the pexpect.before has also got the command in it.

any idea how to solve this.. or is there any other python module which i can use to automation a ssh session like i have here.

i also tried using paramiko but it did not work because we have a to execute some commands before we can reach to normal shell prompt.

2
  • Could there be a # in the output somewhere before the next prompt? Commented Feb 14, 2014 at 18:52
  • I checked that too .. but its not the case here. Commented Feb 19, 2014 at 8:39

1 Answer 1

1

I am also facing the similar problem. I was about to ask the question. You are using a # sign in your

pexpect.expect('#')

This # sign comments everything written after it. Further I guess you should create a child process to spawn a process, Like(I don't know if I'm right in your situation):

child=pexpect.spawn('ssh [email protected]')
child.expect('prompt_expected_by_you')     # put in the prompt you expect after sending SSH
child.sendline('your_password')
child.expect('Prompt_expected_by_you')
child.sendline('ls -ltr')
child.expect('Prompt_expected_by_you')
print child.before,            # , will keep continue your newline print
print child.after
child.sendline('exit')
child.expect('Prompt_expected_by_you')
child.sendline('exit')
child.expect('Prompt_expected_by_you')   # may not be required
child.close                              # close the child session

I have successfully used these commands in FTP, but not able to print result of 'LS -LTR' in SSH. I guess i'll have to initiate a shell, but not sure. Any progress on your side?

Could someone help???

Sign up to request clarification or add additional context in comments.

1 Comment

# doesn't start a comment if it appears in a string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.