Playing sound on local computer after command in SSH connection

4

1

I'm in a ssh connection and I would like to play a sound after a command completes (not on the server where I connected).

Something:

ssh me@somewhere.com
make && play-sound-local

I have built-in pc-speaker disabled so echo -e "\a" doesn't work.

Any suggestions?

Egon

Posted 2010-04-04T19:08:31.003

Reputation: 2 513

Very similar question, with answer: "Playing a sound/notification in finch over ssh on my local machine".

– tanius – 2019-01-31T22:19:06.343

Answers

1

You can configure PulseAudio to allow network connections, then use ssh port forwarding and paplay -s on the remote side to send audio back through the ssh connection. Note that the audio may be delayed or stutter based on the fidelity of the audio and the speed of the connection.

Ignacio Vazquez-Abrams

Posted 2010-04-04T19:08:31.003

Reputation: 100 516

1

(This is the answer by @xzfc with some corrections.)

  1. Install ncat on your client and server machine. Because nc in usual distributions (Debian, Ubuntu) does not have the options to execute commands (-c / -e) which we need.

    apt install nmap
    
  2. On your local machine, set up a server that will execute a command to play a sound when a connection is established to it:

    ncat --sh-exec "paplay /usr/share/sounds/freedesktop/stereo/complete.oga" \
      --keep-open --listen localhost 10009
    

    Or use the equivalent shorthand command:

    ncat -klc "paplay file.oga" localhost 10009
    
  3. SSH into your remote host in such a way that a reverse tunnel is established from port 10008 of the remote host to port 10009 on your local machine (where your server is listening):

    ssh -R 10008:localhost:10009 me@somewhere.com
    
  4. Execute your make command as follows in the SSH shell:

    make; ncat --idle-timeout 1s localhost 10008
    

How it works

After the make command (whether successful or not), the ncat command establishes a connection to port 10008 on the remote host, which SSH forwards to port 10009 on your local machine, where your server listens and will play a sound when the connection is made. After the connection has been established, we no longer need it, so it is closed after --idle-timeout 1s. This will show the shell prompt again instead of a "hanging" ncat command, making it apparent that your SSH command chain really finished.

The SSH reverse tunneling makes this work in spite of any routers / firewalls / NAT between your local machine and the remote machine. (Otherwise you would have to set up port forwarding rules in your router, or have a local machine exposed to the Internet.)

Security

We do ncat -l localhost 10009 for the server, which means it listens only on the network interface with the localhost IP address. This local loop interface only allows connections from your own machine (including, in this case, those reverse tunneled in via SSH). So even if you are not behind a firewall, your port 10009 is not exposed to anyone on the Internet.

tanius

Posted 2010-04-04T19:08:31.003

Reputation: 515

1

Set up primitive server (for example, on port 1234) that plays sound on local computer when someone connects, and forward that port to remote server.

On local machine:

nc -l -p 1234 -e "mplayer sound.ogg"

then

ssh -L 1234:localhost:12345 me@somewhere.com
make && nc localhost 12345

PS: you should use ; instead of && if you want to be notified even when make failed.

xzfc

Posted 2010-04-04T19:08:31.003

Reputation: 431