-
Notifications
You must be signed in to change notification settings - Fork 2
/
ssh.py
124 lines (95 loc) · 3.38 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import subprocess
import config
import shlex
import signal
import select
_default_ssh_bin_path = "/usr/local/bin/ssh"
_default_ssh_options = {
'ClearAllForwardings': 'yes',
'Compression': 'yes',
'ControlPath': '~/.ssh/__remoter_%h.%p.%r',
'ControlPersist': '3600',
'ForwardX11': 'no',
'NumberOfPasswordPrompts': '0',
'LogLevel': 'QUIET',
#'RequestTTY' : 'force',
}
class SSHConnectionDB (config.ConfigDB):
__config_db_key = "remote_host_database"
def __init__(self):
config.ConfigDB.__init__(self, self.__config_db_key)
def create_connection(self, name, host, port=None, user=None):
conn = SSHConnection(name, host, port, user)
self.set(name, conn)
return conn
class SSHConnection:
def __init__(self, name, host, port, user):
self.__ssh_bin_path = _default_ssh_bin_path
self.__ssh_options = _default_ssh_options
self.name = name
self.host = host
self.port = port
self.user = user
self.last_output = None
self.last_exit_code = None
def __getstate__(self):
return { 'name': self.name, 'host': self.host, 'port': self.port, 'user': self.user}
def __setstate__(self, state):
self.__init__(state['name'], state['host'], state['port'], state['user'])
def ssh_build_cmdline(self, remote_command=None, bare=False):
cmdline = self.__ssh_bin_path
if not bare:
cmdline += " -tt"
# add all our default ssh options
for key in self.__ssh_options:
cmdline += " -o %s=%s" % (key, self.__ssh_options[key])
# user name
if self.user is not None:
cmdline += " -l %s" % self.user
# port
if self.port is not None:
cmdline += " -p %s" % self.port
# host name
if not bare:
cmdline += " %s" % self.host
if not bare and remote_command is not None:
cmdline += " %s" % remote_command
return cmdline
def is_connected(self):
cmdline = self.ssh_build_cmdline("-O check")
try:
ret = subprocess.check_call(cmdline, shell=True)
return True
except:
return False
def connect(self):
cmdline = self.ssh_build_cmdline("-M -N -f")
ret = subprocess.check_call(cmdline, shell=True)
def run(self, command, stdout=subprocess.PIPE):
cmdline = self.ssh_build_cmdline(command)
print "launching [%s]" % cmdline
args = shlex.split(cmdline)
self.pipe = subprocess.Popen(args, bufsize=0, stdout=subprocess.PIPE, stderr=stdout)
def poll(self):
return self.pipe.poll()
def wait(self, incoming_pipe, outgoing_pipe):
read_set = [self.pipe.stdout, incoming_pipe]
while True:
try:
rlist, _, _ = select.select(read_set, [], [])
except select.error, e:
if e.args[0] == errno.EINTR:
print "eintr!"
#self.stop()
return
raise
if self.pipe.stdout in rlist:
data = self.pipe.stdout.read(1)
if not data:
return
outgoing_pipe.write(data)
if incoming_pipe in rlist:
self.stop()
return
def stop(self):
self.pipe.send_signal(signal.SIGHUP)