LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Have to leave terminals open on both machines for ssh tunnel (https://www.linuxquestions.org/questions/linux-general-1/have-to-leave-terminals-open-on-both-machines-for-ssh-tunnel-748525/)

manwithaplan 08-18-2009 03:53 PM

Have to leave terminals open on both machines for ssh tunnel
 
My problem is I'm trying to automate the my ssh reverse tunnels.

Currently I have to do this...

ROAMING NOTEBOOK -> ROUTER -> INTERNET -> ROUTER -> HOME-MACHINE


First I run this on the notebook:

Code:

#!/bin/bash
sleep 5
ssh mymachine.***.** -l username -R 25000:127.0.0.1:22 -X -Y >/dev/null

It opens a flashing terminal to my home machine with auto pass keys...


Then I have to run this command on my home machine to open the reverse tunnel:

Code:

ssh 127.0.0.1 -p 25000 -l username -X
This all works fine.... but...

I'd like to connect to the roaming notebook with this reverse tunnel from my home machine, so I need to run the tunnel script on the notebook at start up.

I can successfully achieve all of this ... its just I need to automate the notebook side. The notebook OS is Mint7. So I have tried running the notebook script with the startup preferences, but it neither opens a terminal, or even the ssh tunnel. I have public keys, so password prompts aren't a problem.

My bash is novice at best, below average... and my experience is mostly with openrc and baselayout in Gentoo.

Also if my home machine isn't on, I'd like the script on the notebook to exit, if host isn't available.

Need some suggestions and maybe some sample code. I'm familiar with loops, and case statements... etc Just need idea's where to start on the notebook side.

EDIT: I dont always have to keep my terminal open on my home machine ... Its with the notebook. Tunnel only stays open if I leave the terminal open, with the above script.

wolfperkins 08-19-2009 09:12 AM

I do tunnelling too. I made myself a script that runs in cron and is configured like this:
Code:

* * * * * /path/to/my/script mymachine.***.** 22 25000 > /path/to/log 2>&1
And my script looks like this:
Code:

#!/bin/ksh
TARGET_HOST=${1:-localhost}
TARGET_PORT=${2:-22}
TUNNEL_PORT=${3:-30000}
#Check that we have an active connection to the remote system
ACTIVE_PROCESS=`ps -ef | \
        grep "ssh -X -Y -N -p $TARGET_PORT -R $TUNNEL_PORT:127.0.0.1:22 -l username $TARGET_HOST" | \
        grep -v grep | \
        wc -l`
if [ $ACTIVE_PROCESS -lt 1 ]; then
        echo "`date` : establishing connection to $TARGET_HOST on port $TARGET_PORT"
        ssh -X -Y -N -p $TARGET_PORT -R $TUNNEL_PORT:127.0.0.1:22 -l username $TARGET_HOST >/dev/null
fi

It basically checks if an ssh call is already made to the server to the host before attempting to connect. You let cron handle the automatically scheduled looping. Just make sure you test the script before scheduling it in cron. Everytime it needs to establish a connection it will report it on stdout.

manwithaplan 08-19-2009 10:59 PM

Quote:

Originally Posted by wolfperkins (Post 3649483)
It basically checks if an ssh call is already made to the server to the host before attempting to connect. You let cron handle the automatically scheduled looping. Just make sure you test the script before scheduling it in cron. Everytime it needs to establish a connection it will report it on stdout.

I was brainstorming a way to grep the pid and check it before any ssh tunnel was made at startup, preventing my port problem. Your example script helped immensely. I changed it some (e.g. I have to use the screen command in order for the script to work), or the cron just keeps trying to connect to my server... I tail -f the cron log and it just keeps trying to establish a connection.

And here's the notebooks startup script:

Code:

#!/bin/sh
TARGET_HOST=${1:-myserver.com}
TARGET_PORT=${2:-9874}
TUNNEL_PORT=${3:-22}
#Check that we have an active connection to the remote system
ACTIVE_PROCESS=`ps -ef | \
        grep "ssh $TARGET_HOST -l user -R $TARGET_PORT:127.0.0.1:$TUNNELPORT -X -Y -C" | \
        grep -v grep | \
        wc -l`
if [ $ACTIVE_PROCESS -lt 1 ]; then
        echo "`date` : establishing connection to $TARGET_HOST on port $TARGET_PORT"
        screen -m -d ssh $TARGET_HOST -l user -R $TARGET_PORT:127.0.0.1:$TUNNEL_PORT -X -Y -C >/dev/null
fi

My big problem is myserver is still leaving the ports pid open when the notebook shutsdown or restarts. I'd like for the server to kill the pid when my notebook restarts or shutdowns, so it frees up the port when the notebook restarts

And here is the netstat from my server once the notebook shutdowns or restarts:

Code:

# netstat -anp | grep :9874
tcp        0      0 0.0.0.0:9874            0.0.0.0:*              LISTEN    9088/1

EDIT: This pid problem seems to happen mostly with a WLAN connection

wolfperkins 08-20-2009 01:36 PM

On the server there should be a process that ties the port. You need to identify the process (lsof command might be of assistance), and track when its parent becomes process id 1 you know it is now an orphan and should be killed. Killing that process would then release the port.


All times are GMT -5. The time now is 04:15 AM.