Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2014-05

Ping problems - OS/X

16 messages · 8 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Peter Brooks

I've got a little program that just pings. It runs. I get a 0 error return, but there doesn't seem to be any output:

-rw-r--r-- 1 root wheel 0 21 May 11:09 output.txt

So it's re-writing the output.txt file, but the
written(AstringList.Text) isn't putting anything there.

This is almost a direct copy from the examples, so I'm not clear what I'm doing wrong. It could be that TStringList isn't working as I thought - because even the 'stdout:' doesn't appear in the file 'output.txt'. Or it could be that AProcess.Output isn't working.

Any suggestions?
Free Pascal Compiler version 2.6.4 [2014/02/26] for i386 OS/X 10.9.3

Program nmp(input,output);
uses
  Classes, SysUtils,  Process;
var
  AProcess: TProcess;
  AStringList: TStringList;
  n :Integer;
begin { main }
  for n := 0 to 20 do
        begin { for }
        AStringList := TStringList.Create;
        AProcess := TProcess.Create(nil);
        AProcess.Executable := 'ping';
        AProcess.Parameters.Add('-c 1');
        AProcess.Parameters.Add('www.freepascal.org');
        AProcess.Options := AProcess.Options + [poWaitOnExit, poUsePipes];
        AProcess.Execute;
        AStringList.SaveToFile('output.txt');
        Writeln(n,' Execution Over: Exit Status: ',AProcess.ExitStatus);
        AStringList.Add('stdout:');
        AStringList.LoadFromStream(Aprocess.Output);
        AStringList.Add('stderr:');
        AStringList.LoadFromStream(AProcess.Stderr);
        AStringList.LoadFromStream(AProcess.Output);
        writeln(AStringList.Text);
        AstringList.Free;
        AProcess.Free;
        end; {for}
close(output) {added just in case it was losing the write}
end.

Reply by email

Mattias Gaertner
to Peter Brooks
Show quoted text 44 lines
I've got a little program that just pings. It runs. I get a 0 error return, but there doesn't seem to be any output: -rw-r--r-- 1 root wheel 0 21 May 11:09 output.txt So it's re-writing the output.txt file, but the written(AstringList.Text) isn't putting anything there. This is almost a direct copy from the examples, so I'm not clear what I'm doing wrong. It could be that TStringList isn't working as I thought - because even the 'stdout:' doesn't appear in the file 'output.txt'. Or it could be that AProcess.Output isn't working. Any suggestions? Free Pascal Compiler version 2.6.4 [2014/02/26] for i386 OS/X 10.9.3 Program nmp(input,output); uses Classes, SysUtils, Process; var AProcess: TProcess; AStringList: TStringList; n :Integer; begin { main } for n := 0 to 20 do begin { for } AStringList := TStringList.Create; AProcess := TProcess.Create(nil); AProcess.Executable := 'ping'; AProcess.Parameters.Add('-c 1'); AProcess.Parameters.Add('www.freepascal.org'); AProcess.Options := AProcess.Options + [poWaitOnExit, poUsePipes]; AProcess.Execute; AStringList.SaveToFile('output.txt'); Writeln(n,' Execution Over: Exit Status: ',AProcess.ExitStatus); AStringList.Add('stdout:'); AStringList.LoadFromStream(Aprocess.Output); AStringList.Add('stderr:'); AStringList.LoadFromStream(AProcess.Stderr); AStringList.LoadFromStream(AProcess.Output); writeln(AStringList.Text);
Move it here:
AStringList.SaveToFile('output.txt');
Show quoted text 5 lines
AstringList.Free; AProcess.Free; end; {for} close(output) {added just in case it was losing the write} end.

Mattias

Reply by email

Peter Brooks
to Mattias Gaertner

Sorry, I misled you. I did move it higher up, earlier, to test something, and I forgot to put it back.

This version has exactly the same behaviour:

-rw-r--r-- 1 peterbrooks wheel 0 21 May 13:00 output.txt

Program nmp(input,output);
uses
  Classes, SysUtils,  Process;
var
  AProcess: TProcess;
  AStringList: TStringList;
  n :Integer;
begin { main }
  for n := 0 to 20 do
begin { for }
  AStringList := TStringList.Create;
  AProcess := TProcess.Create(nil);
  AProcess.Executable := 'ping';
  AProcess.Parameters.Add('-c 1');
  AProcess.Parameters.Add('www.freepascal.org');
  AProcess.Options := AProcess.Options + [poWaitOnExit, poUsePipes];
  AProcess.Execute;
  Writeln(n,' Execution Over: Exit Status: ',AProcess.ExitStatus);
  AStringList.Add('stdout:');
  AStringList.LoadFromStream(Aprocess.Output);
  AStringList.Add('stderr:');
  AStringList.LoadFromStream(AProcess.Stderr);
  AStringList.LoadFromStream(AProcess.Output);
  AStringList.SaveToFile('output.txt');
  writeln(AStringList.Text);
AstringList.Free;
AProcess.Free;
end; {for}
close(output);
close(input);
end.

Reply by email

Mark Morgan Lloyd
to Peter Brooks
Peter Brooks wrote 6 lines
Sorry, I misled you. I did move it higher up, earlier, to test something, and I forgot to put it back. This version has exactly the same behaviour: AStringList.LoadFromStream(AProcess.Output);

What is this replacing the current content with?

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email

Mattias Gaertner
to Peter Brooks: Sorry, I misled you. I did move it higher up, earlier, to test something, and I forgot to…
Show quoted text 21 lines
Sorry, I misled you. I did move it higher up, earlier, to test something, and I forgot to put it back. This version has exactly the same behaviour: -rw-r--r-- 1 peterbrooks wheel 0 21 May 13:00 output.txt Program nmp(input,output); uses Classes, SysUtils, Process; var AProcess: TProcess; AStringList: TStringList; n :Integer; begin { main } for n := 0 to 20 do begin { for } AStringList := TStringList.Create; AProcess := TProcess.Create(nil); AProcess.Executable := 'ping';

TProcess needs the full path: '/bin/ping'

TProcess needs to be extended to raise an exception in this case.

Show quoted text 18 lines
AProcess.Parameters.Add('-c 1'); AProcess.Parameters.Add('www.freepascal.org'); AProcess.Options := AProcess.Options + [poWaitOnExit, poUsePipes]; AProcess.Execute; Writeln(n,' Execution Over: Exit Status: ',AProcess.ExitStatus); AStringList.Add('stdout:'); AStringList.LoadFromStream(Aprocess.Output); AStringList.Add('stderr:'); AStringList.LoadFromStream(AProcess.Stderr); AStringList.LoadFromStream(AProcess.Output); AStringList.SaveToFile('output.txt'); writeln(AStringList.Text); AstringList.Free; AProcess.Free; end; {for} close(output); close(input); end.

Mattias

Reply by email

Peter Brooks
to Mattias Gaertner: On Wed, 21 May 2014 13:00:58 +0200 TProcess needs the full path: '/bin/ping' TProcess…

Unfortunately, I've tried both the full path and just the name - apparently TProcess is supposed to look in the path - and it does work. When it is called, it returns a '0' indicating successful completion.

I just tried it again with /sbin/ping instead of ping and the behaviour is just the same.

Is there an easier way to print the stream that comes back from TProcess? I think that the problem is in TStringList.

Mattias Gaertner wrote 52 lines
On Wed, 21 May 2014 13:00:58 +0200 Peter Brooks wrote: > Sorry, I misled you. I did move it higher up, earlier, to test > something, and I forgot to put it back. > > This version has exactly the same behaviour: > > -rw-r--r-- 1 peterbrooks wheel 0 21 May 13:00 output.txt > > Program nmp(input,output); > > uses > Classes, SysUtils, Process; > var > AProcess: TProcess; > AStringList: TStringList; > n :Integer; > begin { main } > for n := 0 to 20 do > begin { for } > AStringList := TStringList.Create; > AProcess := TProcess.Create(nil); > AProcess.Executable := 'ping'; TProcess needs the full path: '/bin/ping' TProcess needs to be extended to raise an exception in this case. > AProcess.Parameters.Add('-c 1'); > AProcess.Parameters.Add('www.freepascal.org'); > AProcess.Options := AProcess.Options + [poWaitOnExit, poUsePipes]; > AProcess.Execute; > Writeln(n,' Execution Over: Exit Status: ',AProcess.ExitStatus); > AStringList.Add('stdout:'); > AStringList.LoadFromStream(Aprocess.Output); > AStringList.Add('stderr:'); > AStringList.LoadFromStream(AProcess.Stderr); > AStringList.LoadFromStream(AProcess.Output); > AStringList.SaveToFile('output.txt'); > writeln(AStringList.Text); > AstringList.Free; > AProcess.Free; > end; {for} > close(output); > close(input); > end. Mattias _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply by email

