LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 08-18-2009, 03:53 PM   #1
manwithaplan
Member
 
Registered: Nov 2008
Location: ~/
Distribution: Arch || Sidux
Posts: 393

Rep: Reputation: 45
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.

Last edited by manwithaplan; 08-18-2009 at 04:04 PM. Reason: Clarification
 
Old 08-19-2009, 09:12 AM   #2
wolfperkins
Member
 
Registered: Oct 2007
Location: Val-des-Monts, Québec, Canada
Distribution: CentOS, RHEL, Fedora
Posts: 109

Rep: Reputation: 16
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.
 
Old 08-19-2009, 10:59 PM   #3
manwithaplan
Member
 
Registered: Nov 2008
Location: ~/
Distribution: Arch || Sidux
Posts: 393

Original Poster
Rep: Reputation: 45
Quote:
Originally Posted by wolfperkins View Post
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

Last edited by manwithaplan; 08-19-2009 at 11:47 PM.
 
Old 08-20-2009, 01:36 PM   #4
wolfperkins
Member
 
Registered: Oct 2007
Location: Val-des-Monts, Québec, Canada
Distribution: CentOS, RHEL, Fedora
Posts: 109

Rep: Reputation: 16
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.
 
  


Reply

Tags
ssh tunnel


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
SSH hang does not leave any messages in logs averroes Red Hat 3 07-12-2009 05:03 AM
SSH Tunnel Through 2 machines? philosophia Linux - Newbie 3 06-25-2007 04:13 PM
setting up an ssh soxy or local ssh tunnel from within an ssh soxy Mangenius Linux - Networking 0 03-05-2007 03:15 PM
Terminals - spying on local terminals with ssh BeatRyder Linux - Software 5 10-21-2004 01:47 AM
automatically open ssh tunnel with xinetd polle007 Linux - Networking 2 07-17-2003 05:11 PM


All times are GMT -5. The time now is 12:21 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration