-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathtelnet.py
More file actions
134 lines (117 loc) · 4.51 KB
/
Copy pathtelnet.py
File metadata and controls
134 lines (117 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#
# Copyright (C) 2010-2017 Samuel Abels
# The MIT License (MIT)
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
The Telnet protocol.
"""
from __future__ import absolute_import, unicode_literals
try:
from future import standard_library
standard_library.install_aliases()
except ModuleNotFoundError:
pass
from ..util.tty import get_terminal_size
from . import telnetlib
from .protocol import Protocol
from .exception import ProtocolException, TimeoutException, \
DriverReplacedException, ExpectCancelledException
class Telnet(Protocol):
"""
The Telnet protocol adapter.
"""
def __init__(self, **kwargs):
Protocol.__init__(self, **kwargs)
self.tn = None
def _telnetlib_received(self, data):
self._receive_cb(data, False)
self.buffer.append(data)
def _connect_hook(self, hostname, port):
assert self.tn is None
rows, cols = get_terminal_size()
self.tn = telnetlib.Telnet(hostname,
port or 23,
encoding=self.encoding,
connect_timeout=self.connect_timeout,
termsize=(rows, cols),
termtype=self.termtype,
stderr=self.stderr,
receive_callback=self._telnetlib_received)
if self.debug >= 5:
self.tn.set_debuglevel(1)
if self.tn is None:
return False
return True
def send(self, data):
self._dbg(4, 'Sending %s' % repr(data))
try:
self.tn.write(data)
except Exception:
self._dbg(1, 'Error while writing to connection')
raise
def _domatch(self, prompt, flush):
if flush:
func = self.tn.expect
else:
func = self.tn.waitfor
# Wait for a prompt.
clean = self.get_driver().clean_response_for_re_match
self.response = None
try:
result, match, self.response = func(
prompt, self.timeout, cleanup=clean)
except Exception:
self._dbg(1, 'Error while waiting for ' + repr(prompt))
raise
if match:
self._dbg(2, "Got a prompt, match was %s" % repr(match.group()))
self.buffer.pop(len(self.response))
self._dbg(5, "Response was %s" % repr(self.response))
if result == -1:
error = 'Error while waiting for response from device'
raise TimeoutException(error)
if result == -2:
if self.driver_replaced:
self.driver_replaced = False
raise DriverReplacedException()
else:
raise ExpectCancelledException()
if self.response is None:
raise ProtocolException('whoops - response is None')
return result, match
def cancel_expect(self):
self.tn.cancel_expect = True
def _set_terminal_size(self, rows, cols):
self.tn.set_window_size(rows, cols)
def interact(self, key_handlers=None, handle_window_size=True):
return self._open_shell(self.tn.sock, key_handlers, handle_window_size)
def close(self, force=False):
if self.tn is None:
return
if not force:
try:
self.response = self.tn.read_all()
except Exception:
pass
self.tn.close()
self.tn = None
self.buffer.clear()
super(Telnet, self).close()