Pexpect ssh wrapper gracious quit and silent command -
Pexpect ssh wrapper gracious quit and silent command -
i want wrap interactive ssh, automatizing login phase:
child = pexpect.spawn('ssh ...') child.expect('pass prompt: ') child.sendline(password) child.expect('shell prompt> ') child.sendline('cd /some/where') child.interact()
this works. however, when user quits shell (not using expect's command char), exception (oserror). solution is:
try: child.interact() except oserror err: if err.errno == 5 , not child.isalive(): # print 'child exited' pass else: raise
is there cleaner solution ?
also, i'd create "cd /some/where" not echoed user. tried with:
child.setecho(false) child.sendline('cd /some/where') child.setecho(true)
but command still echoed. there more right way do, or bug in setecho ?
the best way utilize pxssh handles need when spawning ssh process
see code below:
import pxssh import getpass def connect(): try: s = pxssh.pxssh() s.logfile = open('/tmp/logfile.txt', "w") hostname = raw_input('hostname: ') username = raw_input('username: ') password = getpass.getpass('password: ') s.login(hostname, username, password) s.sendline('ls -l') # run command s.prompt() # match prompt print(s.before) # print before prompt. s.logout() except pxssh.exceptionpxssh e: print("pxssh failed on login.") print(e) if __name__ == '__main__': connect()
pexpect
Comments
Post a Comment