root / src / session / ssh.py @ 14715e41
History | View | Annotate | Download (1.6 kB)
1 |
import paramiko |
---|---|
2 |
|
3 |
from select import select as select |
4 |
|
5 |
from session import Session |
6 |
|
7 |
class SSH(Session): |
8 |
|
9 |
BUFSIZE = 4096
|
10 |
|
11 |
DELIM = ']]>>]]>'
|
12 |
|
13 |
def __init__(self, loadKnownHosts=True, hostname=None, port=22, authType=None, authInfo=None): |
14 |
Session.__init__(self)
|
15 |
self._client = paramiko.SSHClient()
|
16 |
self._client.load_system_host_keys()
|
17 |
self._channel = None |
18 |
self._buffer = '' |
19 |
self._send_buffer
|
20 |
|
21 |
def _connect(self): |
22 |
pass
|
23 |
|
24 |
def _remote_closed(self): |
25 |
pass
|
26 |
|
27 |
def _this_just_in(self, data): |
28 |
buffer += data
|
29 |
pos = buffer.find(DELIM)
|
30 |
if pos != -1: |
31 |
cb(data[:pos]) |
32 |
buffer = buffer[pos + len(DELIM)] |
33 |
|
34 |
def connect(self): |
35 |
self._connect()
|
36 |
self._channel = self._client.get_transport().open_session() |
37 |
self._channel.invoke_subsystem('netconf') |
38 |
Session.connect(self)
|
39 |
|
40 |
def run(self): |
41 |
item = None
|
42 |
sock = self._channel
|
43 |
sock.setblocking(0)
|
44 |
outQ = self._outQ
|
45 |
while True: |
46 |
(r, w, e) = select([sock], [sock], [], 60)
|
47 |
if w:
|
48 |
if not outQ.empty(): |
49 |
to_send += ( outQ.get() + DELIM ) |
50 |
if to_send:
|
51 |
to_send = to_send[sock.send(to_send):] |
52 |
if r:
|
53 |
data = sock.recv(BUFSIZE) |
54 |
if data:
|
55 |
self._this_just_in(data)
|
56 |
else:
|
57 |
self._remote_closed()
|
58 |
|
59 |
class MissingHostKeyPolicy(paramiko.MissingHostKeyPolicy): |
60 |
pass
|