1
2
3
4
5
6
7
8
9
10
11
12
13
14 __doc__="""CollectorClient
15
16 Base class for Telnet and Ssh client collectors
17
18 Device Tree Parameters are:
19
20 zCommandLoginTries - number of times to try login default: 1
21 zCommandPathList - list of path to check for a command
22 zCommandExistanceCheck - shell command issued to look for executible
23 must echo succ if executible is found
24 default: test -f executible
25
26 $Id: CollectorClient.py,v 1.5 2004/04/05 02:05:30 edahl Exp $"""
27
28
29 __version__ = "$Revision: 1.5 $"[11:-2]
30
31 import os, sys, getpass
32 import logging
33 log = logging.getLogger("zen.CmdClient")
34
35 from twisted.internet import protocol, reactor
36
37
38 if os.environ.has_key('USER'):
39 defaultUsername = os.environ['USER']
40 else:
41 defaultUsername = ""
42 defaultPassword = ""
43 defaultLoginTries = 1
44 defaultLoginTimeout = 10
45 defaultCommandTimeout = 10
46 defaultKeyPath = '~/.ssh/id_dsa'
47 defaultSearchPath = []
48 defaultExistanceTest = 'test -f %s'
49
50
52
53 maintainConnection = False
54 cmdindex = 0
55
58
59 self.hostname = hostname
60 self.ip = ip
61 self.port = port
62 commands = commands or []
63 self.cmdmap = {}
64 self._commands = []
65 for pname, cmd in commands:
66 self.cmdmap[cmd] = pname
67 self._commands.append(cmd)
68 self.device = device
69 self.results = []
70 self.protocol = None
71 self.datacollector = datacollector
72
73 if options:
74 defaultUsername = options.username
75 defaultPassword = options.password
76 defaultLoginTries = options.loginTries
77 defaultLoginTimeout = options.loginTimeout
78 defaultCommandTimeout = options.commandTimeout
79 defaultKeyPath = options.keyPath
80 defaultSearchPath = options.searchPath
81 defaultExistanceTest = options.existenceTest
82
83 if device:
84 self.username = getattr(device,
85 'zCommandUsername', defaultUsername)
86 self.password = getattr(device,
87 'zCommandPassword', defaultPassword)
88 self.loginTries = getattr(device,
89 'zCommandLoginTries', defaultLoginTries)
90 self.loginTimeout = getattr(device,
91 'zCommandLoginTimeout', defaultLoginTimeout)
92 self.commandTimeout = getattr(device,
93 'zCommandCommandTimeout', defaultCommandTimeout)
94 self.keyPath = getattr(device,
95 'zKeyPath', defaultKeyPath)
96 self.port = getattr(device, 'zCommandPort', self.port)
97 self.searchPath = getattr(device,
98 'zCommandSearchPath', defaultSearchPath)
99 self.existenceTest = getattr(device,
100 'zCommandExistanceTest', defaultExistanceTest)
101 else:
102 self.username = defaultUsername
103 self.password = defaultPassword
104 self.loginTries = defaultLoginTries
105 self.loginTimeout = defaultLoginTimeout
106 self.commandTimeout = defaultCommandTimeout
107 self.keyPath = defaultKeyPath
108 self.searchPath = defaultSearchPath
109 self.existenceTest = defaultExistanceTest
110
111
112
113
119
120
121 - def addResult(self, command, data, exitCode):
122 "add a result pair to the results store"
123 pname = self.cmdmap.get(command, command)
124 self.results.append((pname, data))
125
126
128 return self._commands
129
130
133
134
136 """called by protocol to see if all commands have been run"""
137 return len(self.results) == len(self._commands)
138
139
141 """tell the datacollector that we are all done"""
142 log.info("command client finished collection for %s",self.hostname)
143 self.cmdindex = 0
144 if self.datacollector:
145 self.datacollector.clientFinished(self)
146
147
148
150 "build options list that both telnet and ssh use"
151
152
153 if not usage:
154 usage = "%prog [options] hostname[:port] command"
155
156 if not parser:
157 from optparse import OptionParser
158 parser = OptionParser(usage=usage,
159 version="%prog " + __version__)
160
161 parser.add_option('-u', '--user',
162 dest='username',
163 default=defaultUsername,
164 help='Login username')
165 parser.add_option('-P', '--password',
166 dest='password',
167 default=defaultPassword,
168 help='Login password')
169 parser.add_option('-t', '--loginTries',
170 dest='loginTries',
171 default=defaultLoginTries,
172 type = 'int',
173 help='number of times to try login')
174 parser.add_option('-L', '--loginTimeout',
175 dest='loginTimeout',
176 type = 'float',
177 default = defaultLoginTimeout,
178 help='timeout login expect statments')
179 parser.add_option('-T', '--commandTimeout',
180 dest='commandTimeout',
181 type = 'float',
182 default = defaultCommandTimeout,
183 help='timeout when issuing a command')
184 parser.add_option('-K', '--keyPath',
185 dest='keyPath',
186 default = defaultKeyPath,
187 help='Path to use when looking for keys')
188 parser.add_option('-s', '--searchPath',
189 dest='searchPath',
190 default=defaultSearchPath,
191 help='Path to use when looking for commands')
192 parser.add_option('-e', '--existenceTest',
193 dest='existenceTest',
194 default=defaultExistanceTest,
195 help='how to check for command')
196 if not parser.has_option('-v'):
197 parser.add_option('-v', '--logseverity',
198 dest='logseverity',
199 default=logging.INFO,
200 type='int',
201 help='Logging severity threshold')
202 return parser
203
204
206 options, args = parser.parse_args()
207 if len(args) < 2:
208 parser.print_help()
209 sys.exit(1)
210 if args[0].find(':') > -1:
211 hostname,port = args[0].split(':')
212 else:
213 hostname = args[0]
214 options.hostname = hostname
215 options.port = port
216 options.commands = args[1:]
217 return options
218