Two generic recommendations first:
- Start by configuring a regular user account
@myvpsip instead of using root.
- Set up public key based authentication for that account so you don't need to mess about with plain text passwords in your ssh scripts.
From the NetworkManager manual page:
NetworkManager will execute scripts in the
/etc/NetworkManager/dispatcher.d directory in alphabetical order in
response to network events. Each script should be (a) a regular file,
(b) owned by root, (c) not writable by group or other, (d) not
set-uid, (e) and executable by the owner. Each script receives two
arguments, the first being the interface name of the device just
activated, and second an action.
So yes, it seems you can trigger actions from NetworkManager on the start and stop of network connections.
A simple script may be (untested):
#!/bin/bash
#/etc/NetworkManager/dispatcher.d/ssh-proxy
case "$2" in
up)
nohup ssh -D 9999 user@myvpsip
tunnelpid=$!
echo $tunnelpid > /var/run/ssh-proxy
;;
down)
if [ -e /var/run/ssh-proxy ] ; then
tunnelpid=$(cat /var/run/ssh-proxy)
kill -9 $tunnelpid
rm /var/run/ssh-proxy
fi
;;
esac
/etc/NetworkManager/dispatcher.dto start it up. – Patrick Dec 4 '13 at 13:29