只是为了回答这个问题。功劳归于
Thomas K
。有关详细信息,请参阅他在问题下的评论。
[STEP 101] # cat foo.py
#!/usr/bin/env python3
import pexpect, sys
spawn = pexpect.spawnu if sys.version_info[0] >= 3 else pexpect.spawn
ssh = spawn('ssh -t foo@localhost bash --noprofile --norc')
ssh.logfile_read = sys.stdout
ssh.expect('assword:')
ssh.sendline('123456')
ssh.expect('bash-[.0-9]+[$#]')
ssh.sendline('exit')
ssh.expect(pexpect.EOF)
ssh.wait()
[STEP 102] #
[STEP 103] # python2 foo.py
foo@localhost's password:
bash-5.1$ exit
exit
Connection to localhost closed.
[STEP 104] #
[STEP 105] # python3 foo.py
foo@localhost's password:
bash-5.1$ exit
exit
Connection to localhost closed.
[STEP 106] #