Issue
I'm trying to configure routers with a python script and wanted to use paramiko expect to do the job. The function change_password is part of a class and contains the following paramiko-expect-lines (with self.ssh_client as an instance of paramiko.SSHClient):
with SSHClientInteraction(self.ssh_client, timeout=5, display=True) as self.interact:
# change password
prompt = '.*root@cb_park:~#.*'
self.interact.expect(prompt)
self.interact.send('passwd')
self.interact.expect(['.*Changing password for root.*', '.*New password:.*'])
self.interact.send('NewPassword')
self.interact.expect('.*Retype password:.*')
self.interact.send('NewPassword')
self.interact.expect(['.*passwd: password for root changed by root.*', prompt])
# modify firstlogin
self.interact.send('uci delete vuci.main.firstlogin')
self.interact.expect(prompt)
self.interact.send('uci commit')
self.interact.expect(prompt)
However, sometimes the send command is executed before the prompt appears. For example in the following scenario:
root@cb_park:~# passwd
Changing password for root
New password:
Retype password:
passwd: password for root changed by root
root@cb_park:~# uci delete vuci.main.firstlogin
uci commit
uci: Entry not found
root@cb_park:~# uci commit
'uci commit' is being executed, even though the expected prompt has not been given out. Sometimes this even happens on the second line and the new password is executed right after 'Changing password for root' even though 'New password:' has not been given out which leads to a timeout right after:
root@cb_park:~# passwd
Changing password for root
NewPassword
New password: EXCESS TIME RECV_READY TIMEOUT, did you expect() before a send()
Retype password: EXCESS TIME RECV_READY TIMEOUT, did you expect() before a send()
Passwords don't match
passwd: password for root is unchanged
root@cb_park:~#
I don't really know what the error could be or how to debug. Any idea?
paramiko 2.8.0
paramiko-expect 0.3.0
Python 3.8.10
Cheers Ian
Solution
in case anybody has a similar problem:
I took out the multiline expects and replaced it by only expecting the last line. I guess I missunderstood the docs and thought I'd have to expect both lines. New code as follows works without timeout.
with SSHClientInteraction(self.ssh_client, timeout=5, display=True) as self.interact:
# change password
prompt = '.*root@cb_park:~#.*'
self.interact.expect(prompt)
self.interact.send('passwd')
self.interact.expect('.*New password:.*') # instead of self.interact.expect(['.*Changing password for root.*', '.*New password:.*'])
self.interact.send('NewPassword')
self.interact.expect('.*Retype password:.*')
self.interact.send('NewPassword')
self.interact.expect(prompt) # instead of self.interact.expect(['.*passwd: password for root changed by root.*', prompt])
# modify firstlogin
self.interact.send('uci delete vuci.main.firstlogin')
self.interact.expect(prompt)
self.interact.send('uci commit')
self.interact.expect(prompt)
Ian
Answered By - Ian_Ious
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.