VBS (within HTA) Plink

Microsoft Windows xp professional w/sp2
March 10, 2010 at 07:36:54
Specs: Windows XP
Hi,
I have been working on a script, part of which will use plink (command line version of putty, used for making ssh connections)
to connect to a remote server run a command and return the result to a text area within the HTA.
This works fine if i have connected to the server before, but if i havent it will hang because it is not getting a response to the y/n question of storing the host keys.
To get around this i thought i could open a cmd prompt and run plink from there, thus giving me sort of an interactive session, so that if they key was known, the user would not be prompted, but if the host key was not know, the dos box would show the message about accepting the host key, and once you type yes or no the command that you passed to plink via the command line is executed.
(at first i thought i would use putty, but it does not allow you to pass a comaand as a commandline variable like plink does)
The issue i have is that when the script runs plink, the dos box pops up but then it hangs there, and my textarea is populated with
<code>
C:\Documents and Settings\%USERNAME%\Desktop\test\VB work\VBS work\VBS text>
</code>
Which is just the local path of my HTA.
The command works fine when i use it manually with the command line.
Can anyone see whats going wrong?
<code>
Sub Plink
tilltext.Value = "Checking Connection Status..."
Refresh
connect = "0"
Call TillPing(strdata2)
If connect = "1" then
tilltext.Value = "Running Command"
Set objShell1 = CreateObject("WScript.Shell")
Set objWshScriptExec1 = objShell1.exec("cmd /K " & Chr(34) & "C:\Program Files\Putty\plink" & Chr(34) & " -ssh" & strdata2 & " -l MYUSER -pw MYPASS " & strplinkcmd)
Set objStdOut1 = objWshScriptExec1.StdOut
strOutput1 = objStdOut1.ReadAll
TillText.value = strOutput1
Refresh
Else
End If
End Sub
</code>

strdata2 = a variable that hold machine name
strplinkcmd = a linux command


Thanks in advance


See More: VBS (within HTA) Plink

Report •


#1
March 10, 2010 at 18:59:51
i got mine to work using yours as a basis. What i did, first i made a batchfile that did the execution of the plink command,
and i made a remote-commands-script file to test.
the batchfile (test.bat) content:
c:\putty\plink -l USER -pw PASS -m c:\putty\nixscrip -ssh shell.isp.net
exit

"nixscrip" just drops a breadcrumb (tracer) to let me know it was there. nixscrip content:
echo testing>fil.fil
exit

then instead of using the .exec, i used ".run":
set wsh=createobject("wscript.shell")
wsh.run("\putty\test.bat"),1

still working on getting that damn dosbox to close though. so far it just sits there when done. why in hell MS did not build window autoclose when "exit" from batchfile?
thanks for posting your code, it motivated me to start playing with putty and plink, and i learned a lot from your code.
Hth.
update: cmd /c fixed the dosbox issue:
wsh.run ("cmd /c \putty\test.bat")


Report •

#2
March 11, 2010 at 00:39:36
hey, Glad you liked the code, ive found it very useful, for situations where i had already connected to the server.

I had thought of this way of doing it (with a batch) but was hoping someone might know a good way to do it cleanly in VBS


Report •

#3
March 11, 2010 at 02:02:51
this worked, supplying constant "n" (ie: answer "No" to "do you want to register the keys..."):
test.bat:

> N echo n
c:\putty\plink -l USER -pw PASS -m c:\putty\nixscrip -ssh shell.isp.net < n
exit

then from hta you can do wsh.run with 0, (invisible) since no user interaction required:
wsh.run ("cmd /c \putty\test.bat"),0

(security issues not addressed!)
this worked to log me on and off without interaction.


Report •

Related Solutions

#4
March 11, 2010 at 03:04:05
ok, sound slike you have made abit of progress, would you mind posting the entire subroutine? as i have been playing about with your suggestions but cant seem to get it working quite as you say.
thanks

Report •

#5
March 11, 2010 at 03:39:05
this is bare-bones, I stripped it down for clarity:

<!-- forum.hta -->
<script language="vbscript">
Sub window_onload
Set wsh = CreateObject("WScript.Shell")
wsh.run("cmd /c c:\putty\test.bat"),0
End Sub
</script>
'-------- end HTA

test.bat:
> n echo n
\putty\plink -ssh -L USER -pw PASSWD -m \putty\nxscrip shell.ISP.net < \putty\n
exit
::--------- end batchscript

nxscrip:
echo dropping a breadcrumb here > test.x
exit


Report •

#6
March 11, 2010 at 04:13:25
ok, so you are not actually pulling the result of the plink command back to the hta, and displaying it in a textbox, you are just displaying it in a dos box (an invisible one at that?)

Report •

#7
March 11, 2010 at 08:18:33
To clarify for everyone else....

I would like to get plink to run exactly as it would as if i typed:

C:\program files\putty\plink -ssh USER@SERVER -pw MYPASS ANYLINUXCOMMAND

into the command prompt.
this would
A) if connected to server before it would run the command with out prompt
B) if never connected to server before, would prompt you to accept the host key, once accepted it would run the command with out prompt

i then want to take the output of the linux command and use it in my hta


Report •

#8
March 11, 2010 at 08:33:22
After playing with this sub for awhile i have come up with this:

Sub Plink
tilltext.Value = "Checking Connection Status..."
Refresh
connect = "0"
Call TillPing(strdata2)
If connect = "1" then
tilltext.Value = "Running Command..."
set wsh=createobject("wscript.shell")
Set objWshScriptExec1 = wsh.exec("cmd /C " & chr(34) & "C:\Documents and Settings\mccons03\Desktop\test\VB work\VBS work\VBS text\plink.bat" & chr(34) & " " & strdata2 & " " & strplinkcmd)
'Set objShell1 = CreateObject("WScript.Shell")
'Set objWshScriptRun1 = objShell1.run("cmd /c " & chr(34) & "C:\Documents and Settings\mccons03\Desktop\test\VB work\VBS work\VBS text\plink.bat" & chr(34) & " " & strdata2 & strplinkcmd)
Set objStdOut1 = objWshScriptExec1.StdOut
strOutput1 = objStdOut1.ReadAll
TillText.value = strOutput1
Refresh
Else
End If
End Sub

plink.bat contains the plink command as in my original sub but using command line switches to pick up strdata2 and strplinkcmd from the hta. This runs fine an the dos window closes, however it still only display a blank cmd box whilst the command is running, if sucessful (ie no prompt to accept host keys is displayed) all works fine however if you are prompted to accept the host key then it will hang again, and if i manually close the cmd window it will return "Terminate batch job (Y/N)?"

why cant i get this to run the way it would if you just typed the command in at the cmd prompt


Report •

Ask Question