0

I have to solve a problem at my firm, which is that we use Raspbian(unix) based Raspberry Pi machines to connect remotely to Windows 7 machines and work from there. The problem is, all the easy to use, free, unix based rdesktop applications can't handle disconnects. They freeze up, and the "not so talented" employees don't know how to stop the rdesktop and reconnect.

I need to write something, preferably a bash application, which can run in the background on the Raspberry and check the connection. If the connection is down it should kill the rdesktop and start a new one as the connection comes back up. I don't know where to start, because while I found some examples, they all used ping to check connection, but my boss said that all the Raspberries sendin ping packets all the time will overload our gateways. Is there a way to check connection without ping?

Drake
  • 111
  • 1
  • 8
  • 1
    This sounds like you want a small patch to avoid getting to the bigger problem. I'd recommend you find a way to make sure that problems like this do not occur instead of cleaning up the mess that is caused. – x13 Sep 15 '16 at 06:56
  • Well, you are right, it should not disconnect, ever. But sometimes it happens, even if we fix the connection. – Drake Sep 15 '16 at 07:05
  • 1
    I agree with @ThisNameBetterBeAvailable . However, if you need a fast solution using a bash script, I would ping periodically, like one packet every 15-30 seconds to avoid flooding – Thomas W. Sep 15 '16 at 07:05

1 Answers1

1

One way to solve it could be to create a daemon which continously checks the connection to the host machine.

Doing it this way involves creating two files

  • the script which pings the host /usr/local/bin/checkconnection.sh
  • the daemon file /etc/init.d/checkconnectiond

Create daemon file:
$ sudo touch /etc/init.d/checkconnectiond $ sudo nano /etc/init.d/checkconnectiond

and paste the following:

# !/bin/sh
# /etc/init.d/checkconnectiond

### BEGIN INIT INFO
# Provides:             checkconnectiond
# Required-Start:       $remote_fs $syslog
# Required-Stop:        $remote_fs $syslog
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6
# Short-Description:    Script for checking connection for remote desktop
# Description:          Script for checking connection for remote desktop
### END INIT INFO

case "$1" in
    start)
        while sleep 30; do (/usr/local/bin/checkconnection.sh &) ; done
        ;;
    stop)
        killall checkconnectiond -q
        ;;
    *)
      echo "Usage: /etc/init.d/checkconnectiond {start|stop}"
      exit 1
      ;;
esac

exit 0

Create the script:

$ sudo nano /usr/local/bin/checkconnection.sh

The script:

if ping -c 1 host_ip &> /dev/null
then
  # do nothing, host is up
else
  killall remotedesktop-pid
fi

Remember to change the host_ip and remotedesktop-pid. You can use the process name when using killall, so if it's called "rdp" you could do killall rdp

Now we got a daemon that will automatically start when booting the raspberry. This daemon runs the checkconnection.sh every 30th second. The checkconnection.sh script run a ping command to the host. If the ping is not successfull it will kill the remote desktop process so the user must manually restart it.

Sources:

Community
  • 1
  • 1
KEK
  • 473
  • 3
  • 15