public
Created

A SysV init script to run a SSH Tunnel. A useful example of using Perl to write init scripts.

  • Download Gist
couchdb-tunnel.pl
Perl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
#!/usr/bin/perl
 
# CouchDB Tunnel Tool for MacOS X and Linux
# Copyright (c) 2010 Linode, LLC
# Author: Philip C. Paradis <pparadis@linode.com>
# Modifications: Sam Kleinman <sam@linode.com>
# Usage: couchdb-tunnel.pl [start|stop]
# Access a CouchDB database instance by way of an SSH tunnel.
 
##
## Edit these values to reflect the authentication credentials for the
## server running the CouchDB instance with which you wish to
## connect. If you have chosen to run CouchDB on an alternate port,
## modify the `$remote_port` value. You should not need to modify the
## `$remote_ip` value.
##
 
$remote_user = "username";
$remote_host = "hostname.yourdomain.com";
$remote_port = "5984";
$remote_ip = "127.0.0.1";
 
##
## Modify these values only if you are running a local CouchDB
## instance.
##
 
$local_ip = "127.0.0.1";
$local_port = "5984";
 
##
## You do not need to edit this file beyond this point.
##
 
$a = shift;
$a =~ s/^\s+//;
$a =~ s/\s+$//;
 
$pid=`ps ax|grep ssh|grep $local_port|grep $remote_port`;
$pid =~ s/^\s+//;
@pids = split(/\n/,$pid);
foreach $pid (@pids)
{
if ($pid =~ /ps ax/) { next; }
split(/ /,$pid);
}
 
if (lc($a) eq "start")
{
if ($_[0]) { print "CouchDB tunnel already running.\n"; exit 1; }
else
{
system "ssh -f -L $local_ip:$local_port:$remote_ip:$remote_port $remote_user\@$remote_host -N";
exit 0;
}
}
elsif (lc($a) eq "stop")
{
if ($_[0]) { kill 9,$_[0]; exit 0; }
else { exit 1; }
}
else
{
print "Usage: couchdb-tunnel.pl [start|stop]\n";
exit 1;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.