forked from semk/GitFS
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathssh.py
executable file
·88 lines (73 loc) · 2.44 KB
/
ssh.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python2
# ssh.py -*- python -*-
# Copyright (c) 2013 Ross Biro
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#
"""run commands on remote hosts via ssh and return an ssh object."""
import logging
from subprocess import Popen, PIPE
from sys import argv;
class SSH(object):
def __init__(self, host, command):
logging.debug('ssh %s:%s' %(host, command))
self.host = host
self.proc = None
self.command = command
self.ssh = "ssh"
self.ssh_args = []
def execute(self):
c = [self.ssh] + self.ssh_args + [self.host] + self.command
self.proc = Popen(c, bufsize = -1, stdin = PIPE, stdout=PIPE, stderr=PIPE, close_fds=True, shell=False)
if self.proc.stdin:
self.proc.stdin.close();
self.proc.stdin = None
def stdout(self):
if self.proc is None:
return None
return self.proc.stdout
def stderr(self):
if self.proc is None:
return None
return self.proc.stderr
def wait(self):
if self.proc is None:
return None
return self.proc.wait()
def poll(self):
if self.proc is None:
return None
return self.proc.wait()
def terminate():
if self.proc is None:
return None
return self.proc.terminate();
def kill():
if self.proc is None:
return None
return self.proc.kill()
def displayAndWait(self):
print self.stdout().read()
print self.stderr().read()
def main(host, command):
ssh = SSH(host, [command])
ssh.execute()
print ssh.stdout().read()
if __name__ == "__main__":
#logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)
if len(argv) != 3:
print 'usage %s <host> <command>' % argv[0]
exit(1)
main (argv[1], argv[2])