Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 telnet from ssh tunnel
#1
Trying to get data (mainly temperature) from a UniFi Switch 16 POE-150W.
Have to first enter the switch with ssh, then telnet into the local host, then issue a "show env" command.
I am having problems getting a return of data after the telnet command. Here's how far I got:

##first install pexpect .. pip install pexpect

from pexpect import pxssh
import getpass
import time
import sys
import subprocess

try:
    s = pxssh.pxssh()
    hostname = '192.168.1.7'
    username = 'u-name'
    password = 'p-word'
    s.login(hostname, username, password)
#just to prove we are logged into ssh tunnel
    s.sendline('ls')
    s.prompt()
    print(s.before)

# telnet into the switch	
    s.sendline('telnet localhost')
    time.sleep(.5) # just wait a bit
    print(sys.stdout)
#does not work
#    for line in sys.stdout:
#	    print(line)

#this is the command to get temperature and other data    
    s.sendline('show env\n')
    print(sys.stdout)
 
    s.logout()
except pxssh.ExceptionPxssh as e:
    print("pxssh failed on login.")
    print(e)
Quote
#2
Paramiko could do this.
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.
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Quote
#3
Would SNMP not be a better option for this?

Chances are if you run an snmpwalk against it's top level you will find the temperature in a MIB somewhere. Wrapping this in some python is easy.

Here is a super simple example of reading a list of data from SNMP. I'm sure you can adapt it.

https://gitlab.com/paulcam/home_heating/...er/snmp.py

This might also help:
https://community.ui.com/questions/SNMP-...d1438c2898
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  3.6 telnet eyler 3 4,800 Jun-28-2019, 05:22 AM
Last Post: Khanhamid90
  Any suggestion on python library to use for both ssh and telnet? lord_mani 4 566 Jun-25-2019, 04:07 PM
Last Post: gb74razor
  telnet question jacklee26 2 469 Mar-30-2019, 06:45 AM
Last Post: jacklee26
  Retrieve output from telnet command Networker 1 476 Mar-12-2019, 01:36 PM
Last Post: searching1
  GNS3 telnet from bash terminal to virtual cisco router marienbad 2 529 Feb-24-2019, 07:55 AM
Last Post: marienbad
  Telnet to switch from jump start server(linux) searching1 0 521 Jan-09-2019, 10:03 PM
Last Post: searching1
  Issue when running telnet program using python3 searching1 11 2,406 Dec-22-2018, 12:48 AM
Last Post: searching1
  Aggregate multiple telnet connections Jibeji 1 1,626 Mar-02-2018, 07:21 PM
Last Post: mpd
  mysql connector/telnet issue (re: text game) rebubula76 1 713 Feb-06-2018, 08:00 PM
Last Post: rebubula76
  Multithread telnet not working Parallel anna 7 2,177 Feb-05-2018, 01:17 PM
Last Post: anna

Forum Jump:


Users browsing this thread: 1 Guest(s)