我尝试过以下代码。但腻子正在启动并立即关闭。在command.txt中包含ls -lrt代码。
Runtime r = Runtime.getRuntime();
//Runtime r2 = Runtime.getRuntime();
Process p = null;
//Process p2 = null;
String s = "D:\\Nandan\\putty.exe -ssh -l ***** -pw ******** XX.XX.XX.XX -m D:\\Nandan\\command.txtx";
//String s2 = "ls -lrt";
try
{
p = r.exec(s);
p.waitFor();
} catch (Exception e)
{
System.out.println("Exception error :"+e.getMessage());
e.printStackTrace();
}
最佳答案
Putty 是 ssh 客户端,因此您可以直接使用 java ssh 库 JSch.jar 在 Linux 机器上执行任何操作,而不是调用 putty。下面是相同的示例代码
Session session = new JSch().getSession(user, hostName, 22);
session.setPassword(password);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
Channel channel=session.openChannel("exec");
((ChannelExec)channel).setCommand("some command here");
String result = IOUtils.toString(channel.getInputStream());
channel.disconnect();
session.disconnect();
要了解更多信息,请访问链接http://www.jcraft.com/jsch/examples/
关于java - 如何通过Java代码连接putty并将命令传递到终端?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37132456/