Wednesday, October 28, 2009

help creating a cron job (ssh tunnel) - Ubuntu Forums

help creating a cron job (ssh tunnel) - Ubuntu Forums: "help creating a cron job (ssh tunnel)
i need to establish and keep alive a reverse ssh tunnel. but i only want to do this over the weekend. i need to establish the connection late friday night, and terminate the connection late sunday night.

here is the script i'm using to establish the connection:
Code:

#!/bin/sh

# $REMOTE_HOST is the name of the remote system
REMOTE_HOST=my.home.system

# $REMOTE_PORT is the remote port number that will be used to tunnel
# back to this system
REMOTE_PORT=5000

# $COMMAND is the command used to create the reverse ssh tunnel
COMMAND='ssh -N -R $REMOTE_PORT:localhost:22 $REMOTE_HOST'

# Is the tunnel up? Perform two tests:

# 1. Check for relevant process ($COMMAND)
pgrep -f -x '$COMMAND' || $COMMAND

# 2. Test tunnel by looking at 'netstat' output on $REMOTE_HOST
ssh $REMOTE_HOST netstat -an | egrep 'tcp.*:$REMOTE_PORT.*LISTEN' \
> /dev/null 2>&1
if [ $? -ne 0 ] ; then
pkill -f -x '$COMMAND'
$COMMAND
fi"

No comments:

Post a Comment