Execute command line on remote machine via ssh. Args: cmd: User-defined command (udf) to execute on the remote host. ip: The ip-address of the host to run the command on. port: Port number that the host is listening on. thread_list: username: Optional. If
(
cmd: str, ip: str, port: int, username: Optional[str] = ""
)
| 96 | |
| 97 | |
| 98 | def execute_remote( |
| 99 | cmd: str, ip: str, port: int, username: Optional[str] = "" |
| 100 | ) -> Thread: |
| 101 | """Execute command line on remote machine via ssh. |
| 102 | |
| 103 | Args: |
| 104 | cmd: User-defined command (udf) to execute on the remote host. |
| 105 | ip: The ip-address of the host to run the command on. |
| 106 | port: Port number that the host is listening on. |
| 107 | thread_list: |
| 108 | username: Optional. If given, this will specify a username to use when issuing commands over SSH. |
| 109 | Useful when your infra requires you to explicitly specify a username to avoid permission issues. |
| 110 | |
| 111 | Returns: |
| 112 | thread: The Thread whose run() is to run the `cmd` on the remote host. Returns when the cmd completes |
| 113 | on the remote host. |
| 114 | """ |
| 115 | ip_prefix = "" |
| 116 | if username: |
| 117 | ip_prefix += "{username}@".format(username=username) |
| 118 | |
| 119 | # Construct ssh command that executes `cmd` on the remote host |
| 120 | ssh_cmd = "ssh -o StrictHostKeyChecking=no -p {port} {ip_prefix}{ip} '{cmd}'".format( |
| 121 | port=str(port), |
| 122 | ip_prefix=ip_prefix, |
| 123 | ip=ip, |
| 124 | cmd=cmd, |
| 125 | ) |
| 126 | |
| 127 | # thread func to run the job |
| 128 | def run(ssh_cmd): |
| 129 | subprocess.check_call(ssh_cmd, shell=True) |
| 130 | |
| 131 | thread = Thread(target=run, args=(ssh_cmd,)) |
| 132 | thread.setDaemon(True) |
| 133 | thread.start() |
| 134 | return thread |
| 135 | |
| 136 | |
| 137 | def get_remote_pids(ip, port, cmd_regex): |
no test coverage detected