Difference between revisions of "Script to Run a Command"

From Observer GigaFlow Support | VIAVI Solutions Inc.
Jump to: navigation, search
Line 1: Line 1:
var ProcessBuilder = Java.type('java.lang.ProcessBuilder');
+
[[Category:Scripting Notes]]
 +
[[Category:Technical Notes]]
 +
var ProcessBuilder = Java.type('java.lang.ProcessBuilder');
 
  var BufferedReader = Java.type('java.io.BufferedReader');
 
  var BufferedReader = Java.type('java.io.BufferedReader');
 
  var InputStreamReader= Java.type('java.io.InputStreamReader');
 
  var InputStreamReader= Java.type('java.io.InputStreamReader');

Revision as of 15:27, 12 October 2018

var ProcessBuilder = Java.type('java.lang.ProcessBuilder');

var BufferedReader = Java.type('java.io.BufferedReader');
var InputStreamReader= Java.type('java.io.InputStreamReader');
output.append(data);
 try {
   // Use a ProcessBuilder
   var pb = new ProcessBuilder("ls","-lrt","/");
   var p = pb.start();
   var is = p.getInputStream();
   var br = new BufferedReader(new InputStreamReader(is));
   var line = null;
   while ((line = br.readLine()) != null) {
     output.append(line);
   }
   var r = p.waitFor(); // Let the process finish.
   if (r == 0) { // No error
      // run cmd2.
   }
 } catch ( e) {
   output.append(e.printStackTrace());
 }
log.warn("end")