Odd behaviour with vbs and plink

Microsoft Windows xp professional w/serv...
August 15, 2010 at 11:03:00
Specs: windows xp sp3, not an issue
Ok so I have a script that connects via plink to a linux server then su's to root and runs a command:

objWshScriptExec2.StdIn.Writeline "su -"
objWshScriptExec2.StdIn.Writeline supass
objWshScriptExec2.StdIn.Writeline "Echo Hello"
objWshScriptExec2.StdIn.Writeline "logout"
objWshScriptExec2.StdIn.Writeline "exit"

This works fine and as expected with the command "echo hello"
however if i substitute that command for something useful, such as the command for shutting down my webserver (which takes a few seconds and comes back with a message such as "shutdown succesful". This is why it sleeps for 8seconds to allow time for the proper command to complete) then the logout of su is ok however it does not exit and the plink window will just hang, if a close the window manually (click on the x) then the routine will complete succesfully and the stdout is even grabbed by the script and displayed successfully. It just seems that the exit command is not working for some reason?!

Anyone have any ideas on this?
I hope i have been clear enough.

See More: Odd behaviour with vbs and plink

Report •

August 15, 2010 at 11:46:06
mc87: a message such as "shutdown succesful" . . . . stdout is even grabbed by the script
Does part of your "command for something useful" include the VBScript trying to grab text from StdOut? That would explain what you're describing.

If it IS, send your commands (all of them), then grab the StdOut.

The behavior of WshScriptExec.StdOut isn't really documented well at all. When you read from it, it'll either return however much data you requested (if the stream's buffer already has that much data in it), or it'll hang until the stream closes. (This normally only happens when the application quits.) It might also return if the stream's buffer hits the 4KB mark; I'm not too sure and I'm apparently too lazy to check.

Report •

August 15, 2010 at 15:13:30
1st, thanks for the insight, I like to know useful little things like that.

2nd, the "something useful" command is just a one line start/stop command and no i am grabbing from stdout at the end of all my commands, let me post abit more of the code to see if it helps.

Set objWshScriptExec2 = objShell1.exec("C:\Program Files\Putty\plink.exe -ssh aserver -l auser -pw apassword")
objWshScriptExec2.StdIn.Writeline "su -"
objWshScriptExec2.StdIn.Writeline supass
objWshScriptExec2.StdIn.Writeline " ./blah/blah/blah start"
objWshScriptExec2.StdIn.Writeline "logout"
objWshScriptExec2.StdIn.Writeline "exit"
Set objStdOut2 = objWshScriptExec2.StdOut
strOutput2 = objStdOut2.ReadAll
Textbox1.value = strOutput2

Also the data returned to stdout by the start stop command i am running is about two lines of text, coupled with all the other commands that i run this still ends up being quite a few lines of text. if you still think it might be a buffer size issue, what would be the best way to get maybe the last 4 lines from stdout instead of everything?

Report •

August 17, 2010 at 07:17:38
bump, just incase this has slipped into oblivion

Report •

Related Solutions

August 17, 2010 at 14:21:13
ok, some more info, ive also tried to pull back just the last line of stdout and this errored in exactly the same way. the code i tried was

With objWshScriptExec2
Do While .Status = 0
Do While Not .StdOut.AtEndOfStream
objWshScriptExec2 .StdOut.ReadLine
End With

This code was placed at the end of all the commands again, in the same place where i had placed code that did a .readall before.
The script hung again and when i closed the plink window manually the last line of stdout was displayed correctly again.
I have tested the script with out trying to read stdout and it works perfectly so the issue is definitely with stdout.

So if anyone can suggest a good way i can get the message i am wanting from stdout (it is not the last line either, it is about the 4th out 8 or so lines that are returned by stdout) then please help! :)

Report •

August 19, 2010 at 05:57:25
mc87: bump, just incase this has slipped into oblivion
It did.

I'm not sure where you're pulling this Sleep() command from. As far as I know, it isn't in IE's DOM, nor is it a part of JScript. Are you sure Sleep(5) sleeps for 5 seconds, and not 5 milliseconds? Same thing with the logout command; it isn't in any POSIX compliant environment I use.

Without looking at the plink output, I'm probably not going to be much help.

All that said, if you do have the commands and waits correctly timed, you might get away with something like this:

Set oExec = objShell1.exec("C:\Program Files\Putty\plink.exe -ssh aserver -l auser -pw apassword")
oExec.StdIn.Writeline "su -"
'Assumed: supass is some password input DOM object thingy, and not a variable
oExec.StdIn.Writeline supass.Value
oExec.StdIn.Writeline " ./blah/blah/blah start"
oExec.StdIn.Writeline "logout"
oExec.StdIn.Writeline "exit"
'Input sent; waiting for plink's exit
Do Until oExec.Status 
  oExec.StdIn.Write Chr(4) 'Ctrl-D
Textbox1.value = oExec.StdOut.ReadAll

Report •

August 21, 2010 at 01:34:43
Sleep is my own sub, since im using a hta and cant use wscript.
the timings are fine, sleep(1) is roughtly equal to .5 of a second.
Logout is where i am logging out from the SU

Lastly, i will try you solution below late ron today, im assuming from the look of it that it just keeps doing something until the window closes and then tries to grab stdout once the window has definately closed.

Report •

August 23, 2010 at 07:37:47
Ok have tested this now and unfortunately im getting exactly the same symptoms.

Is there anyway i can get just abit of the stdout instead of the whole thing?

driving me crazy this one!

Report •

August 23, 2010 at 08:59:32
You can use ReadLine, or use Read, assuming you don't empty the buffer. The problem is there's no way to tell how large the buffer is.

Report •

August 23, 2010 at 23:34:14
i guess ill have a go at a slightly different way of doing readline than i have tried before.
will post back with results

Report •

August 24, 2010 at 11:53:40
Just out of curiosity, what output does StdOut return when you close PLINK?

Report •

August 27, 2010 at 05:02:30
Well razor the output is pretty boring just lots of text from showing all the linux prompts/welcome message etc.

ive solved this problem now, as i only needed one line of stdout, so the following solved it, pretty simple and i should have thought of it earlier really

For count = 0 to 5
strOutput2 = objWshScriptExec2.StdOut.Readline

Report •

Ask Question