Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I run a simple ssh proxy and I am always on the move with my laptop. Having to restart it every time I open the lid and connect to the internet.

sshpass -p password ssh -D 9999 root@myvpsip

Is it possible to have it start when my internet does then stop when the internet connection is terminated?

Network Manger
Cinnamon DE
Arch Linux x64
share|improve this question
 
Well it sounds like it already does "stop when the internet connection is terminated". Can just put a script in /etc/NetworkManager/dispatcher.d to start it up. –  Patrick Dec 4 '13 at 13:29
add comment

migrated from serverfault.com Dec 4 '13 at 7:42

This question came from our site for professional system and network administrators.

1 Answer

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
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.