|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
import optparse
|
|
|
|
import os
|
|
|
|
import shlex
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
|
|
HOST_KEY_ASK_MSG = """
|
|
|
|
The authenticity of host '[91.121.165.123]:9122 ([91.121.165.123]:9122)' can't be established.
|
|
|
|
ECDSA key fingerprint is SHA256:JvfPvazZzQ9/CUdKN7tiYlNZtDRdEgDsYVIzOgPrsR4.
|
|
|
|
Are you sure you want to continue connecting (yes/no)?
|
|
|
|
""".strip('\n')
|
|
|
|
|
|
|
|
HOST_KEY_STRICT_MSG = """Host key verification failed.\n"""
|
|
|
|
|
|
|
|
|
|
|
|
def tty(msg):
|
|
|
|
fp = open('/dev/tty', 'wb', 0)
|
|
|
|
fp.write(msg.encode())
|
|
|
|
fp.close()
|
|
|
|
|
|
|
|
|
|
|
|
def stderr(msg):
|
|
|
|
fp = open('/dev/stderr', 'wb', 0)
|
|
|
|
fp.write(msg.encode())
|
|
|
|
fp.close()
|
|
|
|
|
|
|
|
|
|
|
|
def confirm(msg):
|
|
|
|
tty(msg)
|
|
|
|
fp = open('/dev/tty', 'rb', 0)
|
|
|
|
try:
|
|
|
|
return fp.readline().decode()
|
|
|
|
finally:
|
|
|
|
fp.close()
|
|
|
|
|
|
|
|
|
|
|
|
if os.getenv('FAKESSH_MODE') == 'ask':
|
|
|
|
assert 'y\n' == confirm(HOST_KEY_ASK_MSG)
|
|
|
|
|
|
|
|
if os.getenv('FAKESSH_MODE') == 'strict':
|
|
|
|
stderr(HOST_KEY_STRICT_MSG)
|
|
|
|
sys.exit(255)
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# Set an env var if stderr was a TTY to make ssh_test tests easier to write.
|
|
|
|
#
|
|
|
|
if os.isatty(2):
|
|
|
|
os.environ['STDERR_WAS_TTY'] = '1'
|
|
|
|
|
|
|
|
|
|
|
|
parser = optparse.OptionParser()
|
|
|
|
parser.add_option('--user', '-l', action='store')
|
tests: Fix no such option -o running FakeSsh.test_okay()
Full output of failed test
```
ERROR: test_okay (__main__.FakeSshTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "tests/ssh_test.py", line 16, in test_okay
ssh_path=testlib.data_path('fakessh.py'),
File "/home/alex/src/mitogen/mitogen/master.py", line 650, in ssh
return self.connect('ssh', **kwargs)
File "/home/alex/src/mitogen/mitogen/parent.py", line 463, in connect
return self._connect(context_id, klass, name=name, **kwargs)
File "/home/alex/src/mitogen/mitogen/parent.py", line 449, in _connect
stream.connect()
File "/home/alex/src/mitogen/mitogen/ssh.py", line 104, in connect
super(Stream, self).connect()
File "/home/alex/src/mitogen/mitogen/parent.py", line 395, in connect
self._connect_bootstrap()
File "/home/alex/src/mitogen/mitogen/ssh.py", line 116, in
_connect_bootstrap
time.time() + 10.0):
File "/home/alex/src/mitogen/mitogen/parent.py", line 207, in
iter_read
(''.join(bits)[-300:],)
mitogen.core.StreamError: EOF on stream; last 300 bytes received:
'Usage: fakessh.py [options]\n\nfakessh.py: error: no such option: -o\n'
```
7 years ago
|
|
|
parser.add_option('-o', dest='options', action='append')
|
|
|
|
parser.disable_interspersed_args()
|
|
|
|
|
|
|
|
opts, args = parser.parse_args(sys.argv[1:])
|
|
|
|
args.pop(0) # hostname
|
|
|
|
|
|
|
|
# On Linux the TTY layer appears to begin tearing down a PTY after the last FD
|
|
|
|
# for it is closed, causing SIGHUP to be sent to its foreground group. Since
|
|
|
|
# the bootstrap overwrites the last such fd (stderr), we can't just exec it
|
|
|
|
# directly, we must hold it open just like real SSH would. So use
|
|
|
|
# subprocess.call() rather than os.execve() here.
|
|
|
|
args = [''.join(shlex.split(s)) for s in args]
|
|
|
|
sys.exit(subprocess.call(args))
|