Revision ee4bb099 ncclient/ssh.py
b/ncclient/ssh.py | ||
---|---|---|
13 | 13 |
# limitations under the License. |
14 | 14 |
|
15 | 15 |
import logging |
16 |
import paramiko |
|
17 |
|
|
18 |
from os import SEEK_CUR |
|
19 | 16 |
from cStringIO import StringIO |
17 |
from os import SEEK_CUR |
|
18 |
|
|
19 |
import paramiko |
|
20 | 20 |
|
21 | 21 |
from session import Session, SessionError |
22 | 22 |
|
... | ... | |
42 | 42 |
missing_host_key_policy=paramiko.RejectPolicy): |
43 | 43 |
Session.__init__(self) |
44 | 44 |
self._client = paramiko.SSHClient() |
45 |
self._channel = None |
|
45 | 46 |
if load_known_hosts: |
46 | 47 |
self._client.load_system_host_keys() |
47 | 48 |
self._client.set_missing_host_key_policy(missing_host_key_policy) |
... | ... | |
49 | 50 |
self._parsing_state = 0 |
50 | 51 |
self._parsing_pos = 0 |
51 | 52 |
|
53 |
def _close(self): |
|
54 |
self._channel.close() |
|
55 |
self._connected = False |
|
56 |
|
|
57 |
def _fresh_data(self): |
|
58 |
delim = SSHSession.MSG_DELIM |
|
59 |
n = len(delim) - 1 |
|
60 |
state = self._parsing_state |
|
61 |
buf = self._in_buf |
|
62 |
buf.seek(self._parsing_pos) |
|
63 |
while True: |
|
64 |
x = buf.read(1) |
|
65 |
if not x: # done reading |
|
66 |
break |
|
67 |
elif x == delim[state]: |
|
68 |
state += 1 |
|
69 |
else: |
|
70 |
continue |
|
71 |
# loop till last delim char expected, break if other char encountered |
|
72 |
for i in range(state, n): |
|
73 |
x = buf.read(1) |
|
74 |
if not x: # done reading |
|
75 |
break |
|
76 |
if x==delim[i]: # what we expected |
|
77 |
state += 1 # expect the next delim char |
|
78 |
else: |
|
79 |
state = 0 # reset |
|
80 |
break |
|
81 |
else: # if we didn't break out of above loop, full delim parsed |
|
82 |
till = buf.tell() - n |
|
83 |
buf.seek(0) |
|
84 |
msg = buf.read(till) |
|
85 |
self.dispatch('reply', msg) |
|
86 |
buf.seek(n+1, SEEK_CUR) |
|
87 |
rest = buf.read() |
|
88 |
buf = StringIO() |
|
89 |
buf.write(rest) |
|
90 |
buf.seek(0) |
|
91 |
state = 0 |
|
92 |
self._in_buf = buf |
|
93 |
self._parsing_state = state |
|
94 |
self._parsing_pos = self._in_buf.tell() |
|
95 |
|
|
52 | 96 |
def load_host_keys(self, filename): |
53 | 97 |
self._client.load_host_keys(filename) |
54 | 98 |
|
... | ... | |
96 | 140 |
except Exception as e: |
97 | 141 |
logger.debug('*** broke out of main loop ***') |
98 | 142 |
self.dispatch('error', e) |
99 |
|
|
100 |
def _close(self): |
|
101 |
self._channel.close() |
|
102 |
self._connected = False |
|
103 |
|
|
104 |
def _fresh_data(self): |
|
105 |
delim = SSHSession.MSG_DELIM |
|
106 |
n = len(delim) - 1 |
|
107 |
state = self._parsing_state |
|
108 |
buf = self._in_buf |
|
109 |
buf.seek(self._parsing_pos) |
|
110 |
while True: |
|
111 |
x = buf.read(1) |
|
112 |
if not x: # done reading |
|
113 |
break |
|
114 |
elif x == delim[state]: |
|
115 |
state += 1 |
|
116 |
else: |
|
117 |
continue |
|
118 |
# loop till last delim char expected, break if other char encountered |
|
119 |
for i in range(state, n): |
|
120 |
x = buf.read(1) |
|
121 |
if not x: # done reading |
|
122 |
break |
|
123 |
if x==delim[i]: # what we expected |
|
124 |
state += 1 # expect the next delim char |
|
125 |
else: |
|
126 |
state = 0 # reset |
|
127 |
break |
|
128 |
else: # if we didn't break out of above loop, full delim parsed |
|
129 |
till = buf.tell() - n |
|
130 |
buf.seek(0) |
|
131 |
msg = buf.read(till) |
|
132 |
self.dispatch('reply', msg) |
|
133 |
buf.seek(n+1, SEEK_CUR) |
|
134 |
rest = buf.read() |
|
135 |
buf = StringIO() |
|
136 |
buf.write(rest) |
|
137 |
buf.seek(0) |
|
138 |
state = 0 |
|
139 |
self._in_buf = buf |
|
140 |
self._parsing_state = state |
|
141 |
self._parsing_pos = self._in_buf.tell() |
|
142 | 143 |
|
143 | 144 |
class MissingHostKeyPolicy(paramiko.MissingHostKeyPolicy): |
144 | 145 |
|
Also available in: Unified diff