Skip to content

Instantly share code, notes, and snippets.

@oiuww09fn
Last active November 9, 2015 06:53
Show Gist options
  • Save oiuww09fn/ac74376d3335b50771b2 to your computer and use it in GitHub Desktop.
Save oiuww09fn/ac74376d3335b50771b2 to your computer and use it in GitHub Desktop.
Running command on remote machine.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from paramiko import SSHClient, AutoAddPolicy
class RemoteCMD(object):
def __init__(self):
self.client = None
def ssh_connect(self, server=None, username="", password=""):
if server is None:
raise RuntimeError("Server could not be None")
self.client = SSHClient()
self.client.set_missing_host_key_policy(AutoAddPolicy())
self.client.connect(server, username=username, password=password)
def run_cmd(self, cmd):
""""""
stdin, stdout, stderr = self.client.exec_command(cmd)
return stdout.read()
if __name__ == "__main__":
a = RemoteCMD()
a.ssh_connect("192.168.1.101", "username", "password")
print a.run_cmd("ls /tmp/")
print a.run_cmd("ls /var/")
print a.run_cmd("pwd")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment