Last active
May 4, 2026 17:56
-
-
Save met/42bc1801acf76bca9b9049a24aef15ee to your computer and use it in GitHub Desktop.
SSH example with paramiko
This file contains hidden or 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
| # python3 -m venv foldername | |
| # cd foldername | |
| # source ./bin/activate | |
| # pip3 install paramiko | |
| hostname='server.local' | |
| username='root' | |
| port=22 | |
| password='password' | |
| prikaz='pwd ; ls -la' | |
| import paramiko | |
| import socket | |
| print(socket.getaddrinfo(hostname, port, socket.AF_UNSPEC, socket.SOCK_STREAM)) | |
| print() | |
| ssh = paramiko.SSHClient() | |
| ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # trust unknown hosts, only for development, not for production! | |
| ssh.connect( | |
| hostname=hostname, | |
| port=port, | |
| username=username, | |
| password=password, | |
| timeout=10, | |
| banner_timeout=10, | |
| auth_timeout=10) | |
| _, stdout, stderr = ssh.exec_command(cmd, timeout=10) | |
| print('STDOUT:') | |
| print(stdout.read().decode()) | |
| print() | |
| print('STDERR:') | |
| print(stderr.read().decode()) | |
| ssh.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment