Skip to content

Instantly share code, notes, and snippets.

@nabobalis
Created July 8, 2022 00:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nabobalis/4b59ab5a1cc3c502b33fce053f1b1c19 to your computer and use it in GitHub Desktop.
Save nabobalis/4b59ab5a1cc3c502b33fce053f1b1c19 to your computer and use it in GitHub Desktop.
ssh_lmsal
import paramiko
from sshtunnel import SSHTunnelForwarder
with SSHTunnelForwarder(
('out.lmsal.com', 22),
ssh_username='<USERNAME>',
ssh_password="<PASSOWRD>",
ssh_pkey="<PATH_TO_SSH_PRIV_KEY>",
remote_bind_address=("out.lmsal.com", 22),
local_bind_address=('127.0.0.1', 1111)
) as tunnel:
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname=tunnel.local_bind_host, port=tunnel.local_bind_port,
username="<USERNAME>", password="<PASSWORD>")
# do some operations with client session
stdin, stdout, stderr = client.exec_command("echo abc.txt >> output.txt")
print(stdout.channel.recv_exit_status()) # status is 0
client.close()
print('FINISH!')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment