Unix command Execution with output problem. lazarus / 21-General ---------------------------------------------------------------------- BSaidus · 2023-02-11 12:10 Hello. Given the following OpenBSD unix command line: Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i /usr/bin/netstat -in -f inet|awk '{ print $1,"\t",$3,"\t",$4; }'|grep -v -e lo0 -e enc -e pflog -e tun|grep Link|sed 's/*//'|awk '{ if( system("[ ! -f /var/run/dmesg.boot ]") == 0) system("dmesg >/var/run/dmesg.boot"); cmd="grep ^"$1" /var/run/dmesg.boot"; while((cmd|getline drv0)>0) {$4=drv0} close(cmd); print $1,"\t",$3,"\t",$4;}' after execution it will display following Code: [Select]pcn0     08:00:27:0a:65:2d       pcn0 at pci0 dev 3 function 0 "AMD 79c970 PCnet-PCI" rev 0x10, Am79c970A, rev 0: apic 2 int 19, address 08:00:27:0a:65:2d pcn1     08:00:27:78:3c:29       pcn1 at pci0 dev 8 function 0 "AMD 79c970 PCnet-PCI" rev 0x40, Am79c973, rev 0: apic 2 int 16, address 08:00:27:78:3c:29 em0      08:00:27:8d:4a:c6       em0 at pci0 dev 9 function 0 "Intel 82540EM" rev 0x02: apic 2 int 17, address 08:00:27:8d:4a:c6 em1      08:00:27:00:0c:c8       em1 at pci0 dev 10 function 0 "Intel 82543GC" rev 0x02: apic 2 int 18, address 08:00:27:00:0c:c8 I made a wrapper for the process.runcommand function and try to execute this command as follow Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function fpExecCmd(const ACmd: String; const AArgs: array of string): String;begin  if not process.RunCommand( ACmd, AArgs, Result ) then  begin    Result := 'Error Message';  end;end;    Result := fpExecCmd( '/usr/bin/netstat', ['-in', '-f', 'inet',                '|','awk' , '''{ print $1,"\t",$3,"\t",$4; }''',                '|','grep', '-v', '-e', 'lo0', '-e', 'enc', '-e', 'pflog', '-e', 'tun',                '|','grep', 'Link',                '|','sed' , '''s/*//''',              '|','awk' , '''{ if( system("[ ! -f /var/run/dmesg.boot ]") == 0) system("dmesg >/var/run/dmesg.boot"); cmd="grep ^"$1" /var/run/dmesg.boot"; while((cmd|getline drv0)>0) {$4=drv0} close(cmd); print $1,"\t",$3,"\t",$4;}'''] ); var  LRet: String ;begin  LRet := fpExecCmdNetStat();  WriteLn(LRet); end. The execution gives the 'Error Message'. I've tried different combination but ... not successful. Any one have an idea ?? Thanks. ---------------------------------------------------------------------- BSaidus · 2023-02-11 12:25 Hi, Find one solution but, do not like to me. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function fpExecCmdNetStatSh(): String;const  CNetSh: String = 'netstat -in -f inet|awk ''{ print $1,"\t",$3,"\t",$4; }''|grep -v -e lo0 -e enc -e pflog -e tun|grep Link|sed ''s/*//''|awk ''{ if( system("[ ! -f /var/run/dmesg.boot ]") == 0) system("dmesg >/var/run/dmesg.boot"); cmd="grep ^"$1" /var/run/dmesg.boot"; while((cmd|getline drv0)>0) {$4=drv0} close(cmd); print $1,"\t",$3,"\t",$4;}''';var  Ltemp: TStringList;  Lstr: String;begin   Ltemp := TStringList.Create;   Lstr := '#!/bin/sh' + LineEnding + CNetSh;   try     Ltemp.Text := Lstr;   Ltemp.SaveToFile('t.sh');     Result := fpExecCmd('/bin/sh', ['t.sh']);   finally     Ltemp.Free;     DeleteFile('t.sh');   end;end; var  LRet: String ;begin  LRet := fpExecCmdNetStatSh();  WriteLn(LRet);end.  It works, but ... I seek the way to run it using function offred by process unit. ---------------------------------------------------------------------- TRon · 2023-02-11 12:43 Quote from: BSaidus on February 11, 2023, 01:25:05 pmIt works, but ... I seek the way to run it using function offred by process unit. If you wish to use the functionality from TProcess then simply use it ? The wiki is full of examples. If something is not working for you when using tprocess then you really have to state what is not working for you. You can use the exact same commands with tprocess as you did with fpexeccmd. PS if you rely on shell internals then the only way to solve it is by using the shell. Otherwise you can omit the part where you save it to a script-file. edit: PPS: your 'working' solution does not work for me on a linux system, thus sorry to say I can't test for you. What system/OS do you use ? ---------------------------------------------------------------------- marcov · 2023-02-11 14:31 If you want to have shell interpretation in your commandlines, you must call a shell interpreter to interpret it. This includes piping like > etc.  Otherwise it goes straight to the kernel, without any interpretation. So try: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   Result := fpExecCmd( '/bin/sh', ['-c',wh/usr/bin/netstat and whole rest of commandline in single string']) ---------------------------------------------------------------------- PierceNg · 2023-02-11 14:35 Quote from: BSaidus on February 11, 2023, 01:25:05 pmCode: [Select]'netstat -in -f inet|awk ''{ print $1,"\t",$3,"\t",$4; }''|grep -v -e lo0 -e enc -e pflog -e tun|grep Link|sed ''s/*//''|awk ''{ if( system("[ ! -f /var/run/dmesg.boot ]") == 0) system("dmesg >/var/run/dmesg.boot"); cmd="grep ^"$1" /var/run/dmesg.boot"; while((cmd|getline drv0)>0) {$4=drv0} close(cmd); print $1,"\t",$3,"\t",$4;}'' It works, but ... I seek the way to run it using function offred by process unit. First, the way the original command is constructed, you'll need to feed it to a shell. Each of the awk, grep, sed etc is a process by itself. To run the script without shell, your program will have to do what the shell does: fork/exec each process and join up their stdin/stdout using pipes, i.e., work like a shell. So might as well just use the shell to run your script. Second, it should be possible to write a single awk program that does all the awk/grep/sed stuff so your process pipeline should simply be | . And from there, you could implement what the single awk program does in Pascal. Then, you could use TProcess to run the netstat command and process its output in your Pascal program. But I have a question: Why not just run t.sh? Why replicate the work in Pascal? ---------------------------------------------------------------------- TRon · 2023-02-11 14:38 @BSaidus: As marcov wrote, but on a Linux system I get stuck here: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program test; {$mode objfpc}{$H+} uses  sysutils, classes, process; function fpExecCmd(const ACmd: String; const AArgs: array of string): String;begin  if not process.RunCommand( ACmd, AArgs, Result ) then  begin    Result := 'Error Message';  end;end; //   CNetSh: String = 'netstat -in -f inet|awk ''{ print $1,"\t",$3,"\t",$4; }''|grep -v -e lo -e enc -e pflog -e tun|grep Link|sed ''s/*//''|awk ''{ if( system("[ ! -f /var/run/dmesg.boot ]") == 0) system("dmesg >/var/run/dmesg.boot"); cmd="grep ^"$1" /var/run/dmesg.boot"; while((cmd|getline drv0)>0) {$4=drv0} close(cmd); print $1,"\t",$3,"\t",$4;}''';function phase1: string;var  ret: string absolute result;  cmd: string;begin cmd := 'netstat -in -f inet | awk ''{ print $1,"\t",$3,"\t",$4 }'' | grep -v -e lo0 -e enc -e pflog -e tun'; ret := fpExecCmd('/usr/bin/bash', ['-c', cmd]);end; var  Lines: TStringList;  Line : string;begin  Lines := TStringList.Create;  Lines.Text := phase1;  for Line in Lines do WriteLn(Line);  Lines.Free;end. (* outputKernel   tableIface    RX-OK   RX-ERReth0   87205   0eth1     0       0lo       24      0*) .. because there is no "Link" to grep for me. ---------------------------------------------------------------------- Thaddy · 2023-02-11 15:27 It would also be nice if Process would be created...? ---------------------------------------------------------------------- BSaidus · 2023-02-13 08:22 Thank you all for advices. I take the advice of @marcov. It works well. ---------------------------------------------------------------------- marcov · 2023-02-13 08:26 Quote from: Thaddy on February 11, 2023, 04:27:17 pmIt would also be nice if Process would be created...? It is an unit ?