Ask Your Question
4

How can a Python script be initiated on a distant computer using SSH from within Python?

asked 2021-05-19 11:00:00 +0000

nofretete gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-06-01 07:00:00 +0000

david gravatar image

To initiate a Python script on a distant computer using SSH from within Python, you can use the paramiko library which allows to execute commands remotely through an SSH connection.

Here is an example code snippet:

import paramiko

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

# replace hostname, username and password with correct values
ssh.connect('hostname', username='username', password='password')

# replace script_path with the path to the python script on the remote computer
stdin, stdout, stderr = ssh.exec_command('python script_path')

# print the output of the script
print(stdout.readlines())

ssh.close()

This code will create an SSH connection to the remote computer, execute the specified Python script and print its output. Make sure to replace the placeholders with the correct values (hostname, username, password, script_path).

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-05-19 11:00:00 +0000

Seen: 10 times

Last updated: Jun 01 '21