Read telnet responses

Posted on
Thu Jan 11, 2018 10:14 am
aderrington offline
Posts: 116
Joined: Feb 03, 2015

Read telnet responses

Hi,

I'm looking at wiritng a few python scripts to control some ZuneAudio players running on Raspberry pi's around the house.
I can telnet into them and get them to play etc through terminal, and i can get a python script to play from within Indigo, but what i need to be able to get is the status of the player, this means sending a command and then reading the response given.

I can get a list of the status of the device, i just need to get the 1 line - Status.
Here is the code i'm using currently to get the list.
Code: Select all
import os,re,telnetlib

HOST = "192.168.2.2"
PORT = "6600"
tn = telnetlib.Telnet()
tn.open(HOST, PORT)
tn.write("status\n")
ret1 = tn.read_until("nextsong")
indigo.server.log("\n%s" % ret1)
tn.close


And here's the list. I need to just know if the status is, Play, Pause or Stop.

Code: Select all
11 Jan 2018, 17:11:20
  Script                         
OK MPD 0.19.0
volume: 78
repeat: 1
random: 0
single: 1
consume: 0
playlist: 11
playlistlength: 4
mixrampdb: 0.000000
state: stop
song: 3
songid: 4
nextsong


Can anyone give me any advice on where to go from here?
Many thanks,
Andrew

Posted on
Thu Jan 11, 2018 10:24 am
kw123 offline
User avatar
Posts: 8363
Joined: May 12, 2013
Location: Dallas, TX

Read telnet responses

I am using expect to drive terminal sessions. It is a full blown scripting language made for that. But the learning curve is steep.

If you just want to check the output
Parse the output with find and split in python.



Sent from my iPhone using Tapatalk

Posted on
Thu Jan 11, 2018 10:54 am
Colorado4Wheeler offline
User avatar
Posts: 2794
Joined: Jul 20, 2009
Location: Colorado

Re: Read telnet responses

Well if you are able to get the telnet session from within Py and you can get the output from the shell into Py and all you need to do is parse results you could do this:

Code: Select all
if "state: stop" in ret1:
     do something here....


Super low-brow way to do it but no need to regex it or do other CPU intensive tasks if you are only after the one semi-static value.

My Modest Contributions to Indigo:

HomeKit Bridge | Device Extensions | Security Manager | LCD Creator | Room-O-Matic | Smart Dimmer | Scene Toggle | Powermiser | Homebridge Buddy

Check Them Out Here

Posted on
Thu Jan 11, 2018 11:29 am
kw123 offline
User avatar
Posts: 8363
Joined: May 12, 2013
Location: Dallas, TX

Re: Read telnet responses

and your last line needs to be:
tn.close()
might work w/o the () but you might accumulate open ports w/o ()

Posted on
Thu Jan 11, 2018 12:42 pm
jay (support) offline
Site Admin
User avatar
Posts: 18219
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Read telnet responses

So, here's a bit of Python fun:

Code: Select all
>>> print ret1
11 Jan 2018, 17:11:20
  Script                         
OK MPD 0.19.0
volume: 78
repeat: 1
random: 0
single: 1
consume: 0
playlist: 11
playlistlength: 4
mixrampdb: 0.000000
state: stop
song: 3
songid: 4
nextsong
>>> status_dict = dict([nvstring.split(": ") for nvstring in [line for line in ret1.split("\n") if ": " in line]])
>>> print status_dict
{'songid': '4', 'playlistlength': '4', 'playlist': '11', 'repeat': '1', 'consume': '0', 'mixrampdb': '0.000000', 'random': '0', 'state': 'stop', 'volume': '78', 'single': '1', 'song': '3'}
>>> status_dict["state"]
'stop'
>>>


So, this line:

Code: Select all
status_dict = dict([nvstring.split(": ") for nvstring in [line for line in ret1.split("\n") if ": " in line]])


takes ret1, strips out the lines that don't have a 'name: value' pair, splits said pairs up into separate name and value elements, and creates a dict out of them. The end result is that you can now easily grab any of the values returned with a simple dictionary reference:

Code: Select all
status_dict['state']


All without any special regex parsing, etc. This is why I love Python - it has great built-in functionality for doing this sort of data manipulation.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Thu Jan 11, 2018 12:55 pm
Colorado4Wheeler offline
User avatar
Posts: 2794
Joined: Jul 20, 2009
Location: Colorado

Re: Read telnet responses

Clever :)

My Modest Contributions to Indigo:

HomeKit Bridge | Device Extensions | Security Manager | LCD Creator | Room-O-Matic | Smart Dimmer | Scene Toggle | Powermiser | Homebridge Buddy

Check Them Out Here

Posted on
Thu Jan 11, 2018 1:27 pm
kw123 offline
User avatar
Posts: 8363
Joined: May 12, 2013
Location: Dallas, TX

Re: Read telnet responses

That’s where I want to get to. Mine would have been 10 lines and would have looked like fortan IV



Sent from my iPhone using Tapatalk

Posted on
Thu Jan 11, 2018 4:47 pm
aderrington offline
Posts: 116
Joined: Feb 03, 2015

Re: Read telnet responses

Thats great!!!

Thank you very much Jay (And everyone else for your suggestions)
Thats worked a treat!

Still got a little more work to do for some of the scripts but you've helped enormously!

Thank you!
Andrew

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 2 guests