Telnet script to amplifier

Hi all!

I have a Pioneer VSX-921 which I am very happy about. It has airplay, lots of other features. One of them is controlling it with your Iphone and telnet.
And now it becomes interesting! I want to make a bash script to log into my amp, send a command: MZ (which means mute) and then exit the telnet session.

I allready installed telnet

sudo apt-get install telnet

I can login with: telnet 192.168.1.155 (it doesn't require a password and portnmbr)
its gives this:

Trying 192.168.1.155...
Connected to 192.168.1.155.
Escape character is '^]'.
BridgeCo AG Telnet server

and when I type MZ it mutes the amp.

Now, is it possible to put this into a script? I allready tried this:

#!/bin/sh

#################################################################

spawn telnet 192.168.1.155
send "MZ"

but is says unknown command spawn and send...

It is also possible to ask the amp what status it has (on/of, mute on/off, how high the volume is and etc) For example: when I type ?p it gives back a code PWR0 or PWR1 (so on or off)

I want to make a script that checks if the amp is on, and if so, it mutes the amp when the script is run.

If I can get some kind of example that would be great!, I can experiment from there... But now, i am stuck :roll:

Thanks anyway!

anyone?

Try:

{
  sleep 2
  echo MZ
  sleep 2
  } | telnet 192.168.1.155

spawn and send are expect, not shell, commands.
The script will be more robust, if you rewrite it in expect.

try�

telnet 192.168.1.155<<EOF
send MZ
EOF

WOW, it works :slight_smile:

I am very happy now! thank you very very much!.
You said it would be more robust if I rewrite this into a expect? but how must I do this. I am running this on a Raspberry Pi BTW.

and is it also to send a command ?p which gives a result PWR0 or PWR1 and then react on it. So something like this

{
  sleep 2
  echo ?p
If PWR0
then 
echo MZ
  sleep 2
  } | telnet 192.168.1.155

Or something like this? I allready tried it but it gaive me a syntax error near }

Assuming that PWR0 means the power is off and PWR1 means the power is on, and assuming that you want to mute if the power is on, I think you want something more like:

{
        sleep 2
        echo '?p'
        sleep 2
} | telnet 192.168.1.155 | (
        read power
        if [ "$power" = "PWR1" ]
        then {  sleep 2
                echo MZ
                sleep 2
        } | telnet  192.168.1.155
        fi
)

Obviously, this is untested (since I don't have an amp like this). If the amplifier has a command to terminate a command session, (something like exit or quit ) you probably also want to add an echo to send that command to the amplifier in both of the above telnet scripts after the 2nd sleep.

Better quote the ? to prevent the shell from attempting a substitution.

  echo "?p"

Agreed, I'll edit my earlier proposal.

Thanks! You are helping me allot!

When I add this code into the command line it gives me this:

{
>   sleep 2
>   echo mz
>   sleep 2
>   } | telnet 192.168.1.155
Trying 192.168.1.155...
Connected to 192.168.1.155.
Escape character is '^]'.
BridgeCo AG Telnet server
FL022020204D555445204F4646202020
MUT1
Connection closed by foreign host.

This is just fine because it mutes the amp. This also works inside a bash script, making it executable and running it from the commandline.

When I add your script into the commandline it doesn't give me any errors, but it doesnt do anything else. It just goes back to the commandline. Maybe the connection is closed by foreign host maybe the problem. I also tried with or without the "" and this wasn't the solution. What am I doing wrong?

thanks again!

You aren't seeing the output from the 1st telnet session because the script is reading it instead of displaying it on the terminal. I was mistakenly expecting to see only PWR0 or PWR1 on the 1st (and only) line of output from telnet . Let's try a slight modification:

#!/bin/ksh
echo '*** Request amp status...'
{	sleep 2
	echo '?p'
	sleep 2
} | telnet 192.168.1.155 | while read power
do	printf "%s\n" "$power"
	if [ "$power" = "PWR1" ]
	then 	echo '*** Amp power is on; send mute command...'
		{	sleep 2
			echo MZ
			sleep 2
		} | telnet 192.168.1.155
		echo '*** Amp should be muted now.'
	fi
done

This should let us see the output from both telnet sessions (assuming we do get a line from the 1st session that just contains "PWR1").

(I normally use ksh, but this script should also work with bash.)

Sorry I couldn't react earlier! I really like that you are helping me, but I've been busy last night so I couldnt try it any sooner(wife nagging :p) .

I've tried you script and adjusted some tiny bits. Strange thing is that the pioneer amp gives a answer on the ?p question with PWR0, instead of 1... which you would expect. Anyway, I've tried your script it gave me this:

pi@domoticzpi ~ $  .  /home/pi/domoticz/scripts/bash/versterkeruit1.sh
*** Request amp status...
Trying 192.168.1.155...
Connected to 192.168.1.155.
Escape character is '^]'.
BridgeCo AG Telnet server
PWR0
Connection closed by foreign host.

So there is something happing! but I it wont go any further. When I look at the telnet session closely it does not only give me the PWR0 answer sometimes, but sometimes a line with numbers. I guess this happens when something changes inside the amp. some sort of feedback. Then again, the script does not go any further so maybe this is not the problem.

I found a site which describes some kind of scripts which stores the output. I don't know if we can use this?
(im not allowed to post URL yet... so put the www infront

thanks anyway...

If you show us the data the script is supposed to handle, we can show you how to handle it.

netcat (nc) would be much better than telnet if you can get it... Telnet's not really meant to be automated. netcat, you can instruct to connect, send, wait a second for an answer, then quit without hanging. It prints no fluff.

$ echo "QUIT" | nc localhost 21 -q 1
220 ProFTPD x.x.x Server (ProFTPD Default Installation) [127.0.0.1]
221 Goodbye.

$

Thank you for your help! I guess what I want is the following:
I need to send this command: ?p (which send a power request) and then the amp responds with PWR0 if on. Then, when on, send MZ (which means Mute)

The amp listens to 192.168.1.155 and has portnmbr 8102.

is this enough?

#!/bin/bash

# Might need \r\n, not sure
T="\n"

amp() {
        printf "%s${T}" "$*" | nc 192.168.1.155 8192 -q 1
}


REPLY=$(amp "?p")

echo "Got reply '$REPLY'"

if [[ "$REPLY" == *PWR0* ]]
then
        echo "got PWR0"
        amp "MZ"
else
        echo "Did not get PWR0"
fi

I run the script and gave me a output, but it was wrong, The amplifier is on, but it gave me did not get PWR0. I changed the port to 8102 and to 23, both gave the same result. Why did you made it port 8192? mistake? I also eddit it with another command, like ?V (which is volume, it should give me VOL125... But it didn't) So I saw you added a comment, maybe it needs \r\n. where does it go? Many thanks for your help! I think we are on the right track :smiley:

It would be nice to know what output -- not just that it was "wrong" -- that's why I put the debugging statements in it, to tell what's happening.

T="\r\n" instead of T="\n" but don't bother doing that yet until you've posted the actual output for us to look at...

well, when I run the script:

pi@domoticzpi ~ $ . /home/pi/domoticz/scripts/bash/versterkeruit2.sh
Got reply ''
Did not get PWR0

It gave me this... How did you expected the debugging info? Am I running it the wrong way? sorry, but im a bit of a noob, I am trying my best though...

I expected some sort of reply to be between the two ' ', instead of nothing. It's getting no answer at all.

You should do chmod +x ./scriptname once, then you will be able to run the script properly with ./scriptname

Try \r\n. It might not have seen the command if it wanted the \r.

You could also try -q 2, though that'd make it lag longer.

Ok, I changed it with -q 2 and added the T="\r\n"

It now DOES give a respond:

pi@domoticzpi ~ $ . /home/pi/domoticz/scripts/bash/versterkeruit2.sh
'ot reply 'PWR0
got PWR0

:smiley: only it doesn't mute, so maybe it needs more lag?

You are still running it wrong, see my above post for how to run it properly. It doesn't technically matter here, but for other scripts may matter a lot.

Interesting... The reply seems to only contain a carriage return, not a newline. (Which is why the last ' gets sent the back of the line) Perhaps you should try T="\r"