1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * ident "%Z%%M% %I% %E% SMI" 23 * 24 * Copyright (c) 1999 by Sun Microsystems, Inc. 25 * All rights reserved. 26 * 27 * SysCommand 28 * Execute a command and capture stdout/stderr. 29 * 30 */ 31 32 package com.sun.admin.pm.server; 33 34 import java.io.*; 35 36 public class SysCommand 37 { 38 39 private Process p = null; 40 private String out = null; 41 private String err = null; 42 private int status = 0; 43 44 public static void main(String[] args) { 45 SysCommand syscmd = new SysCommand(); 46 String cmd = "ypcat hosts"; 47 String o = ""; 48 try { 49 syscmd.exec(cmd); 50 } 51 catch (Exception e) { 52 System.out.println(e); 53 } 54 o = syscmd.getOutput(); 55 System.out.println(o); 56 } 57 58 /* 59 * Execute a system command. 60 * @param String cmd The command to be executed. 61 */ 62 public void exec(String cmd) throws Exception 63 { 64 if (cmd == null) { 65 throw new pmInternalErrorException( 66 "SysCommand.exec(): null command"); 67 } 68 69 debug_log(cmd); 70 71 p = Runtime.getRuntime().exec(cmd); 72 if (p == null) { 73 throw new pmInternalErrorException( 74 "SysCommand.exec(): null process"); 75 } 76 out = readOut(); 77 err = readErr(); 78 p.waitFor(); 79 status = getStatus(); 80 dispose(); 81 } 82 83 public void exec(String[] cmd) throws Exception 84 { 85 if (cmd == null) { 86 throw new pmInternalErrorException( 87 "SysCommand.exec(): null command"); 88 } 89 90 // Trim command arrays with nulls at the end. 91 int i; 92 for (i = 0; i < cmd.length; i++) { 93 if (cmd[i] == null) { 94 break; 95 } 96 } 97 if (i != cmd.length) { 98 String[] newcmd = new String[i]; 99 100 for (i = 0; i < newcmd.length; i++) { 101 newcmd[i] = cmd[i]; 102 } 103 debug_log(PrinterDebug.arr_to_str(newcmd)); 104 p = Runtime.getRuntime().exec(newcmd); 105 } else { 106 debug_log(PrinterDebug.arr_to_str(cmd)); 107 p = Runtime.getRuntime().exec(cmd); 108 } 109 if (p == null) { 110 throw new pmInternalErrorException( 111 "SysCommand.exec(): null process"); 112 } 113 out = readOut(); 114 err = readErr(); 115 p.waitFor(); 116 status = getStatus(); 117 dispose(); 118 } 119 120 121 public void exec(String cmd, String locale) throws Exception 122 { 123 if (cmd == null) { 124 throw new pmInternalErrorException( 125 "SysCommand.exec(): null command"); 126 } 127 128 debug_log(locale + "; " + cmd); 129 130 String [] envp = new String[1]; 131 envp[0] = locale; 132 p = Runtime.getRuntime().exec(cmd, envp); 133 if (p == null) { 134 throw new pmInternalErrorException( 135 "SysCommand.exec(): null process"); 136 } 137 out = readOut(); 138 err = readErr(); 139 p.waitFor(); 140 status = getStatus(); 141 dispose(); 142 } 143 144 public String getOutput() { 145 if (out == null) 146 return (null); 147 return (new String(out)); 148 } 149 public String getError() { 150 if (err == null) 151 return (null); 152 return (new String(err)); 153 } 154 public int getExitValue() { 155 return (status); 156 } 157 158 159 private String readOut() throws Exception 160 { 161 String result = null; 162 String line = null; 163 BufferedReader out = null; 164 165 out = new BufferedReader( 166 new InputStreamReader(p.getInputStream())); 167 while ((line = out.readLine()) != null) { 168 if (result == null) 169 result = line; 170 else 171 result = result.concat("\n" + line); 172 } 173 return (result); 174 } 175 176 private String readErr() throws Exception 177 { 178 String errstr = null; 179 String line = null; 180 BufferedReader err = null; 181 182 err = new BufferedReader( 183 new InputStreamReader(p.getErrorStream())); 184 while ((line = err.readLine()) != null) { 185 if (errstr == null) { 186 errstr = line; 187 } else { 188 errstr = errstr.concat("\n" + line); 189 } 190 } 191 return (errstr); 192 } 193 194 private int getStatus() throws Exception 195 { 196 return (p.exitValue()); 197 } 198 199 /* 200 * Clean up opened file descriptors. 201 */ 202 private void dispose() { 203 204 try { 205 p.getInputStream().close(); 206 p.getOutputStream().close(); 207 p.getErrorStream().close(); 208 p.destroy(); 209 } 210 catch (Exception e) { 211 Debug.message("SVR:" + e.getMessage()); 212 } 213 } 214 215 /* 216 * Log all commands as is except lpset with a password. 217 */ 218 private void debug_log(String cmd) 219 { 220 if ((cmd.indexOf("lpset") != -1) && 221 (cmd.indexOf(" -w ") != -1)) { 222 String clean = ""; 223 int i = cmd.indexOf(" -w "); 224 int j = 0; 225 226 try { 227 i += 4; 228 clean = cmd.substring(0, i); 229 clean = clean.concat("**** "); 230 while (cmd.charAt(i) != ' ') { 231 i++; 232 } 233 } catch (Exception e) { 234 Debug.message("SVR: lpset command with a passwd."); 235 return; 236 } 237 238 clean = clean.concat(cmd.substring(i, cmd.length())); 239 Debug.message("SVR: " + clean); 240 241 } else { 242 Debug.message("SVR: " + cmd); 243 } 244 } 245 } 246