AvailableProgramError() ReadProgramErrorData()

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
infratec
Always Here
Always Here
Posts: 7588
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

AvailableProgramError() ReadProgramErrorData()

Post by infratec »

Hi,

often when I use RunProgram() and I have to deal with the output of the program, I did not get all the
stuff which is written in a cmd window.

For example:
c:\Program Files (x86)\PuTTY>plink -ssh -pw wrongpassword root@192.168.0.1 "tcpdump -ni eth0 -s 0 -U -w - not port 22"
Access denied
root@217.113.176.50's password:
I can fetch the line with 'password:' via AvailableProgramOutput() and ReadProgramData()
But the line with 'Access denied' is not fetchable with this nor with ReadProgramError()

Code: Select all

Parameter$ = "-ssh -pw wrongpassword root@192.168.0.1 "
Parameter$ + #DQUOTE$
Parameter$ + "tcpdump -ni eth0 -s 0 -U -w - not port 22"
Parameter$ + #DQUOTE$

PLink = RunProgram("c:\Program Files (x86)\PuTTY\plink.exe", Parameter$, "", #PB_Program_Open|#PB_Program_Read|#PB_Program_Write|#PB_Program_Error)

If IsProgram(PLink)
  *Buffer = AllocateMemory(4096)
  If *Buffer
    Repeat
      Error$ = ReadProgramError(PLink)
      If Len(Error$)
        Debug "stderr: " + Error$
      EndIf
      AvailableData = AvailableProgramOutput(PLink)
      If AvailableData
        If AvailableData > MemorySize(*Buffer)
          AvailableData = MemorySize(*Buffer)
        EndIf
        ReadProgramData(PLink, *Buffer, AvailableData)
        Debug "stdout: " + PeekS(*Buffer, AvailableData, #PB_UTF8)
      EndIf
    Until Len(Error$)
    FreeMemory(*Buffer)
  EndIf
  
EndIf
Maybe it is send to stderr but not 'flushed' or whatever.
Or it is a bug, but other stuff send to stderr is readable, for example if tcpdump is not installed on the PC.
So I don't think it's a bug.

Bernd
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: AvailableProgramError() ReadProgramErrorData()

Post by Michael Vogel »

I just searched for a thread which contains a solution for the missing AvailableProgramError() function and found your posting - maybe you can check, if plink forwards all needed information by using the following code...

Code: Select all

CmdHandle=RunProgram("plink.exe",Cmd,Tmp,#PB_Program_Open|#PB_Program_Hide|#PB_Program_Error|#PB_Program_Read)
AvailableProgramError.s

If CmdHandle
	Debug "..."
	While ProgramRunning(CmdHandle)
		Delay(5); avoid a high CPU usage
		AvailableProgramError=ReadProgramError(CmdHandle)
		If AvailableProgramError
			Debug "ERR: "+AvailableProgramError
		EndIf
		If AvailableProgramOutput(CmdHandle)
			Debug "OUT: "+ReadProgramString(CmdHandle)
		EndIf
	Wend
	Debug "Exitcode: " + Str(ProgramExitCode(CmdHandle))
	CloseProgram(CmdHandle)
EndIf
Post Reply