Reinier Olislagers
to Peter Brooks
Peter Brooks wrote
Unfortunately, I've tried both the full path and just the name - apparently TProcess is supposed to look in the path - and it does work. When it is called, it returns a '0' indicating successful completion.

/disclaimer: haven't looked at your code/
voicechecker & fpcup contains a wrapper around TProcess[1] that lets you easily get output. See the convenience functions ExecuteCommand etc.

An example of use: pinging a host on OSX, can be found in voicechecker's pingtest.pas [2]

It works well for voicechecker, fpcup and some other projects

[1]
https://bitbucket.org/reiniero/voicechecker/src/01d4337c5ed948aeafe57d892d33db98c0a9f742/processutils.pas?at=default

[2]
https://bitbucket.org/reiniero/voicechecker/src/01d4337c5ed948aeafe57d892d33db98c0a9f742/pingtest.pas?at=default

Reply by email

Mark Morgan Lloyd
to Peter Brooks: Unfortunately, I've tried both the full path and just the name - apparently TProcess is…
Peter Brooks wrote 10 lines
Unfortunately, I've tried both the full path and just the name - apparently TProcess is supposed to look in the path - and it does work. When it is called, it returns a '0' indicating successful completion. I just tried it again with /sbin/ping instead of ping and the behaviour is just the same. Is there an easier way to print the stream that comes back from TProcess? I think that the problem is in TStringList.

I've already said that you're repeatedly overwriting the content.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email

Reinier Olislagers
to Reinier Olislagers: /disclaimer: haven't looked at your code/ voicechecker & fpcup contains a wrapper around…
Reinier Olislagers wrote 21 lines
On 21/05/2014 16:36, Peter Brooks wrote: > Unfortunately, I've tried both the full path and just the name - > apparently TProcess is supposed to look in the path - and it does > work. When it is called, it returns a '0' indicating successful > completion. /disclaimer: haven't looked at your code/ voicechecker & fpcup contains a wrapper around TProcess[1] that lets you easily get output. See the convenience functions ExecuteCommand etc. An example of use: pinging a host on OSX, can be found in voicechecker's pingtest.pas [2] It works well for voicechecker, fpcup and some other projects [1] https://bitbucket.org/reiniero/voicechecker/src/01d4337c5ed948aeafe57d892d33db98c0a9f742/processutils.pas?at=default [2] https://bitbucket.org/reiniero/voicechecker/src/01d4337c5ed948aeafe57d892d33db98c0a9f742/pingtest.pas?at=default

Perhaps you need to further analyze the output when you get result code 0 as the ping command may have succeeded but the hosts were unreachable. Voicechecker's voicecheckermain.pas uses this when calling pingtest:

function TForm1.AnalyzeUnixPing(OutputText: TStringList): boolean;
begin
  result:=false;
  if (pos(' 0.0% packet loss',OutputText.Text)>0) then //OSX
    result:=true
  else if (pos(' 0% packet loss',OutputText.Text)>0) then //Linux
    result:=true;
end;

and

  ATest:=TRemoteCommandTest.Create;
  ATest.TestName:='RouterInternetUp';
... details ...
  TRemoteCommandTest(ATest).Command:='ping -c 1 www.google.com; echo "";
echo ""';
  TRemoteCommandTest(ATest);
... more code...

Reply by email

waldo kitty
to Peter Brooks: Sorry, I misled you. I did move it higher up, earlier, to test something, and I forgot to…
Peter Brooks wrote
AStringList.LoadFromStream(Aprocess.Output); AStringList.LoadFromStream(AProcess.Stderr); AStringList.LoadFromStream(AProcess.Output);

