Last active
February 13, 2021 07:56
-
-
Save eduardofcgo/befdcdf10ca6c4bf310fa4e97b7b380b to your computer and use it in GitHub Desktop.
Run local script on remote machine with SSH in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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