root / ncclient / session / ssh.py @ a956ef07
History | View | Annotate | Download (4.6 kB)
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 |
import logging |
16 |
from cStringIO import StringIO |
17 |
from os import SEEK_CUR |
18 |
import socket |
19 |
|
20 |
import paramiko |
21 |
|
22 |
from session import Session, SessionError, SessionCloseError |
23 |
|
24 |
logger = logging.getLogger('ncclient.ssh')
|
25 |
|
26 |
BUF_SIZE = 4096
|
27 |
MSG_DELIM = ']]>]]>'
|
28 |
|
29 |
class SSHSession(Session): |
30 |
|
31 |
def __init__(self, load_known_hosts=True, |
32 |
missing_host_key_policy=paramiko.RejectPolicy()): |
33 |
Session.__init__(self)
|
34 |
self._client = paramiko.SSHClient()
|
35 |
self._channel = None |
36 |
if load_known_hosts:
|
37 |
self._client.load_system_host_keys()
|
38 |
self._client.set_missing_host_key_policy(missing_host_key_policy)
|
39 |
self._in_buf = StringIO()
|
40 |
self._parsing_state = 0 |
41 |
self._parsing_pos = 0 |
42 |
|
43 |
def _close(self): |
44 |
self._channel.close()
|
45 |
self._connected = False |
46 |
|
47 |
def _fresh_data(self): |
48 |
delim = MSG_DELIM |
49 |
n = len(delim) - 1 |
50 |
state = self._parsing_state
|
51 |
buf = self._in_buf
|
52 |
buf.seek(self._parsing_pos)
|
53 |
while True: |
54 |
x = buf.read(1)
|
55 |
if not x: # done reading |
56 |
break
|
57 |
elif x == delim[state]:
|
58 |
state += 1
|
59 |
else:
|
60 |
continue
|
61 |
# loop till last delim char expected, break if other char encountered
|
62 |
for i in range(state, n): |
63 |
x = buf.read(1)
|
64 |
if not x: # done reading |
65 |
break
|
66 |
if x==delim[i]: # what we expected |
67 |
state += 1 # expect the next delim char |
68 |
else:
|
69 |
state = 0 # reset |
70 |
break
|
71 |
else: # if we didn't break out of above loop, full delim parsed |
72 |
till = buf.tell() - n |
73 |
buf.seek(0)
|
74 |
msg = buf.read(till) |
75 |
self.dispatch('reply', msg) |
76 |
buf.seek(n+1, SEEK_CUR)
|
77 |
rest = buf.read() |
78 |
buf = StringIO() |
79 |
buf.write(rest) |
80 |
buf.seek(0)
|
81 |
state = 0
|
82 |
self._in_buf = buf
|
83 |
self._parsing_state = state
|
84 |
self._parsing_pos = self._in_buf.tell() |
85 |
|
86 |
def load_host_keys(self, filename): |
87 |
self._client.load_host_keys(filename)
|
88 |
|
89 |
def set_missing_host_key_policy(self, policy): |
90 |
self._client.set_missing_host_key_policy(policy)
|
91 |
|
92 |
def connect(self, hostname, port=830, username=None, password=None, |
93 |
key_filename=None, timeout=None, allow_agent=True, |
94 |
look_for_keys=True):
|
95 |
self._client.connect(hostname, port=port, username=username,
|
96 |
password=password, key_filename=key_filename, |
97 |
timeout=timeout, allow_agent=allow_agent, |
98 |
look_for_keys=look_for_keys) |
99 |
transport = self._client.get_transport()
|
100 |
self._channel = transport.open_session()
|
101 |
self._channel.invoke_subsystem('netconf') |
102 |
self._channel.set_name('netconf') |
103 |
self._connected = True |
104 |
self._post_connect()
|
105 |
|
106 |
|
107 |
def run(self): |
108 |
chan = self._channel
|
109 |
chan.setblocking(0)
|
110 |
q = self._q
|
111 |
try:
|
112 |
while True: |
113 |
if chan.closed:
|
114 |
raise SessionCloseError(self._in_buf.getvalue()) |
115 |
if chan.send_ready() and not q.empty(): |
116 |
data = q.get() + MSG_DELIM |
117 |
while data:
|
118 |
n = chan.send(data) |
119 |
if n <= 0: |
120 |
raise SessionCloseError(self._in_buf.getvalue(), data) |
121 |
data = data[n:] |
122 |
if chan.recv_ready():
|
123 |
data = chan.recv(BUF_SIZE) |
124 |
if data:
|
125 |
self._in_buf.write(data)
|
126 |
self._fresh_data()
|
127 |
else:
|
128 |
raise SessionCloseError(self._in_buf.getvalue()) |
129 |
except Exception as e: |
130 |
logger.debug('*** broke out of main loop ***')
|
131 |
self.dispatch('error', e) |