1

I recently bought a laptop and wrote a script that sets up a reverse ssh tunnel if a certain user logs in through GDM. I did this because I've had a laptop stolen in the past and would like to be prepared in case it happens again.

I put the script in the directory below which GDM runs as root whenever a user logins.

/etc/gdm/PostLogin/Default

Here is the script itself:

if [[ "$(users)" = "user user" ]]; then
   while (( "$(netstat -an | grep -E "\:22[ \t]+" | grep ESTABLISHED | \
    wc -l)" < "1" ))
      do
      ssh -fvN -p 22 -R tunnelport:localhost:sshdport user@example.com
      sleep 20
   done
fi

The script runs if I'm already logged in, but if I login through GDM the first if statement fails. If I remove the if statement then it hangs on the ssh command. Any ideas on what I can do?

0

I would enable this script so that it's verbose and writes its output to a file so that you can more easily ascertain what's tripping it up.

For increased verbosity add this before the first if statement.

set -x

To get the script logging everything to a file you can use this method that's described in this SO Q&A titled: How can i fully log all bash scripts actions?. The following is most excerpted from that post:

#!/bin/bash
exec 3>&1 4>&2
trap 'exec 2>&4 1>&3' 0 1 2 3
exec 1>log.out 2>&1
# Everything below will go to the file 'log.out':

Explanation:

  1. exec 3>&1 4>&2

    Saves file descriptors so they can be restored to whatever they were before redirection or used themselves to output to whatever they were before the following redirect.

  2. trap 'exec 2>&4 1>&3' 0 1 2 3

    Restore file descriptors for particular signals. Not generally necessary since they should be restored when the sub-shell exits.

  3. exec 1>log.out 2>&1

    Redirect stdout to file log.out then redirect stderr to stdout. Note that the order is important when you want them going to the same file. stdout must be redirected before stderr is redirected to stdout.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.