Revision 13120135
b/src/session/session.py | ||
---|---|---|
9 | 9 |
self._id = None |
10 | 10 |
self._inQ = Queue.Queue() # server -> client |
11 | 11 |
self._outQ = Queue.Queue() # client -> server |
12 |
|
|
12 |
|
|
13 | 13 |
def connect(self): |
14 | 14 |
self.start() |
15 | 15 |
|
... | ... | |
19 | 19 |
def send(self, msg): |
20 | 20 |
self._inQ.add(msg) |
21 | 21 |
|
22 |
def expect_close(self, val=True): |
|
23 |
'''operations.CloseSession must call this before a call to send(), |
|
24 |
so that the remote endpoint closing the connection does not result |
|
25 |
in an exception''' |
|
26 |
self._expect_close = val |
|
27 |
|
|
22 | 28 |
@property |
23 | 29 |
def id(self): |
30 |
'Session ID' |
|
24 | 31 |
return self._id |
32 |
|
|
33 |
@property |
|
34 |
def is_connected(self): |
|
35 |
return self._is_connected |
|
25 | 36 |
|
b/src/session/ssh.py | ||
---|---|---|
1 | 1 |
import paramiko |
2 | 2 |
|
3 |
from select import select as select |
|
4 |
|
|
3 | 5 |
from session import Session |
4 | 6 |
|
5 | 7 |
class SSH(Session): |
6 | 8 |
|
9 |
BUFSIZE = 4096 |
|
10 |
|
|
7 | 11 |
def __init__(self, loadKnownHosts=True, hostname=None, port=22, authType=None, authInfo=None): |
8 | 12 |
Session.__init__(self) |
9 | 13 |
self._client = paramiko.SSHClient() |
... | ... | |
14 | 18 |
def _connect(self): |
15 | 19 |
pass |
16 | 20 |
|
21 |
def _remote_closed(self): |
|
22 |
pass |
|
23 |
|
|
24 |
def _this_just_in(self, data): |
|
25 |
pass |
|
26 |
|
|
17 | 27 |
def connect(self): |
18 | 28 |
self._connect() |
19 |
self._channel = self._client.get_transport().get_channel()
|
|
29 |
self._channel = self._client.get_transport().open_session()
|
|
20 | 30 |
self._channel.invoke_subsystem('netconf') |
21 | 31 |
Session.connect(self) |
22 | 32 |
|
23 | 33 |
def run(self): |
24 | 34 |
item = None |
25 | 35 |
sock = self._channel |
36 |
sock.setblocking(0) |
|
26 | 37 |
inQ, outQ = self._inQ, self._outQ |
27 |
to_send = '' |
|
28 | 38 |
while True: |
29 |
if not outQ.empty(): |
|
30 |
to_send += outQ.get() |
|
31 |
if to_send: |
|
32 |
to_send = to_send[sock.send(to_send):] |
|
33 |
|
|
34 |
|
|
39 |
(r, w, e) = select([sock], [sock], [], 60) |
|
40 |
if w: |
|
41 |
if not outQ.empty(): |
|
42 |
to_send += outQ.get() |
|
43 |
if to_send: |
|
44 |
to_send = to_send[sock.send(to_send):] |
|
45 |
if r: |
|
46 |
data = sock.recv(BUFSIZE) |
|
47 |
if data: |
|
48 |
self._this_just_in(data) |
|
49 |
else: |
|
50 |
self._remote_closed() |
|
51 |
|
|
35 | 52 |
class MissingHostKeyPolicy(paramiko.MissingHostKeyPolicy): |
36 | 53 |
pass |
Also available in: Unified diff