OT: BASH script to Windows .bat file conversion

Erik Bixby erik.bixby at gmail.com
Tue Nov 7 11:32:07 MST 2006


For what it's worth, I managed to get it working.  I wouldn't classify
my solution as elegant, but it works.

For posterity, the actual batch file is:

@ECHO OFF
rem Batch file to check for ssh connection
rem and bring it up if it's down

c:
cd \putty
if exist netstat.txt (del netstat.txt)
netstat -an > netstat.txt
find "60000" netstat.txt >NUL
if errorlevel 1 (
plink [YOUR_PLINK_COMMANDS_HERE]
)
del netstat.txt

The script searches for port 60000, just so I have a port listening,
so I know plink.exe is running.  I used the following line to run the
.cmd file every minute, with the SYSTEM account:

schtasks /create /sc minute /mo 1 /ru system /tn "SSH Tunnel" /tr
c:\putty\upcheck.bat

You'll want to run it as system, as otherwise you'll have a DOS box
popping up every minute, as I discovered when I initially ran the task
as my user.  You'll also need to import your SSH server's RSA key, so
the system account has access to it.  It took me a while to figure
this one out...  ;)  The way I went about it was to attach to the SSH
server once with putty, so you can get the proper values from
HKEY_CURRENT_USER/Software/SimonTatham/PuTTY/SshHostKeys.  Inside that
"key" is a "string value" named
"rsa2@[SERVER_PORT]:[SERVER_IP/HOSTNAME]."  I just copied and pasted
into the new "string value" I created in
HKEY_USERS\.DEFAULT\Software\SimonTatham\PuTTY\SshHostKeys.  Once you
have that setup properly, the system user can find your server's RSA
key, and can establish the connection.  EDIT YOUR MACHINE'S REGISTRY
AT YOUR OWN RISK!

I don't know that it's the most elegant solution, but it appears to
work (I've tested it a bit).  As always, I'd be happy to hear any
constructive criticism.  And, if I ever get bored enough, I might even
create a nice web page with pretty pictures that walks you through the
entire process step by step...
-Erik

On 11/6/06, Erik Bixby <erik.bixby at gmail.com> wrote:
> [This line intentionally left blank.]
> I have a script that does a job for me in the Linux world.  The script follows:
>
> #!/bin/bash
> # Script to see if SSH tunnel is up, and bring it up if it's down
>
> if test `netstat -an | grep LISTEN | grep :59999 | wc -l` -lt 1
> then
> ssh -N -T -R 60000:localhost:22 -L 59999:localhost:3128
> tunneler at host1.example.com -g
> fi
>
> I run this script in a cron job, and it does a respectable job of
> keeping a SSH tunnel up between two Linux hosts (although,
> constructive input is always welcome).  I am looking to do something
> similar with a Windows host.  I have a plink.exe command line that
> establishes a connection and forwards my ports all around.  However, I
> am hoping someone might be able to help me with an elegant method of
> checking for a link already up, and only running plink.exe if
> necessary.  I would prefer to not check for plink.exe running, in case
> I decide to set up multiple instances.  I'm hoping someone might be
> able to help me with a batch file that accomplishes the same goal as
> this shell script that I can run periodically on the Windows hosts.
>
> And, for the curious, I'm trying to set this up, amongst other
> reasons, so my in-laws' computers will establish a connection to a
> machine I can connect to as well, for remote administration.
>
> As always, I thank everyone in advance for your time in considering my quandary.
> -Erik Bixby
>


More information about the PLUG-discuss mailing list