Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 10 Nov 2009 18:19:01 +0000
From:      David Collins <davidcollins001@gmail.com>
To:        kdk@daleco.biz, freebsd-questions@freebsd.org
Subject:   Re: Remote ssh tunnel in background or script?
Message-ID:  <4af9ae95.RHunUtG9FYOTtwfD%davidcollins001@gmail.com>
In-Reply-To: <4AF85FC9.10103@daleco.biz>
References:  <4AF85FC9.10103@daleco.biz>

next in thread | previous in thread | raw e-mail | index | archive | help
This is a multi-part message in MIME format.

--=_4af9ae95.IxGfVXUUHDB0EK50Rpo+FJA8sCYptram0K3B+d3MYZoRb0bR
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

Kevin Kinsey <kdk@daleco.biz> wrote:

> Greetings!
>
> In order to continue to allow them to connect to an outbound
> SMTP box on the LAN, I've done this on their server:
>
> sudo ssh -L thisbox:24:remotebox:52525 me@remotebox

I wrote a script to get around my home firewall, it doesn't do exactly
as you want but that only requires changing the ssh bit. I call it
from cron so it stays alive, if it dies it will re-connect otherwise
it just checks a lock file.

It may be of use

David


--=_4af9ae95.IxGfVXUUHDB0EK50Rpo+FJA8sCYptram0K3B+d3MYZoRb0bR
Content-Type: text/plain;
 charset=us-ascii
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
 filename="revssh"

#!/usr/bin/perl


##
## PURPOSE:
##	run reverse ssh to work
##	
##	designed to be run from crontab. creates a lock file so that
##	not more than one instance of the process is started
##


use strict; 
use warnings;


## user crontab doesn't have permission in /var for lock file
## or for ports below 1024
my $username='username';

my $hostname="hostname";
my $address=$hostname.".somewhere.com";
my $port=$ARGV[0]; #2022;

my $lckfile="/tmp/revssh.${hostname}.pid";





sub start_ssh {

    ## fork process to start ssh
    defined( my $pid=fork ) or die "cannot fork process: $!";



    ## parent - open lock file with child pid
    if($pid) {

	print "Starting process: $pid\n";

	open(LOCKFILE,">$lckfile") or die "Cannot create lock file: $!";
	print LOCKFILE "${pid}";
	close(LOCKFILE);

    } else {

	## child - start ssh process
	exec("ssh -qnNCX -R ${port}:localhost:22 ".
	     "${username}\@${address}")
	  or die "cannot exec process\n";
    }

}




## main

if(! -e $lckfile) {

    start_ssh();

} else {

    ## get running(?) pid from pid file
    @ARGV = ($lckfile);my $old_pid = <ARGV>;
    my $running = kill 0, $old_pid;


    ## lock file exists - is process still running?
    if ( $running == 1 ) {
	die "Process running: $old_pid\n";
    } else {
	## check lockfile was deleted!
	if(! unlink $lckfile) {
 	      die "Lockfile not deleted\n";
 	  }
	print "Orphan lock file - Lock file deleted\n\t";

	start_ssh();
    }
}

--=_4af9ae95.IxGfVXUUHDB0EK50Rpo+FJA8sCYptram0K3B+d3MYZoRb0bR--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?4af9ae95.RHunUtG9FYOTtwfD%davidcollins001>