-
Notifications
You must be signed in to change notification settings - Fork 0
/
remote7.py
30 lines (26 loc) · 976 Bytes
/
remote7.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import argparse
import subprocess
import sys
parser = argparse.ArgumentParser()
parser.add_argument("host", help="provide host name")
parser.add_argument("command", help="provide command")
args = parser.parse_args()
class RemoteCommand(object):
def __init__(self, host, command):
self.host = host
self.command = command
def exec_cmd(self):
print "Executing command {0}".format(self.command),
print "on host {0}".format(self.host)
ssh = subprocess.Popen(["ssh", "%s" % self.host, self.command],
shell=False,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
result = ssh.stdout.readlines()
if result == []:
error = ssh.stderr.readlines()
print "ERROR: {0}".format(error)
else:
print "OUTPUT: {0}".format(result)
r = RemoteCommand(args.host , args.command)
r.exec_cmd()