1
2
3
4
5
6
7
8
9
10
11
12
13
14 from twisted.conch.ssh import transport, userauth, connection, common, keys, channel
15 from twisted.internet import defer, protocol, reactor
16 from twisted.python import log
17 import struct, sys, getpass, os
18
19 import logging
20
21
22
23 USER, HOST = None, None
24
25 from Exceptions import NoValidConnection, LoginFailed, CommandTimeout, CommandNotFound
26
28
29 - def __init__(self, searchPath=[], existenceCheck='test -f %s',
30 options=None, context=None, log=None):
41
42
43 - def check(self, hostname, timeout=1):
44 "check to see if a device supports ssh"
45 from telnetlib import Telnet
46 import socket
47 try:
48 tn = Telnet(hostname, 22)
49 index, match, data = tn.expect(['SSH',], timeout)
50 tn.close()
51 if index == 0: return 1
52 except socket.error:
53 return 0
54
56 "login to a machine using ssh"
57 pass
58
59
68
69
71 "run a command using the shell path and return its output"
72 pass
73
74
76 "close the telnet session"
77 pass
78
79
93
94
95
96
97
100 print 'host key fingerprint: %s' % fingerprint
101 return defer.succeed(1)
102
106
107
108
111
112 return defer.succeed(
113 getpass.getpass("%s@%s's password: " % (USER, HOST)))
114
116 log.debug('Getting Public Key from %s' % self.factory.keyPath)
117 path = os.path.expanduser(self.factory.keyPath)
118
119
120 if not os.path.exists(path) or hasattr(self, 'lastPublicKey'):
121
122 return
123 return keys.getPublicKeyString(path+'.pub')
124
129
130
133 for cmd in self.commands:
134 ch = CommandChannel(2**16, 2**15, self)
135 ch.command = cmd
136 self.openChannel(ch)
137
138
140
142 print 'find command %s in path %s failed: %s' % (self.command, self.path, reason)
143
145 self.conn.sendRequest(self, 'exec', common.NS(''))
146
152
153
154
155
157 name = 'session'
158
160 self.log.warn('open of %s failed: %s' % (self.command, reason))
161
163
164 self.data = ''
165 d = self.conn.sendRequest(self, 'exec',
166 common.NS(self.command), wantReply = 1)
167
168
169
170
171
174
176
177 self.loseConnection()
178 self.results.append(self.data)
179 if len(self.results) == len(self.commands):
180 reactor.stop()
181
182
183
184
185
186
187
188
189
190