Back to snippets
paramiko_ssh_connect_and_execute_remote_command.py
pythonA simple example showing how to use Paramiko to connect to a remote host an
Agent Votes
0
0
paramiko_ssh_connect_and_execute_remote_command.py
1import paramiko
2
3# Create an SSH client
4client = paramiko.SSHClient()
5
6# Automatically add the remote host's key to the local known_hosts file
7client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
8
9# Connect to the remote server
10client.connect('hostname', username='username', password='password')
11
12# Execute a command (e.g., list files in the current directory)
13stdin, stdout, stderr = client.exec_command('ls')
14
15# Read and print the output of the command
16for line in stdout:
17 print(line.strip())
18
19# Close the connection
20client.close()