Paramiko could do this.
To understand it, you have to read the documentation.
This is a minimal interactive example.
The source-address is
The desination-address is the second argument of the
This defines where the server should connect. If the switch is on the same host, then 127.0.0.1.
If this is not the case, for example the switch is somewhere in your network with his own ip,
you must change the destination address to the switch-ip.
The telnetlib is using the already open connection.
The interact method helps to check if the connections works and
if the device accepts commands.
The argumentparser makes it more flexible.
To understand it, you have to read the documentation.
This is a minimal interactive example.
The source-address is
('127.0.0.1', 0)
. The port 0 should take any free port for the client.The desination-address is the second argument of the
open_channel
method.This defines where the server should connect. If the switch is on the same host, then 127.0.0.1.
If this is not the case, for example the switch is somewhere in your network with his own ip,
you must change the destination address to the switch-ip.
import telnetlib import sys from contextlib import contextmanager from argparse import ArgumentParser from getpass import getpass from paramiko import ( SSHClient, MissingHostKeyPolicy, ) @contextmanager def make_tunnel(ssh_user, ssh_password, ssh_host, ssh_port, dst_ip, dst_port): client = SSHClient() client.set_missing_host_key_policy(MissingHostKeyPolicy()) client.connect(hostname=ssh_host, username=ssh_user, password=ssh_password, port=ssh_port) transport = client.get_transport() tunnel = transport.open_channel('direct-tcpip', (dst_ip, dst_port), ('127.0.0.1', 0)) yield tunnel tunnel.close() client.close() if __name__ == '__main__': parser = ArgumentParser() parser.add_argument('ssh_user', type=str) parser.add_argument('ssh_host', type=str) parser.add_argument('dst_ip', type=str) parser.add_argument('dst_port', type=int) parser.add_argument('--ssh_port', type=int, default=22) args = parser.parse_args() password = getpass('Please enter the password: ') try: with make_tunnel(ssh_password=password, **vars(args)) as channel: tn = telnetlib.Telnet() # instead of connecting with telnet # reuse the already open socket served # by ssh tn.sock = channel tn.interact() except KeyboardInterrupt: print('Got KeyboardInterrupt, shutting down graceful') except Exception as e: print('Error:', e) sys.exit(1)The telnet-client's sock object is assigned to the open channel.
The telnetlib is using the already open connection.
The interact method helps to check if the connections works and
if the device accepts commands.
The argumentparser makes it more flexible.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!