Pstools是一系列工具的集合 ,用於進行本機和遠程機器的管理。工具包中其中包括了psexec,psfile,psgetsid,psinfo等使用工具,你可以在http://www.microsoft.com/technet/sysinternals/FileAndDisk/PsTools.mspx
進行下載,裏面還包括了一個chm格式的文檔。
下面是一個簡單的使用java exec命令執行psexec的程序。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class PSCaller {
private final static String callSuccess = "with process ID";
/**
* Use a process to execute a command
*
* @param cmd
* @return exit value
* @throws Exception
*/
public int execute(String cmd) throws Exception {
int exit_code = -1;
String ls_1;
Process p = Runtime.getRuntime().exec(cmd);
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(p.getErrorStream()));
while ((ls_1 = bufferedReader.readLine()) != null) {
System.out.println("## " + ls_1);
if (ls_1.toLowerCase().contains(callSuccess.toLowerCase())) {
// this.setRunnerStatus(Short.valueOf("4"));
exit_code = 0;
}
}
p.waitFor();
return exit_code;
}
/**
*
* @return pstool command
* @throws Exception
*/
public String createCmd() throws Exception {
String cmd = "";
String host = "hostname";
// String host = "localhost";
String bat = "notepad.exe";
String session = "2";
String user = "autouser";
String password = "passwd";
cmd += "cmd /c /"C://Program Files//pstools//psexec/" ////";
cmd += host;
cmd += " -u ";
cmd += user;
cmd += " -p ";
cmd += password;
cmd += " -i ";
cmd += session;
cmd += " -d ";
cmd += bat;
return cmd;
}
public static void main(String[] args) {
PSCaller caller = new PSCaller();
String cmd;
int exitnum = -1;
try {
cmd = caller.createCmd();
System.out.println(cmd);
exitnum = caller.execute(cmd);
System.out.println("Main--" + exitnum);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}