PDA

View Full Version : vpnc, sshd, sftp and SLEEP



rholme
9th February 2017, 03:03 PM
If you use any of ssh, sftp or vpnc and put your machine to sleep, you may have rude surprises when you come back. Sftp and ssh will have timed out leaving a dysfunctional terminal session, vpnc may leave it;s own resolv.conf file possibly stopping all internet name searches. To fix this you need a little script in
/usr/lib/systemd/system-sleep
This script should terminale any sessions for any of these and restore your default resolv.conf file.
I copied my default resolv.conf to /etc/resolv.conf.noVPNC and the below script does it all. If you don;t use vpnc, you can slim it down a little. It also leaves a trace file in /tmp from the last run.

---------
#!/bin/bash
# needs to be installed: /usr/lib/systemd/system-sleep (links don't work)

BACK=resolv.conf.noVPNC
DATE=`date +%Y_%m_%d_%H_%M_%S`
NAME=`basename $0`

LOGFILE=/tmp/${NAME}${DATE}

ABORT() { echo "$NAME at $DATE ERROR: $*" | tee -a $LOGFILE; exit 1; }

if [ $# -ne 2 ]; then ABORT "expect 2 parameters, got $#"
elif [ "$2" != "suspend" ]; then ABORT "second parameter not 'suspend', got $2"
elif [ "$1" = "post" ]; then exit 0
elif [ "$1" != "pre" ]; then ABORT "first parameter not 'pre' or 'post', got $1"
fi
rm -f /tmp/${NAME}* # remove prior incantations
echo "$NAME called with $# params: $*" > $LOGFILE

INCLUDE="ssh|sftp|vpnc"
EXCLUDE="grep|sshd|agent|Agent"
PIDS=`ps -eaf | egrep "$INCLUDE" | egrep -v "$EXCLUDE" | awk '{print $2}'`
for PID in $PIDS
do
LINE=`ps -eaf| grep -w $PID | grep -v grep`
echo "Killing $LINE" >> $LOGFILE
kill -1 $PID
done
if [ "$PIDS" != "" ]; then
(echo "Kill pids: $PIDS"; sleep 1; echo "Remaining"; \
ps -eaf | egrep "$INCLUDE" | egrep -v "$EXCLUDE") >> $LOGFILE
fi
cd /etc
if [ -f $BACK ]; then cp $BACK resolv.conf
else echo "Could not find /etc/$BACK" >> $LOGFILE
fi
exit 0
----