【发布时间】:2015-01-22 06:21:19
【问题描述】:
我想编写一个 java 程序来检索在不同服务器上运行的所有服务的状态(大约 20 个)。为此,我使用的是 SC 命令,我可以使用 java 程序来做到这一点。但是现在我陷入了一种情况,我想通过使用 RUNAS 以其他用户身份运行 SC 命令,我面临的问题是,一旦第一次执行命令,我就无法输入密码.以下是我正在使用的代码:-
String[] command = new String[3];
command[0] = "cmd";
command[1] = "/c";
command[2] = "runas /noprofile /user:domain\\admin \"sc \\\\serverName queryex type= service state= all\"";
Process p = Runtime.getRuntime().exec(command);
PrintWriter writer = new PrintWriter(new OutputStreamWriter(new BufferedOutputStream(p.getOutputStream())), true);
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = reader.readLine();
while (line != null) {
new PrintWriter(p.getOutputStream(),true).println("AdminPassword");
System.out.println(line);
line = reader.readLine();
}
BufferedReader stdInput = new BufferedReader(newInputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
String Input;
while ((Input = stdInput.readLine()) != null) {
System.out.println(Input);
}
String Error;
while ((Error = stdError.readLine()) != null) {
System.out.println(Error);
}
但我无法打印所有服务的状态。提供密码后我不确定是否需要捕获其他流??
有什么帮助吗?
谢谢
【问题讨论】:
-
一个安全问题可能会影响您,这里的大部分答案可能是:如果您通过命令行提供凭据,则在使用任务管理器或查看进程列表时,它们可能以纯文本形式提供类似的东西。