首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过openSSH在远程计算机上的Powershell命令

通过openSSH在远程计算机上的Powershell命令
EN

Stack Overflow用户
提问于 2021-05-19 17:36:34
回答 1查看 63关注 0票数 0

我试图通过openSSH从linux机器连接到windows机器,并在windows框上运行一些powershell命令。由于一些限制,我无法在linux机器上安装powershell。

从linux手动启动openSSH,然后运行命令,效果非常好。

我试图在Java中做同样的事情,但问题是我没有看到powershell命令的输出运行。

下面是复制相同代码的代码:

代码语言:javascript
复制
public class Example {
  public static class Writers exte ds Thread {
    Process process;
    Writers(Process p) {
      process =p;
    }
 
  public void run() {
    try {
      OutputStreamWriter writer= new OutputStreamWriter(process.getOutputStream);
      String command = "expect -c 'spawn ssh user@host ; expect \"password\" ; send \"passcode\\r\"; interact' \n";
      writer.write(command);
      writer.write("echo \"hello123\");
      writer.flush();
    } catch (Exception e) {}
    }
    
    public static void main(String[] args) throws Exception {
        ProcessBuilder builder = new ProcessBuilder("bash");
        builder.redirectErrorStream(true);
        Process process = builder.start();
        Example.Writers writers = new Example.Writers(process);
        writers.start();

        BufferedReader stdout = new BufferedReader( new InputStreamReader(process.getInputStream()));
        String s = null;
        while((s= stdout.readLine()) != null) {
           System.out.println(s);
        }
    }
}

OUTPUT:
I see only Windows powershell startup logo and nothing after that.

Tell me why this hello123 not getting printed.
Or, why the powershell output stream not getting redirected to output stream of linux machine ?

manually opening interactive ssh shell and running powershell commands run perfectly, but not via code.
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-04 15:16:08

交互模式不适合脚本和代码,也不推荐。最好使用一些现有的ssh框架(如jsch )来连接,这些框架可以作为开源maven依赖项使用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67608216

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档