无法在 java 中使用 openchannel 访问远程计算机中的 cmd 提示符
Unable to access the cmd prompt in remote machine using openchannel in java
I'm trying to access the cmd prompt in administrator mode and run a batch file in the remote machine,but right now I'm not able to access the cmd prompt through openchannel. Did anybody tried to access it from remote machine in java?
这是代码
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
JSch jsch = new JSch();
session = jsch.getSession(user, ip, 22);
session.setPassword(password);
session.setTimeout(timeOut);
session.setConfig(config);
session.connect();
System.out.println("session connected");
//open command prompt to run the command = "C:\executeBatchFile.bat" file
Channel channel = (ChannelExec) session.openChannel("exec");
((ChannelExec)channel).setCommand("cmd.exe /c \"echo %cd%\"\executeBatchFile.bat");
channel.connect();
InputStream outputstream_from_the_channel = channel.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(outputstream_from_the_channel));
String jarOutput;
while ((jarOutput = reader.readLine()) != null)
{
System.out.println("Inside while loop");
System.out.println(jarOutput + "\n");
}
reader.close();
session.disconnect();
预期行为:set 命令应该 运行 作为管理员(虽然我已经以管理员身份登录),返回到 c:drive (cd) 并执行批处理文件即; C:executeBatchFile.bat
实际行为:当我打印 jarOutput 时,命令给出了用户路径(不是管理员)。 IE; C:\Users\Admin\executeBatchFile.bat
你能就此提出任何解决方案吗?
This has been resolved using PsExec command instead of JSCH
String pscommand=E:\Tool\psexec -u user -p pwd \\ip -s -d cmd.exe /c C:\executescript.bat
process = Runtime.getRuntime().exec(pscommand);
InputStream es = process.getErrorStream();
BufferedReader errReader = new BufferedReader(new InputStreamReader(es));
String line;
// Read STDOUT into a buffer.
while ((line = errReader.readLine()) != null)
{
system.out.println(line);
}
谁能告诉我如何在管理员模式下打开cmd提示符(我只使用管理员凭据登录但仍然无法在管理员模式下打开)。这里我需要运行管理员中的脚本。
I'm trying to access the cmd prompt in administrator mode and run a batch file in the remote machine,but right now I'm not able to access the cmd prompt through openchannel. Did anybody tried to access it from remote machine in java?
这是代码
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
JSch jsch = new JSch();
session = jsch.getSession(user, ip, 22);
session.setPassword(password);
session.setTimeout(timeOut);
session.setConfig(config);
session.connect();
System.out.println("session connected");
//open command prompt to run the command = "C:\executeBatchFile.bat" file
Channel channel = (ChannelExec) session.openChannel("exec");
((ChannelExec)channel).setCommand("cmd.exe /c \"echo %cd%\"\executeBatchFile.bat");
channel.connect();
InputStream outputstream_from_the_channel = channel.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(outputstream_from_the_channel));
String jarOutput;
while ((jarOutput = reader.readLine()) != null)
{
System.out.println("Inside while loop");
System.out.println(jarOutput + "\n");
}
reader.close();
session.disconnect();
预期行为:set 命令应该 运行 作为管理员(虽然我已经以管理员身份登录),返回到 c:drive (cd) 并执行批处理文件即; C:executeBatchFile.bat
实际行为:当我打印 jarOutput 时,命令给出了用户路径(不是管理员)。 IE; C:\Users\Admin\executeBatchFile.bat
你能就此提出任何解决方案吗?
This has been resolved using PsExec command instead of JSCH
String pscommand=E:\Tool\psexec -u user -p pwd \\ip -s -d cmd.exe /c C:\executescript.bat
process = Runtime.getRuntime().exec(pscommand);
InputStream es = process.getErrorStream();
BufferedReader errReader = new BufferedReader(new InputStreamReader(es));
String line;
// Read STDOUT into a buffer.
while ((line = errReader.readLine()) != null)
{
system.out.println(line);
}
谁能告诉我如何在管理员模式下打开cmd提示符(我只使用管理员凭据登录但仍然无法在管理员模式下打开)。这里我需要运行管理员中的脚本。