Reputation: 318
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.
Upvotes: 1
Views: 6170
Reputation: 153
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?
Upvotes: 1