Page 2 of 6

Re: Catch the output of PLINK.exe (PUTTY)

Posted: 25 May 2016 13:54
by LittleBiG
PeterH wrote:Strange. Tried it, using plink v0.67. Destination: local NAS.

In DOS window it works as expected.
With runret: nothing is returned.

So it seems to me that the (complete) output of plink isn't captured, somehow.

:shock: Just tried to mis-spell the userid - now, after a small timeout, runret returned the request for a password??? So *some* output of plink *is* returned! But not the result of a remote command?
The same experience of mine :veryconfused:

EDIT: I have just tried and yes, runret cannot catch anything coming from the server, even if it is written to the standard output. I would be very curious if it is a runret bug...

Re: Catch the output of PLINK.exe (PUTTY)

Posted: 25 May 2016 23:04
by PeterH
highend wrote:plink.exe *without a command*. Mh?

Just "plink.exe"? Does that start a default session from putty?

For me it just prints the command line syntax of it...
...\plink.exe -pw <pw> <user>@192.168.2.2
:arrow:

Code: Select all

Using username "<user>".


BusyBox v1.16.1 (2015-11-12 17:58:02 CST) built-in shell (ash)
Enter 'help' for a list of built-in commands.

DiskStation> cd /
DiskStation> ls
←[0;0mautoupd@te.info←[0m  ←[1;34minitrd←[0m           ←[1;34mroot←[0m             ←[1;34musr←[0m
←[1;34mbin←[0m              ←[1;34mlib←[0m              ←[1;34mrun←[0m              ←[1;34mvar←[0m
←[1;34mdev←[0m              ←[1;34mlost+found←[0m       ←[1;34msbin←[0m             ←[1;34mvar.defaults←[0m
←[1;34metc←[0m              ←[1;34mmnt←[0m              ←[1;34msys←[0m              ←[1;34mvolume1←[0m
←[1;34metc.defaults←[0m     ←[1;34mproc←[0m             ←[1;34mtmp←[0m
DiskStation>
←[0;0m is an example of what I meant.

Re: Catch the output of PLINK.exe (PUTTY)

Posted: 25 May 2016 23:14
by highend
So three possible problems:
- Busybox
- Ash shell
- Colorisation control codes

On my system:
- Bash
- Color codes don't influence what is returned

So maybe you can boil it down to Busybox / Ash...

Re: Catch the output of PLINK.exe (PUTTY)

Posted: 26 May 2016 00:19
by PeterH
OK: CCC (=Color Control Codes :biggrin: ) seem not to be the problem: just executed "help" command, *not* returning any such codes. But runret still doesn't return anything.

With my *very* limited linux knowledge: no more idea. (Proud to know putty+plink :ninja: )
And: no influence on busybox/ash: it's just Synology NAS.

Re: Catch the output of PLINK.exe (PUTTY)

Posted: 26 May 2016 09:30
by LittleBiG
I managed to find a workaround. First I tried this to send the output into file, I added ">result.txt" to the end. It created the result.txt file on the server. Then I tried many different syntaxes and in the end this worked:

Code: Select all

run "cmd /c (C:\putty\PLINK.EXE -pw password user@server.sv.com ""cd data/Archive/;ls"") >result.txt";
I didn't even know that brackets are allowed in a DOS command. Anyway, this syntax fills the result.txt with the file list. It will be a Unix file in the end, so at the end of each line there is only LF, not CRLF. However, change this command to runret still won't work. I would really ask Don, why the display and a file can catch the return but not runret.

Re: Catch the output of PLINK.exe (PUTTY)

Posted: 26 May 2016 09:47
by highend
Why your last command doesn't work with runret? runret() captures the standard output, not file streams...
Guys, find out what you have in common to narrow the issue down. It works flawlessly on my side...

What happens if you execute:

Code: Select all

text runret("C:\putty\PLINK.EXE -pw password user@server.sv.com ""uname""", <curpath>);

Re: Catch the output of PLINK.exe (PUTTY)

Posted: 26 May 2016 09:58
by LittleBiG
highend wrote:Why your last command doesn't work with runret? runret() captures the standard output, not file streams...
Guys, find out what you have in common to narrow the issue down. It works flawlessly on my side...

What happens if you execute:

Code: Select all

text runret("C:\putty\PLINK.EXE -pw password user@server.sv.com ""uname""", <curpath>);
A big, fat nothing. Full empty. :-(

EDIT:When I change it to run instead of runret and add cmd /k not to close the DOS window, I get "Linux" written into the DOS window.

Re: Catch the output of PLINK.exe (PUTTY)

Posted: 26 May 2016 10:11
by highend
If you like, change the password of that user, limit his permissions to read access on "data/Archive" and pm me the username, pw + fqdn of that machine. Let's see if the problem is the machine itself or your (and PeterH's) environment... Afterwards you'll change the pw again + restore permissions...

Re: Catch the output of PLINK.exe (PUTTY)

Posted: 26 May 2016 11:04
by LittleBiG
highend wrote:If you like, change the password of that user, limit his permissions to read access on "data/Archive" and pm me the username, pw + fqdn of that machine. Let's see if the problem is the machine itself or your (and PeterH's) environment... Afterwards you'll change the pw again + restore permissions...
Thanks but it is not possible. I don't have any other kind of access of this server than read. Actually I am not even sure where this server is on the globe...

Re: Catch the output of PLINK.exe (PUTTY)

Posted: 27 May 2016 12:14
by PeterH
highend wrote:If you like, change the password of that user, limit his permissions to read access on "data/Archive" and pm me the username, pw + fqdn of that machine. Let's see if the problem is the machine itself or your (and PeterH's) environment... Afterwards you'll change the pw again + restore permissions...
Sorry: no chance to reach my NAS from outside.
(And not wanting to change that in the moment :whistle: )

"Environment": you think of the remote side, don't you? Something I could have a look at? (As said: very limited linux knowledge here.)

So in the moment:
- with >c:file.txt always all text (especially of plink) can be captured
- for you this seems to be the case for runret(), too
- runret() for me and LittleBig captures "a lot" of output, but misses the re-routed output that plink received from the external system (while it does show "native" output of plink, i.e. error messages)

The basic reason *might* be something on the remote system, (maybe the shell in use,) making plink to somehow prevent runret capturing the data.

One more idea I had was line endings: LF instead of CR LF. But if my test is correct this also doesn't make a difference.

Hm - as I said before: strange! Out of ideas...

Re: Catch the output of PLINK.exe (PUTTY)

Posted: 27 May 2016 12:49
by LittleBiG
I would be curious if other scripting language could get the output correctly...

Re: Catch the output of PLINK.exe (PUTTY)

Posted: 27 May 2016 13:28
by highend
Sorry: no chance to reach my NAS from outside.
Open your firewall, forward ports! :D

No, I never said that I tried to use runret in conjunction with a file redirection. But it doesn't matter in this case.

Environment: Yeah, the external machine, not your own. And as I stated earlier in this thread, 3 possibilities:
- Busybox
- Ash shell (or a shell different than bash)
- Colorisation control codes

I don't have a Synology at home and I don't have access to the server of LittleBig. So there are some possibilites (for you) to find out why it isn't working. One way: Fire up a virtual machine (Hyper-V, VMware player, etc.) with a live version of Linux operating systems. Fire up the ssh server and connect to it via plink. Use a live cd with busybox and one with a normal shell (which you can than replace with ash).

And if you really want to find out if a different language caputures the output, use this (Autohotkey):

Code: Select all

MsgBox, % Runret("<path to plink>\plink.exe -pw <password> <iser>@<ip> cd /media;ls")


; http://www.autohotkey.com/board/topic/15455-stdouttovar/page-7
Runret(sCmd, sParams="")
{
	; Assemble command + parameters
	sCmd := (sParams) ? sCmd . " " . sParams : sCmd

	DllCall("CreatePipe", "Ptr*", hStdInRd , "Ptr*", hStdInWr , "Uint", 0, "Uint", 0)
	DllCall("CreatePipe", "Ptr*", hStdOutRd, "Ptr*", hStdOutWr, "Uint", 0, "Uint", 0)
	DllCall("SetHandleInformation", "Ptr", hStdInRd , "Uint", 1, "Uint", 1)
	DllCall("SetHandleInformation", "Ptr", hStdOutWr, "Uint", 1, "Uint", 1)

	; Fill a StartupInfo structure
	if A_PtrSize = 4 ; We're on a 32-bit system
	{
		VarSetCapacity(pi, 16, 0)
		sisize := VarSetCapacity(si, 68, 0)
		NumPut(sisize,    si,  0, "UInt")
		NumPut(0x100,     si, 44, "UInt")
		NumPut(hStdInRd , si, 56, "Ptr") ; stdin
		NumPut(hStdOutWr, si, 60, "Ptr") ; stdout
		NumPut(hStdOutWr, si, 64, "Ptr") ; stderr
	}
	else if A_PtrSize = 8 ; We're on a 64-bit system
	{
		VarSetCapacity(pi, 24, 0)
		sisize := VarSetCapacity(si, 96, 0)
		NumPut(sisize,    si,  0, "UInt")
		NumPut(0x100,     si, 60, "UInt")
		NumPut(hStdInRd , si, 80, "Ptr") ; stdin
		NumPut(hStdOutWr, si, 88, "Ptr") ; stdout
		NumPut(hStdOutWr, si, 96, "Ptr") ; stderr
	}

	DllCall("CreateProcess" , "Uint", 0			 ; Application Name
							, "Ptr", &sCmd		 ; Command Line
							, "Uint", 0			 ; Process Attributes
							, "Uint", 0			 ; Thread Attributes
							, "Int", True		 ; Inherit Handles
							, "Uint", 0x08000000 ; Creation Flags (0x08000000 = Suppress console window)
							, "Uint", 0			 ; Environment
							, "Uint", 0			 ; Current Directory
							, "Ptr", &si		 ; Startup Info
							, "Ptr", &pi)		 ; Process Information

	DllCall("CloseHandle", "Ptr", NumGet(pi, 0))
	DllCall("CloseHandle", "Ptr", NumGet(pi, A_PtrSize))
	DllCall("CloseHandle", "Ptr", hStdOutWr)
	DllCall("CloseHandle", "Ptr", hStdInRd)
	DllCall("CloseHandle", "Ptr", hStdInWr)

	VarSetCapacity(sTemp, 4095)
	nSize := 0
	Loop
	{
		result := DllCall("Kernel32.dll\ReadFile", "Uint", hStdOutRd, "Ptr", &sTemp, "Uint", 4095, "UintP", nSize, "Uint", 0)
		if (result = "0")
			break
		else
			sOutput := sOutput . StrGet(&sTemp, nSize, "CP850")
	}

	DllCall("CloseHandle", "Ptr", hStdOutRd)
	return, sOutput
}


Re: Catch the output of PLINK.exe (PUTTY)

Posted: 27 May 2016 13:41
by LittleBiG
Thanks, works perfectly. :veryconfused: So can we say that something is not ok with the XY runret?

Re: Catch the output of PLINK.exe (PUTTY)

Posted: 27 May 2016 14:29
by highend
Open a cmd prompt and enter:

Code: Select all

chcp {enter}
Output?

Re: Catch the output of PLINK.exe (PUTTY)

Posted: 27 May 2016 15:12
by LittleBiG
highend wrote:Open a cmd prompt and enter:

Code: Select all

chcp {enter}
Output?
Active code page: 437