如何通过Java代码连接putty并将命令传递给终端?
我试过下面的代码。但腻子正在启动并立即关闭。在command.txt中包含ls-lrt代码如何通过Java代码连接putty并将命令传递给终端?,java,unix,putty,Java,Unix,Putty,我试过下面的代码。但腻子正在启动并立即关闭。在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:
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();
要了解更多信息,请通过链接I success使其成为那样
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Dictionary;
import java.util.Hashtable;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
public static void puttyConnection(String user, String hostName, String password)
throws JSchException, IOException {
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("shell");
OutputStream ops = channel.getOutputStream();
PrintStream ps = new PrintStream(ops, true);
channel.connect();
InputStream input = channel.getInputStream();
ps.println("Put Here your command");
ps.println("Put Here another command");
ps.close();
try {
printResult(input, channel);
} catch (Exception e) {
e.printStackTrace();
}
channel.disconnect();
session.disconnect();
}
关于函数printResult
private static void printResult(InputStream input, Channel channel) throws Exception {
int SIZE = 1024;
byte[] tmp = new byte[SIZE];
while (true) {
while (input.available() > 0) {
int i = input.read(tmp, 0, SIZE);
if (i < 0)
break;
System.out.print(new String(tmp, 0, i));
}
if (channel.isClosed()) {
System.out.println("exit-status: " + channel.getExitStatus());
break;
}
try {
Thread.sleep(300);
} catch (Exception ee) {
}
}
}
private static void printResult(InputStream输入,通道通道)引发异常{
int SIZE=1024;
字节[]tmp=新字节[大小];
while(true){
while(input.available()>0){
int i=输入读取(tmp,0,大小);
if(i<0)
打破
系统输出打印(新字符串(tmp,0,i));
}
if(channel.isClosed()){
System.out.println(“退出状态:+channel.getExitStatus());
打破
}
试一试{
睡眠(300);
}捕获(异常ee){
}
}
}
修复代码问题,这是问题的一部分
putty的-m
选项指示服务器启动文件中的命令,而不是shell,以便命令完成后会话立即关闭。为了使putty会话具有交互性,我们需要添加-t
选项,并且在作为-m选项输入的文件中,我们需要添加“/bin/bash”作为最后一个命令
putty.exe -ssh -l username -pw NextGenCne password -m D:\test.txt -t
D:\test.txt文件的内容如下所示
cd /home/username/mydirectory
/bin/bash
因此,完整的程序如下所示
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 -t";
//String s2 = "ls -lrt";
try
{
p = r.exec(s);
p.waitFor();
} catch (Exception e)
{
System.out.println("Exception error :"+e.getMessage());
e.printStackTrace();
}
注意:如果目标是从java代码中以某种方式打开putty终端,那么上面的方法是可以的,如果目标只是在服务器上执行一些命令并捕获输出,那么就按照上面的答案中已经提到的那样使用JSch。为什么要使用putty。对于putty means非交互模式,您可以直接使用ssh-m选项,因此也可以在command.txtx文件中添加“-t”,添加类似以下内容的
ls-ltr/bin/bash
对于cmd打开putty,请使用下面的-t选项D:\\Nandan\\putty.exe-ssh-l*****-pw*****-XX.XX.XX-md:\\Nandan\\command.txtxtx-t