Revision 94265508
b/ncclient/transport/errors.py | ||
---|---|---|
14 | 14 |
|
15 | 15 |
from ncclient import TransportError |
16 | 16 |
|
17 |
class SessionCloseError(TransportError): |
|
18 |
|
|
19 |
def __init__(self, in_buf, out_buf=None): |
|
20 |
msg = 'Unexpected session close.' |
|
21 |
if in_buf: |
|
22 |
msg += '.. IN_BUFFER: |%s| ' % in_buf |
|
23 |
if out_buf: |
|
24 |
msg += '.. OUT_BUFFER: |%s|' % out_buf |
|
25 |
SSHError.__init__(self, msg) |
|
26 |
|
|
17 | 27 |
class SSHError(TransportError): |
18 | 28 |
pass |
19 | 29 |
|
... | ... | |
28 | 38 |
% (hexlify(key.get_fingerprint()), hostname)) |
29 | 39 |
self.hostname = hostname |
30 | 40 |
self.key = key |
31 |
|
|
32 |
class SSHSessionClosedError(SSHError): |
|
33 |
|
|
34 |
def __init__(self, in_buf, out_buf=None): |
|
35 |
msg = 'Unexpected session close.' |
|
36 |
if in_buf: |
|
37 |
msg += '\nIN_BUFFER: %s' % in_buf |
|
38 |
if out_buf: |
|
39 |
msg += '\nOUT_BUFFER: %s' % out_buf |
|
40 |
SSHError.__init__(self, msg) |
b/ncclient/transport/session.py | ||
---|---|---|
66 | 66 |
self.send(HelloBuilder.build(self._client_capabilities)) |
67 | 67 |
error = None |
68 | 68 |
init_event = Event() |
69 |
# callbacks |
|
69 | 70 |
def ok_cb(id, capabilities): |
70 |
self._id, self._capabilities = id, Capabilities(capabilities) |
|
71 |
self._id, self._server_capabilities = id, Capabilities(capabilities)
|
|
71 | 72 |
init_event.set() |
72 | 73 |
def err_cb(err): |
73 | 74 |
error = err |
... | ... | |
75 | 76 |
listener = HelloListener(ok_cb, err_cb) |
76 | 77 |
self.add_listener(listener) |
77 | 78 |
# start the subclass' main loop |
78 |
self.start()
|
|
79 |
self.start() |
|
79 | 80 |
# we expect server's hello message |
80 | 81 |
init_event.wait() |
81 | 82 |
# received hello message or an error happened |
82 | 83 |
self.remove_listener(listener) |
83 | 84 |
if error: |
84 | 85 |
raise error |
85 |
logger.debug('initialized:session-id:%s' % self._id) |
|
86 |
logger.info('initialized: session-id=%s | server_capabilities=%s' % |
|
87 |
(self.id, self.server_capabilities)) |
|
86 | 88 |
|
87 | 89 |
def send(self, message): |
88 | 90 |
logger.debug('queueing:%s' % message) |
... | ... | |
93 | 95 |
|
94 | 96 |
def run(self): |
95 | 97 |
raise NotImplementedError |
96 |
|
|
97 |
def capabilities(self, whose='client'): |
|
98 |
if whose == 'client': |
|
99 |
return self._client_capabilities |
|
100 |
elif whose == 'server': |
|
101 |
return self._server_capabilities |
|
102 | 98 |
|
103 | 99 |
### Properties |
104 | 100 |
|
... | ... | |
141 | 137 |
|
142 | 138 |
def error(self, err): |
143 | 139 |
self._error_cb(err) |
144 |
|
|
145 |
|
|
146 |
class DebugListener: |
|
147 |
|
|
148 |
def __str__(self): |
|
149 |
return 'DebugListener' |
|
150 |
|
|
151 |
def received(self, raw): |
|
152 |
logger.info('DebugListener:[received]:%s' % raw) |
|
153 |
|
|
154 |
def error(self, err): |
|
155 |
logger.info('DebugListener:[error]:%s' % err) |
b/ncclient/transport/ssh.py | ||
---|---|---|
21 | 21 |
import paramiko |
22 | 22 |
|
23 | 23 |
from . import logger |
24 |
from errors import SSHError, SSHUnknownHostError, SSHAuthenticationError, SSHSessionClosedError
|
|
24 |
from errors import SSHError, SSHUnknownHostError, SSHAuthenticationError, SessionCloseError
|
|
25 | 25 |
from session import Session |
26 | 26 |
|
27 | 27 |
BUF_SIZE = 4096 |
... | ... | |
258 | 258 |
self._buffer.write(data) |
259 | 259 |
self._parse() |
260 | 260 |
else: |
261 |
raise SSHSessionClosedError(self._buffer.getvalue())
|
|
261 |
raise SessionCloseError(self._buffer.getvalue())
|
|
262 | 262 |
if not q.empty() and chan.send_ready(): |
263 | 263 |
data = q.get() + MSG_DELIM |
264 | 264 |
while data: |
265 | 265 |
n = chan.send(data) |
266 | 266 |
if n <= 0: |
267 |
raise SSHSessionClosedError(self._buffer.getvalue(), data)
|
|
267 |
raise SessionCloseError(self._buffer.getvalue(), data)
|
|
268 | 268 |
data = data[n:] |
269 | 269 |
except Exception as e: |
270 | 270 |
self.close() |
b/ncclient/transport/util.py | ||
---|---|---|
1 |
# Copyright 2009 Shikhar Bhushan |
|
2 |
# |
|
3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
|
4 |
# you may not use this file except in compliance with the License. |
|
5 |
# You may obtain a copy of the License at |
|
6 |
# |
|
7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
|
8 |
# |
|
9 |
# Unless required by applicable law or agreed to in writing, software |
|
10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
|
11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
12 |
# See the License for the specific language governing permissions and |
|
13 |
# limitations under the License. |
|
14 |
|
|
15 |
from . import logger |
|
16 |
|
|
17 |
class DebugListener: |
|
18 |
|
|
19 |
def __str__(self): |
|
20 |
return 'DebugListener' |
|
21 |
|
|
22 |
def received(self, raw): |
|
23 |
logger.debug('DebugListener:[received]:%s' % raw) |
|
24 |
|
|
25 |
def error(self, err): |
|
26 |
logger.debug('DebugListener:[error]:%r' % err) |
Also available in: Unified diff