| Trees | Indices | Help |
|
|---|
|
|
1 ###########################################################################
2 #
3 # This program is part of Zenoss Core, an open source monitoring platform.
4 # Copyright (C) 2007, Zenoss Inc.
5 #
6 # This program is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License version 2 as published by
8 # the Free Software Foundation.
9 #
10 # For complete information please visit: http://www.zenoss.com/oss/
11 #
12 ###########################################################################
13
14 __doc__="""SshClient
15
16 SshClient runs commands on a remote box using ssh
17 and returns their results
18
19 $Id: SshClient.py,v 1.5 2004/04/05 02:05:30 edahl Exp $"""
20
21 __version__ = "$Revision: 1.5 $"[11:-2]
22
23 import os, getpass
24 import logging
25 log = logging.getLogger("zen.SshClient")
26
27 import Globals
28
29 from twisted.conch.ssh import transport, userauth, connection
30 from twisted.conch.ssh import common, keys, channel
31 from twisted.internet import defer, reactor
32
33 from Exceptions import *
34
35 import CollectorClient
36
38
40 #blowing off host key right now, should store and check
41 log.debug('%s host key: %s' % (self.factory.hostname, fingerprint))
42 return defer.succeed(1)
43
47
49 sshconn = SshConnection(self.factory)
50 sshauth = SshUserAuth(self.factory.username, sshconn, self.factory)
51 self.requestService(sshauth)
52
53
55 lastPublicKey = False
57 user = str(user) # damn unicode
58 if user == '':
59 user = os.getlogin()
60 log.debug('attempting to authenticate using username: %s' % user)
61 userauth.SSHUserAuthClient.__init__(self, user, instance)
62 self.user = user
63 self.factory = factory
64
66 if not self.factory.password or self.factory.loginTries <= 0:
67 self.factory.clientFinished()
68 return
69 else:
70 self.factory.loginTries -= 1
71 return defer.succeed(self.factory.password)
72
74 log.debug('Getting Public Key from %s' % self.factory.keyPath)
75 keyPath = os.path.expanduser(self.factory.keyPath)
76 # this works with rsa too
77 # just change the name here and in getPrivateKey
78 path = None
79 if os.path.exists(keyPath):
80 path = keyPath
81 if not path or self.lastPublicKey:
82 # the file doesn't exist, or we've tried a public key
83 return
84 return keys.getPublicKeyString(path+'.pub')
85
94
96
98 log.debug("creating new ssh connection")
99 connection.SSHConnection.__init__(self)
100 self.factory = factory
101
102
105
107 """open a new command channel for each command in queue"""
108 ch = CommandChannel(cmd, conn=self)
109 self.openChannel(ch)
110
112 # grr.. patch SSH inherited method to deal with partially
113 # configured channels
114 self.localToRemoteChannel[channel.id] = None
115 self.channelsToRemoteChannel[channel] = None
116 connection.SSHConnection.channelClosed(self, channel)
117
118
120 name = 'session'
121
123 channel.SSHChannel.__init__(self, conn=conn)
124 self.command = command
125 self.exitCode = None
126
129
131 log.debug('opening command channel for %s' % self.command)
132 self.data = ''
133 log.debug('running command remotely: exec %s' % common.NS(self.command))
134 d = self.conn.sendRequest(self, 'exec', common.NS(self.command),
135 wantReply = 1)
136
140
143
150
151
153
154 - def __init__(self, hostname, ip, port=22, commands=[], options=None,
155 device=None, datacollector=None):
156 CollectorClient.CollectorClient.__init__(self, hostname, ip, port,
157 commands, options, device, datacollector)
158 self.protocol = SshClientTransport
159 self.connection = None
160 self.transport = None
161
162
164 """Start ssh collection.
165 """
166 reactor.connectTCP(self.ip, self.port, self, self.loginTimeout)
167
168
170 """run commands that are in the command queue"""
171 log.info("connected to device %s" % self.hostname)
172 self.connection = sshconn
173 for cmd in self.getCommands():
174 sshconn.addCommand(cmd)
175
176
178 """add command to queue and open a command channel for a command"""
179 CollectorClient.CollectorClient.addCommand(self, commands)
180 if type(commands) == type(''): commands = (commands,)
181 if self.connection:
182 for cmd in commands:
183 self.connection.addCommand(cmd)
184
187 #self.connection.loseConnection()
188
189
191 import socket
192 parser = CollectorClient.buildOptions()
193 options = CollectorClient.parseOptions(parser,22)
194 client = SshClient(options.hostname,
195 socket.gethostbyname(options.hostname),
196 options.port,
197 commands=options.commands, options=options)
198 def stop():
199 if client.commandsFinished():
200 reactor.stop()
201 else:
202 reactor.callLater(1, stop)
203 stop()
204 client.run()
205 reactor.run()
206 import pprint
207 pprint.pprint(client.getResults())
208
209 if __name__ == '__main__':
210 main()
211
| Trees | Indices | Help |
|
|---|
| Generated by Epydoc 3.0beta1 on Thu Oct 25 16:28:39 2007 | http://epydoc.sourceforge.net |