[quote]
LoadFromStream loads the contents of Stream into the stringlist. Each line in the stream (as marked by the end-of-line marker of the particular OS the application runs on) becomes one string in the stringlist. This action replaces the contents of the stringlist, it does not append the strings to the current content.
[/quote]

those LoadFromStream calls are overwriting the contents of your stringlist... note the last sentence in the above quote from
http://www.freepascal.org/docs-html/rtl/classes/tstrings.loadfromstream.html

you need to figure out how to run through your streams and AStringList.Add() the entries to your stringlist

*OR*

you need to have another set of stringlists for those process outputs and then use AStringList.AddStrings()

*OR*

you need to AStringList.Assign() another string list, print/save it, AStringList.Assign() the next string list, print/save it and so on... as LoadFromStream, Assign replaces the current contents of AStringList with the new contents...

FWIW: if you are using the "improved example" on
http://wiki.freepascal.org/Executing_External_Programs then you should note that it does state that it is flawed and that you should use the code in the next example "reading large output" which waits till the end and then loads the stream into the stringlist and then you can manipulate it from there...

NOTE: No off-list assistance is given without prior approval. Please *keep mailing list traffic on the list* unless private contact is specifically requested and granted.

Reply by email

Peter Brooks
to waldo kitty

Yes, thank you. The best thing I've learned is to be suspicious of examples...

I've got it now, thank you all for the assistance.

I took AStringList.Add() to mean that it added the string to the list of strings - I was wrong..

I did note that the example said that it was flawed, but, foolishly, as it turned out, I thought it meant 'flawed' rather than 'doesn't work at all'.

waldo kitty wrote 49 lines
On 5/21/2014 7:00 AM, Peter Brooks wrote: > > AStringList.LoadFromStream(Aprocess.Output); > AStringList.LoadFromStream(AProcess.Stderr); > AStringList.LoadFromStream(AProcess.Output); [quote] LoadFromStream loads the contents of Stream into the stringlist. Each line in the stream (as marked by the end-of-line marker of the particular OS the application runs on) becomes one string in the stringlist. This action replaces the contents of the stringlist, it does not append the strings to the current content. [/quote] those LoadFromStream calls are overwriting the contents of your stringlist... note the last sentence in the above quote from http://www.freepascal.org/docs-html/rtl/classes/tstrings.loadfromstream.html you need to figure out how to run through your streams and AStringList.Add() the entries to your stringlist *OR* you need to have another set of stringlists for those process outputs and then use AStringList.AddStrings() *OR* you need to AStringList.Assign() another string list, print/save it, AStringList.Assign() the next string list, print/save it and so on... as LoadFromStream, Assign replaces the current contents of AStringList with the new contents... FWIW: if you are using the "improved example" on http://wiki.freepascal.org/Executing_External_Programs then you should note that it does state that it is flawed and that you should use the code in the next example "reading large output" which waits till the end and then loads the stream into the stringlist and then you can manipulate it from there... -- NOTE: No off-list assistance is given without prior approval. Please *keep mailing list traffic on the list* unless private contact is specifically requested and granted. _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply by email

Mattias Gaertner
to Peter Brooks
Show quoted text 10 lines
Yes, thank you. The best thing I've learned is to be suspicious of examples... I've got it now, thank you all for the assistance. I took AStringList.Add() to mean that it added the string to the list of strings - I was wrong.. I did note that the example said that it was flawed, but, foolishly, as it turned out, I thought it meant 'flawed' rather than 'doesn't work at all'.

Do you mean this example:
http://wiki.freepascal.org/Executing_External_Programs#An_improved_example_.28but_not_correct_yet.29

It works here.
What is wrong?

Mattias

Reply by email

Marco van de Voort
to Mattias Gaertner: On Wed, 21 May 2014 21:44:10 +0200 Do you mean this example:…

In our previous episode, Mattias Gaertner said:

Show quoted text 9 lines
> I did note that the example said that it was flawed, but, foolishly, > as it turned out, I thought it meant 'flawed' rather than 'doesn't > work at all'. Do you mean this example: http://wiki.freepascal.org/Executing_External_Programs#An_improved_example_.28but_not_correct_yet.29 It works here. What is wrong?

Not using

http://wiki.freepascal.org/Executing_External_Programs#.28Process..29RunCommand

:-)

Reply by email