@@ -67,15 +67,17 @@ def run_command(self, command, *, group_change_expected=False) -> List[str]:
67
67
def connect (self ):
68
68
"""Connect to the Telnet server."""
69
69
try :
70
- self ._telnet = Telnet (self ._host , self ._port , self ._timeout )
71
- self ._telnet .set_option_negotiation_callback (TelnetConnection .__set_max_window_size )
70
+ self ._telnet = Telnet ()
71
+ self ._telnet .set_option_negotiation_callback (TelnetConnection .__negotiate_naws )
72
+ self ._telnet .open (self ._host , self ._port , self ._timeout )
72
73
73
74
self ._read_until (b'Login: ' )
74
75
self ._telnet .write ((self ._username + '\n ' ).encode ('UTF-8' ))
75
76
self ._read_until (b'Password: ' )
76
77
self ._telnet .write ((self ._password + '\n ' ).encode ('UTF-8' ))
77
78
78
79
self ._read_response (True )
80
+ self ._set_max_window_size ()
79
81
except Exception as e :
80
82
message = "Error connecting to telnet server: %s" % str (e )
81
83
_LOGGER .error (message )
@@ -105,30 +107,35 @@ def _read_until(self, needle: Union[bytes, Pattern]) -> (Match, bytes):
105
107
assert i == 0 , "No expected response from server"
106
108
return match , text
107
109
110
+ # noinspection PyProtectedMember
111
+ def _set_max_window_size (self ):
112
+ """
113
+ --> inform the Telnet server of the window width and height. see __negotiate_naws
114
+ """
115
+ from telnetlib import IAC , NAWS , SB , SE
116
+ import struct
117
+
118
+ width = struct .pack ('H' , 65000 )
119
+ height = struct .pack ('H' , 5000 )
120
+ self ._telnet .get_socket ().sendall (IAC + SB + NAWS + width + height + IAC + SE )
121
+
108
122
# noinspection PyProtectedMember
109
123
@staticmethod
110
- def __set_max_window_size (tsocket , command , option ):
124
+ def __negotiate_naws (tsocket , command , option ):
111
125
"""
112
- Set Window size to resolve line width issue
113
- Set Windows size command: IAC SB NAWS <16-bit value> <16-bit value> IAC SE
114
- --> inform the Telnet server of the window width and height.
126
+ --> inform the Telnet server we'll be using Window Size Option.
115
127
Refer to https://www.ietf.org/rfc/rfc1073.txt
116
128
:param tsocket: telnet socket object
117
129
:param command: telnet Command
118
130
:param option: telnet option
119
131
:return: None
120
132
"""
121
- from telnetlib import DO , DONT , IAC , WILL , WONT , NAWS , SB , SE
122
- import struct
133
+ from telnetlib import DO , DONT , IAC , WILL , WONT , NAWS
123
134
124
135
if option == NAWS :
125
- width = struct .pack ('H' , 65000 )
126
- height = struct .pack ('H' , 5000 )
127
- tsocket .send (IAC + WILL + NAWS )
128
- tsocket .send (IAC + SB + NAWS + width + height + IAC + SE )
129
- # -- below code taken from telnetlib source
136
+ tsocket .sendall (IAC + WILL + NAWS )
137
+ # -- below code taken from telnetlib
130
138
elif command in (DO , DONT ):
131
- tsocket .send (IAC + WONT + option )
139
+ tsocket .sendall (IAC + WONT + option )
132
140
elif command in (WILL , WONT ):
133
- tsocket .send (IAC + DONT + option )
134
-
141
+ tsocket .sendall (IAC + DONT + option )
0 commit comments