我试图通过openSSH从linux机器连接到windows机器,并在windows框上运行一些powershell命令。由于一些限制,我无法在linux机器上安装powershell。
从linux手动启动openSSH,然后运行命令,效果非常好。
我试图在Java中做同样的事情,但问题是我没有看到powershell命令的输出运行。
下面是复制相同代码的代码:
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.发布于 2021-06-04 15:16:08
交互模式不适合脚本和代码,也不推荐。最好使用一些现有的ssh框架(如jsch )来连接,这些框架可以作为开源maven依赖项使用。
https://stackoverflow.com/questions/67608216
复制相似问题