Skip to content

Instantly share code, notes, and snippets.

@eduardofcgo
Last active February 13, 2021 07:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eduardofcgo/befdcdf10ca6c4bf310fa4e97b7b380b to your computer and use it in GitHub Desktop.
Save eduardofcgo/befdcdf10ca6c4bf310fa4e97b7b380b to your computer and use it in GitHub Desktop.
Run local script on remote machine with SSH in python
import pexpect
async def _run_script_ssh(user, host, password, script_path, port=22, args=None):
skip_host_key_check_args = (
"-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"
)
ssh_command = f'ssh {user}@{host} -p {port} {skip_host_key_check_args} "bash -s" -- < {script_path} {args or ""}'
child = pexpect.spawn("/bin/bash", ["-c", ssh_command], encoding="utf-8")
await child.expect("password:", async_=True)
child.sendline(password)
await child.expect(pexpect.EOF, async_=True)
return child.before
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment