Script to Run a Command

From Observer GigaFlow Support | VIAVI Solutions Inc.
Jump to: navigation, search

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

var BufferedReader = Java.type('java.io.BufferedReader');
var InputStreamReader= Java.type('java.io.InputStreamReader');
var ProcessBuilder= Java.type('java.lang.ProcessBuilder');
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")