Skip to content

Instantly share code, notes, and snippets.

@mkeneqa
Created July 5, 2019 17:53
Show Gist options
  • Save mkeneqa/b730cb108675b8eaae70d6ae87db37bc to your computer and use it in GitHub Desktop.
Save mkeneqa/b730cb108675b8eaae70d6ae87db37bc to your computer and use it in GitHub Desktop.
A quick python script to send linux server commands from windows,mac or linux over ssh.
import sys, paramiko
# help src: https://stackoverflow.com/a/48826690 , https://gist.github.com/mlafeldt/841944#file-ssh_demo-py
hostname = "10.10.01.01"
password = "pass"
command = "ps aux |grep python"
username = "userme"
port = 22
try:
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.WarningPolicy)
client.connect(hostname, port=port, username=username, password=password)
transport = client.get_transport()
session = transport.open_session()
session.set_combine_stderr(True)
session.get_pty()
session.exec_command(command)
stdin = session.makefile('wb', -1)
stdout = session.makefile('rb', -1)
stdin.write(password +'\n')
stdin.flush
results = stdout.read().decode("utf-8")
print(results)
finally:
client.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment