mchuca's picture
Server Monitor: six editions (C++/Qt, Python, web for Linux and Windows), requirements and developer kit
bad179a
Raw History Blame Contribute Delete
1.2 kB
#!/usr/bin/env python3
"""Review stand-in for ssh: logs argv and the askpass environment as JSON lines to $FAKESSH_LOG, asks the askpass helper for the
password like ssh would, then runs the remote command line locally with stdin/stdout passed through (the "server" is this machine)."""
import json, os, subprocess, sys
env = {k: os.environ.get(k) for k in ('SSH_ASKPASS', 'SSH_ASKPASS_REQUIRE', 'SERVERMONITOR_ASKPASS', 'DISPLAY')}
answer = mode = None
if env['SSH_ASKPASS']:
mode = oct(os.stat(env['SSH_ASKPASS']).st_mode & 0o777) + ' in ' + oct(os.stat(os.path.dirname(env['SSH_ASKPASS'])).st_mode & 0o777)
if env['SSH_ASKPASS'].endswith('.sh'): answer = subprocess.run([env['SSH_ASKPASS'], "me@fakehost's password: "], capture_output=True, text=True).stdout
open(os.environ['FAKESSH_LOG'], 'a').write(json.dumps({'argv': sys.argv[1:], 'env': env, 'askpass_answer': answer, 'askpass_mode': mode, 'askpass_text': open(env['SSH_ASKPASS'], newline='').read() if env['SSH_ASKPASS'] else None}) + '\n')
if 'deadhost' in ' '.join(sys.argv): sys.stderr.write('ssh: connect to host deadhost port 22: Connection refused\n'); sys.exit(255)
os.execvp('/bin/sh', ['/bin/sh', '-c', sys.argv[-1]])