Link to home
Start Free TrialLog in
Avatar of jasimon9
jasimon9Flag for United States of America

asked on

Calling putty from a batch file when setting up ssh tunneling

I am using putty to set up ssh tunneling by checking the "don't start a command or shell at all" checkbox. This works fine.

I have tried to set up a windows batch file to start my IDE and also to kick off several instances of putty for the ssh tunnels. This does not work because for some reason starting ssh in this way does not return processing to its parent shell.

Normally you can execute commands asynchronously in windows by the following method:

 - suppose you wanted to execute from a batch file the following command: "my_job my_paramters"
 - normally the above command in a batch file waits until the job completes before returning for the next line of the batch file
 - the usual method to cause the command to be processed asynchronously is as follows: "cmd /c my_job my_paramters". This cmd /c causes a new shell to be initiated with an immediate return to the batch file

However, this does not work with the "don't start a command or shell at all" setting in putty. I have also tried using "start" which is another approach.

Any ideas of how I could put the putty invocation of the ssh tunnel into a batch file?
Avatar of gheist
gheist
Flag of Belgium image

You should use start.exe /w so it waits for child to complete. CMD never does.
Avatar of jasimon9

ASKER

Thanks for the input, but what I need to do is the opposite -- I don't want the child to complete. I want it to start the first putty and immediately start the second putty.

I explained above the normal way this is done, but that for this particular example, when putty is started with "don't start a command or shell at all", it does not work.
Launch Windows NT comamnd prompt CMD.EXE
Inside that type START.EXE /?

Read text, hope this helps.
Nope, been there, done that. Tried /SEPARATE thinking that might spin it off and continue on with the batch file. No luck.

This is a special case. Normally this is easy to do. Just not in this case.
Please use 32bit version of putty from original website
http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html

Kind of documented in help page:
START.EXE %COMSPEC% /c putty.exe
Using both the start and the cmd as you show actually works to start the two separate putty sessions.

However, I am left with additional command prompt windows. I realize this is two clicks to close, but the ability to start them up from a batch file only saves a couple of extra clicks anyways.

Any way to have them close automatically?
As from START.EXE's help in Vista -

START.EXE /B %COMSPEC% blah blah

/B starts new command without creating window, I wonder if that fills missing bits.

Maybe START.EXE /B START.EXE blah blah detaches process??
I am using XP, btw.

Using the /b either by itself or along with /c results in the putty commands not running.

Also, I tried /min along with /c to at least get the DOS windows to minimize. With /min the putty commands also do not start.

So the only workable solution leaves 3 additional dos prompts .. the original one plus one more for each putty instance. That is a worse result than simply starting the two putty commands manually.

Still hoping for a solution that works.
There is no /C flag for START.EXE on Vista. Consequentially it does not run.

Which command gives extra windows and which ones give nothing?
I do not use Vista. The original question specifies XP, and I again pointed this out in my last post.
This batch file that results in 3 extra DOS sessions:

start %comspec% /c "C:\Program Files\Tools\putty.exe" -load prod-mysql-tunnel
start %comspec% /c "C:\Program Files\Tools\putty.exe" -load dev-mysql-tunnel
"C:\Program Files\Zend\ZendStudio-5.5.1\bin\ZDE.exe"



The following set of batch files results in non starting:

start %comspec% /b test1.bat
start %comspec% /b test2.bat
echo hi

test1.bat
"C:\Program Files\Tools\putty.exe" -load prod-mysql-tunnel

test2.bat
"C:\Program Files\Tools\putty.exe" -load dev-mysql-tunnel
ASKER CERTIFIED SOLUTION
Avatar of gheist
gheist
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
/M is invalid for XP
START.EXE /MIN then
might be getting closer

I have tried some combinations and got the following results:

With a command like
   START.EXE /B %COMSPEC% /C START.EXE /MIN "C:\putty.exe" -load default
I got "Windows cannot find .EXE ...."

With a command like
   START /B %COMSPEC% /C START /MIN "C:\putty.exe" -load default
I got "Windows cannot find -load ...."

With a command like
   START /B %COMSPEC% /C START /MIN "C:\putty.exe -load default"
the putty sessions were simply not started at all.

With a command like
   START /B %COMSPEC% /C "START /MIN C:\putty.exe -load default"
I got "Windows cannot find C:\putty.exe ...."
Use correct path to putty.exe...
doh

Actually, was using the correct command, but just pasted the wrong thing into e-e comment.

The command as tested was as follows, with the quotes moved around to various places:

    start /B %comspec% /c start /MIN "C:\Program Files\Tools\putty.exe -load prod-mysql-tunnel"
and actually got "Windows cannot find C:\Program"

The "cannot find c:\program" part gave me an idea. So I tried

   start /B %comspec% /c "start /MIN C:\Progra~1\Tools\putty.exe -load prod-mysql-tunnel"

This in fact worked to my surprise, and started off the putty session minimized. So I put the lines into the batch file, and it appears to work, with only one DOS window hanging around.

For reference, here is the batch file:

start /B %comspec% /c "start /MIN C:\Progra~1\Tools\putty.exe -load prod-mysql-tunnel"
start /B %comspec% /c "start /MIN C:\Progra~1\Tools\putty.exe -load dev-mysql-tunnel"
"C:\Program Files\Zend\ZendStudio-5.5.1\bin\ZDE.exe"

The short form of the path is needed I am guessing because of a quoting issue in the nested start commands. I did play around with some nested quotes, but did not get that working. So the short form of the path works fine for the purpose.

The only remaining "lack of perfection" was the original DOS window that is open and waiting on command completion. The window may be closed without harming the child processes. When the third line program ZendStudio exits, this DOS window returns to the command prompt.

My next step was to set up an icon to start the batch file, and set it to run minimized. That takes care of the one original DOS window, and I have got the result I sought.

Points to be awarded.
It looks like START is CMD builtin and lost in functionality since Windows 95.

They dont like spaces in filenames either, no matter how hard you quote them.