Solved

Calling putty from a batch file when setting up ssh tunneling

Posted on 2008-10-21
18
5,219 Views
Last Modified: 2013-12-23
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?
0
Comment
Question by:jasimon9
  • 9
  • 9
18 Comments
 
LVL 61

Expert Comment

by:gheist
Comment Utility
You should use start.exe /w so it waits for child to complete. CMD never does.
0
 

Author Comment

by:jasimon9
Comment Utility
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.
0
 
LVL 61

Expert Comment

by:gheist
Comment Utility
Launch Windows NT comamnd prompt CMD.EXE
Inside that type START.EXE /?

Read text, hope this helps.
0
 

Author Comment

by:jasimon9
Comment Utility
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.
0
 
LVL 61

Expert Comment

by:gheist
Comment Utility
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
0
 

Author Comment

by:jasimon9
Comment Utility
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?
0
 
LVL 61

Expert Comment

by:gheist
Comment Utility
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??
0
 

Author Comment

by:jasimon9
Comment Utility
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.
0
 
LVL 61

Expert Comment

by:gheist
Comment Utility
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?
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

Author Comment

by:jasimon9
Comment Utility
I do not use Vista. The original question specifies XP, and I again pointed this out in my last post.
0
 

Author Comment

by:jasimon9
Comment Utility
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
0
 
LVL 61

Accepted Solution

by:
gheist earned 500 total points
Comment Utility
I am unable to purchase XP to answer your question.

I guess CMD.EXE does not like /B parameter.

Can you repeat after me:

START.EXE /B %COMSPEC% /C START.EXE /M "C:\putty.exe" -load default
0
 

Author Comment

by:jasimon9
Comment Utility
/M is invalid for XP
0
 
LVL 61

Expert Comment

by:gheist
Comment Utility
START.EXE /MIN then
0
 

Author Comment

by:jasimon9
Comment Utility
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 ...."
0
 
LVL 61

Expert Comment

by:gheist
Comment Utility
Use correct path to putty.exe...
0
 

Author Comment

by:jasimon9
Comment Utility
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.
0
 
LVL 61

Expert Comment

by:gheist
Comment Utility
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.
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Join & Write a Comment

Some time ago I was asked to set up a web portal PC to put at our entrance. When customers arrive, they could see a webpage 'promoting' our company. So I tried to set up a windows 7 PC as a kiosk PC.......... I will spare you all the annoyances I…
This is an article about my experiences with remote access to my clients (so that I may serve them) and eventually to my home office system via Radmin Remote Control. I have been using remote access for over 10 years and have been improving my metho…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now