在java中使用openchannel无法访问远程机器的cmd提示符。

问题描述 投票:0回答:1
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时,命令给出了用户路径(不是以管理员身份),即;C:\Users/AdminexecuteBatchFile.bat。

你能不能给我一些解决方法?

java jsch
1个回答
0
投票
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提示符(我只用管理员凭证登录,但还是不能在管理员模式下打开).这里我需要在管理员模式下运行脚本。

© www.soinside.com 2019 - 2024. All rights reserved.