Execute Server Commands from Caché / Ensemble / IRIS

The question has come up several times and I saw mixed answers and no quick example

My personal preference is using CPIPE device as you get back exactly the output you will get at the command line interface of your OS .
The tricky thing is to stop reading in time.
The example just displays what you normally see in your console.
it becomes useful if you look for things that you can't get from any $system.whatever()

e.g.
- your servers IP address or addresses depending on your configuration.  
- pinging any other server to see if it is still visible on the network
- running nslookup 

it is simply this:

 zpipe  quit ; CPIPE example to run host console command
cmd(command="",test=0) ;
 if command="" set command="dir"
 quit $$execute(command,test)

execute(cmd,test) Public {
 set dev="|CPIPE|1"
 set $zt="cls"
 set empty=0
 open dev:cmd:0
 write:test $test,!
 else  write "pipe failed",! quit 0
 while empty<3 {
    use dev read line 
    set empty=$s($l(line):0,1:$i(empty))
    use 0 write line,!  ;;; or do any kind of analysis of the line 
    }

cls ;
 set $zt="" use 0
 close dev 
 if $ze'["<ENDOFFILE>" $ze,!
 quit $t
 }
 

..

Just runUSER>do cmd^zpipe() to see if you are allowed to use PIPES
(I just met a narrow-minded security manager that didn't allow the use of pipes to Caché) 

or 

USER>do cmd^zpipe("tracert community.intersystems.com")

Tracing route to community.intersystems.com [54.83.203.138] over a maximum of 30 hops:
 
  1     3 ms     1 ms     1 ms  10.10.1.1
  2     *        *        *     Request timed out.
  3   116 ms   168 ms   132 ms  at-vie01b-rc1-ae11-0.aorta.net [84.116.228.201]
  4   142 ms   114 ms   115 ms  at-vie05d-rc1-ae29-0.aorta.net [84.116.140.2]
  5     *        *        *     Request timed out.
  6   114 ms   114 ms   115 ms  us-nyc01b-rd2-ae9-0.aorta.net [84.116.140.170]
  7   118 ms   116 ms   117 ms  us-was03a-rd1-ae5-0.aorta.net [84.116.146.142]
  8   114 ms   115 ms   114 ms  us-was03a-ri1-ae11-0.aorta.net [84.116.130.165]
  9   114 ms   116 ms   114 ms  213.46.182.202
 10   145 ms   132 ms   132 ms  54.239.111.240
 11   115 ms   115 ms   114 ms  54.239.110.152
 12   126 ms   136 ms   132 ms  54.239.110.149
 13   115 ms   116 ms   114 ms  54.239.108.133
 14   129 ms   148 ms   133 ms  52.93.24.94
 15   115 ms   114 ms   117 ms  52.93.24.85
 16     *        *        *     Request timed out.
 17     *        *        *     Request timed out.
 18     *        *        *     Request timed out.
 19     *        *        *     Request timed out.
 20     *        *        *     Request timed out.
 21   429 ms   121 ms   114 ms  ec2-54-83-203-138.compute-1.amazonaws.com [54.83.203.138]

Trace complete.

download code

Comments

Another variants:

USER>k  s args=3,args(1)="-h",args(2)=21,args(3)="community.intersystems.com" do $system.OBJ.DisplayError(##class(%Net.Remote.Utility).RunCommandViaCPIPE("tracert",,.output,,900,.args)) output

or

USER>k  s args=3,args(1)="-h",args(2)=21,args(3)="community.intersystems.com" do $system.OBJ.DisplayError(##class(%Net.Remote.Utility).RunCommandViaZF("tracert",,.output,,900,,.args)) output

Last parameter  .args came only with 2017.1
for earlier versions you need to compose it into first argument.