繁体   English   中英

无法使用 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 提示符(我仅使用管理员凭据登录,但仍然无法在管理员模式下打开)。这里我需要以管理员身份运行脚本。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM