Is it possible to create a sensor via Telnet?, or a script to get data via Telnet Commands?

Hi,
We have a console that we can remotely telnet into and issue a command which provides the following data, is it possible to get this data into HA?

NAME
STREET ADDRESS
CITY STATE ZIP
PHONE NUMBER

AUG 20, 2020  9:43 PM

  #   NAME      UNITS  MEASURE   DIF  TEMP
  1   READING1  2220   28.59     0.0   75.8
  2   READING2  1260   18.94     0.0   77.7
  3   READING3  1406   20.48     0.0   79.7

Yes, write a script to get what you want then use a commandline sensor.

1 Like

@nickrout, sorry for my ignorance, but I have no idea where to start, any chance you can provide an example?

what do you want in the sensor? Bearing in mind a sensor has one value. What are these values actually representing?

Just need the units for each reading.

I guess I should also have asked if there is a lig in username/password and if the server disconnects once it has sent the data?

Hey @nickrout, no login, the flow is as follows in the command window.

telnet ip-address port#

Once connected, you would press ctrl & a together, release, then type 200, which populates the info in OP, and no, the server does not disconnect after providing the data.

I have to say that is an awful system to automate! Before I get into it further, are you sure there is no better system available? Like a rest sensor or something?

You’ll probably need to use the linux utility expect along with the telnet utility.

Something like

#! expect -f

spawn telnet server port
expect "prompt" #or whetever prompt you first get
send "\x01"; send "200"

then you’ll need to use grep and cut to get the value.

I tried this on a Raspberry Pi, but got the following.

pi@raspberrypi:~ $ expect -f
expect: option requires an argument -- 'f'
usage: expect [-div] [-c cmds] [[-f] cmdfile] [args]

That is a script, wite it into a file and make it executable, then execute it :slight_smile:
And it certainly isn’t finished.

I assume this isn’t on the internet, and I won’t be able to try it out?

It is actually is online at a remote location.

perhaps PM me the details, that requires you to trust me. I will NOT be offended if you don’t PM me.

Sure, I was actually able to create a vbs script which writes the output to a file, but have no idea how to bring that info into HA.

Edit: Sent

If you have that file visible to HA, (exactly as the output above) then the command is reasonably simple, to get the first line it’s something like

grep READING1 filename |cut -b 17-20

Ok, I’ll have a go at it, were you able to try it out?

I had a short play but then it was bed time :slight_smile:

:+1: no worries

If you can do it in Python, then you can use telnetlib. The following is what I use to login to a router and get some data. By no means saying its well written, but it does work :smiley:

I run it via cron periodically, and the results are output in JSON, which I decode in HA to create sensors. I am sure you could tailor it to achieve what you do above.

    try:
#        print("Opening Telnet connection")
        telnet = telnetlib.Telnet('192.168.xxx.xxx')
#        print("Created new Telnet instance")
        telnet.read_until(b'Username : ', 20)
#        print("Found username")
        telnet.write(('xxxxxx' + '\r\n').encode('ascii'))
#        print("Sending username")
        telnet.read_until(b'Password : ', 20)
#        print("Found password")
        telnet.write(('xxxxxx' + '\r\n').encode('ascii'))
#        print("Sending password")
        telnet.read_until(b'=>', 20)
#        print("Found prompt")
        telnet.write(('hostmgr list\r\n').encode('ascii'))
#        print("Calling hostmgr")
        devices_result = telnet.read_until(b'=>').split(b'\r\n', 60)
#        print("Command complete")
        telnet.write(('exit' + '\r\n').encode('ascii'))
#        print("Sent exit command")
        exit
    except EOFError:
        print("Unexpected response from router")
        return
    except ConnectionRefusedError:
        print("Connection refused by router. Telnet enabled?")
        return
    except:
        print("Error")
        return

Hope that helps get you started.

1 Like

OK so you have a script that gets the text that the telnet generates. Lets call that text file scirpttest.

I assume that there are always the same line lengths, ie the data you want starts at byte 32 and finishes on 35.

grep searches a file and outputs the line that contains the text searched for.

cut finds characters from that line. In this case by bytecount from the start of the line.

grep READING1 scripttest

outputs

  1   READING1                 1578   22.25     0.0   78.5     4422

After cut is applied

grep READING1 scripttest|cut -b 32-35

outputs

1578

So your commandline sensor

sensor:
  - platform: command_line
    command: "grep READING1 scripttest|cut -b 32-35"
    unit_of_measurement: "gal"
    name: Reading1 Sensor
    upload_interval: #this will depend how often you want it to update