Module netmiko.telnet_proxy
Expand source code
from typing import Dict, Any, Optional
import socket
import telnetlib
try:
import socks
SOCKS_SUPPORTED = True
except ModuleNotFoundError:
SOCKS_SUPPORTED = False
class Telnet(telnetlib.Telnet):
def __init__(
self,
host: Optional[str] = None,
port: int = 0,
timeout: float = socket._GLOBAL_DEFAULT_TIMEOUT, # type: ignore
proxy_dict: Optional[Dict[str, Any]] = None,
) -> None:
self.proxy_dict = proxy_dict
super().__init__(host=host, port=port, timeout=timeout)
def open(
self,
host: str,
port: int = 0,
timeout: float = socket._GLOBAL_DEFAULT_TIMEOUT, # type: ignore
) -> None:
"""
Connect to a host.
The optional second argument is the port number, which
defaults to the standard telnet port (23).
Don't try to reopen an already connected instance.
proxy_dict = {
'proxy_type': socks.SOCKS5,
'proxy_addr': hostname,
'proxy_port': port,
'proxy_username': username,
'proxy_password': password
}
"""
self.eof = 0
if not port:
port = telnetlib.TELNET_PORT
self.host = host
self.port = port
self.timeout = timeout
if SOCKS_SUPPORTED:
self.sock = socks.create_connection(
(host, port), timeout, **self.proxy_dict
)
else:
msg = """
In order to use the telnet socks proxy, you must 'pip install pysocks'. Note, pysocks is
unmaintained (so use at your own risk).
"""
raise ModuleNotFoundError(msg)
Classes
class Telnet (host: Optional[str] = None, port: int = 0, timeout: float = <object object>, proxy_dict: Optional[Dict[str, Any]] = None)
-
Telnet interface class.
An instance of this class represents a connection to a telnet server. The instance is initially not connected; the open() method must be used to establish a connection. Alternatively, the host name and optional port number can be passed to the constructor, too.
Don't try to reopen an already connected instance.
This class has many read_*() methods. Note that some of them raise EOFError when the end of the connection is read, because they can return an empty string for other reasons. See the individual doc strings.
read_until(expected, [timeout]) Read until the expected string has been seen, or a timeout is hit (default is no timeout); may block.
read_all() Read all data until EOF; may block.
read_some() Read at least one byte or EOF; may block.
read_very_eager() Read all data available already queued or on the socket, without blocking.
read_eager() Read either data already queued or some data available on the socket, without blocking.
read_lazy() Read all data in the raw queue (processing it first), without doing any socket I/O.
read_very_lazy() Reads all data in the cooked queue, without doing any socket I/O.
read_sb_data() Reads available data between SB … SE sequence. Don't block.
set_option_negotiation_callback(callback) Each time a telnet option is read on the input flow, this callback (if set) is called with the following parameters : callback(telnet socket, command, option) option will be chr(0) when there is no option. No other action is done afterwards by telnetlib.
Constructor.
When called without arguments, create an unconnected instance. With a hostname argument, it connects the instance; port number and timeout are optional.
Expand source code
class Telnet(telnetlib.Telnet): def __init__( self, host: Optional[str] = None, port: int = 0, timeout: float = socket._GLOBAL_DEFAULT_TIMEOUT, # type: ignore proxy_dict: Optional[Dict[str, Any]] = None, ) -> None: self.proxy_dict = proxy_dict super().__init__(host=host, port=port, timeout=timeout) def open( self, host: str, port: int = 0, timeout: float = socket._GLOBAL_DEFAULT_TIMEOUT, # type: ignore ) -> None: """ Connect to a host. The optional second argument is the port number, which defaults to the standard telnet port (23). Don't try to reopen an already connected instance. proxy_dict = { 'proxy_type': socks.SOCKS5, 'proxy_addr': hostname, 'proxy_port': port, 'proxy_username': username, 'proxy_password': password } """ self.eof = 0 if not port: port = telnetlib.TELNET_PORT self.host = host self.port = port self.timeout = timeout if SOCKS_SUPPORTED: self.sock = socks.create_connection( (host, port), timeout, **self.proxy_dict ) else: msg = """ In order to use the telnet socks proxy, you must 'pip install pysocks'. Note, pysocks is unmaintained (so use at your own risk). """ raise ModuleNotFoundError(msg)
Ancestors
- telnetlib.Telnet
Methods
def open(self, host: str, port: int = 0, timeout: float = <object object>) ‑> None
-
Connect to a host. The optional second argument is the port number, which defaults to the standard telnet port (23).
Don't try to reopen an already connected instance.
proxy_dict = { 'proxy_type': socks.SOCKS5, 'proxy_addr': hostname, 'proxy_port': port, 'proxy_username': username, 'proxy_password': password }
Expand source code
def open( self, host: str, port: int = 0, timeout: float = socket._GLOBAL_DEFAULT_TIMEOUT, # type: ignore ) -> None: """ Connect to a host. The optional second argument is the port number, which defaults to the standard telnet port (23). Don't try to reopen an already connected instance. proxy_dict = { 'proxy_type': socks.SOCKS5, 'proxy_addr': hostname, 'proxy_port': port, 'proxy_username': username, 'proxy_password': password } """ self.eof = 0 if not port: port = telnetlib.TELNET_PORT self.host = host self.port = port self.timeout = timeout if SOCKS_SUPPORTED: self.sock = socks.create_connection( (host, port), timeout, **self.proxy_dict ) else: msg = """ In order to use the telnet socks proxy, you must 'pip install pysocks'. Note, pysocks is unmaintained (so use at your own risk). """ raise ModuleNotFoundError(msg)