# Copyright (c) 2026 Eric Urban # Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: # The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. # dependencies: # asyncssh==2.22.0 # cffi==2.0.0 # cryptography==46.0.7 # pycparser==3.0 # typing_extensions==4.15.0 import asyncio import asyncssh import random import sys import time import binascii import shlex import posixpath async def run_sudo_remotely(hostname, username, password, cmd_to_run, tmp_path='/dev/shm'): # create randomly named files for the helper rng = random.Random(str(time.time()) + hostname) rand_suffix = str(binascii.hexlify(rng.randbytes(12)), encoding='ascii') tmpfilename_pw = posixpath.join(tmp_path, "sudohelper_%s_pw.sh" % (rand_suffix) ) tmpfilename_sudo = posixpath.join(tmp_path, "sudohelper_%s_sudo.sh" % (rand_suffix)) cmd_to_run = shlex.join(cmd_to_run) # passing known_hosts=None disables host key checking async with asyncssh.connect(hostname, username=username, password=password, known_hosts=None) as conn: async with conn.start_sftp_client() as sftp: # create a script to supply the password async with sftp.open(tmpfilename_pw, 'w', attrs=asyncssh.SFTPAttrs(permissions=0o700)) as fout: await fout.write("#!/bin/sh\n") await fout.write("echo ") await fout.write(shlex.quote(password)) await fout.write("\n") # create a script to run sudo async with sftp.open(tmpfilename_sudo, 'w', attrs=asyncssh.SFTPAttrs(permissions=0o700)) as fout: await fout.write("#!/bin/sh\n") await fout.write("export SUDO_ASKPASS=" + tmpfilename_pw +"\n") await fout.write("exec sudo --askpass $@ ") await fout.write(cmd_to_run) await fout.write("\n") try: # run the script that invokes sudo async with conn.create_process(tmpfilename_sudo, stdin=asyncssh.DEVNULL) as process: stdout, stderr = await process.communicate() exit_code = process.exit_status finally: # remove both scripts [await sftp.unlink(x) for x in (tmpfilename_pw, tmpfilename_sudo,)] return (exit_code, stdout, stderr) async def run_remote_commands(remote_commands): remote_commands = [run_sudo_remotely(*rc) for rc in remote_commands] return (await asyncio.gather(*remote_commands)) remote_commands = [] for line in sys.stdin: line = line.strip() if len(line) == 0 or line[0] == '#': continue line = [x.strip() for x in line.split(',')] hostname = line[0] username = line[1] password = line[2] cmd = line[3:] remote_commands.append((hostname, username, password, cmd)) remote_results = asyncio.run(run_remote_commands(remote_commands)) for result in remote_results: exit_code, stdout, stderr = result sys.stdout.write(stdout) sys.stderr.write(stderr) if exit_code != 0: sys.exit(exit_code)