我正在开发Java应用程序以SSH转换到zxa10 c320设备。我用jsch库来ssh。但是,当我在第一个while循环中运行该应用程序时,它没有返回任何输出。当我通过指向ssh某些其他Linux服务器运行该应用程序时,它可以正常工作。
如果我做错了任何事情,请告诉我。
我在'exec'和'shell'模式下都尝试这种方法。
此外,我尝试使用sshj库执行此操作。它也没有用。
public class RunCommand {
public String runCommand(String command) {
String host = "XX.XX.X.XX";
String user = "user";
String password = "password";
String result = "";
try{
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
JSch jsch = new JSch();
// Create a JSch session to connect to the server
Session session = jsch.getSession(user, host, 22);
session.setPassword(password);
session.setConfig(config);
// Establish the connection
session.connect();
if(session.isConnected()) {
System.out.println("Connected...");
}
ChannelExec channel = (ChannelExec) session.openChannel("exec");
channel.setCommand("pwd");
int stat = channel.getExitStatus();
StringBuilder outputBuffer = new StringBuilder();
StringBuilder errorBuffer = new StringBuilder();
InputStream in = channel.getInputStream();
InputStream err = channel.getExtInputStream();
channel.setPty(true);
channel.connect();
System.out.println("Channel Connected");
System.out.println("channel.isConnected() "+channel.isConnected());
System.out.println("channel.isClosed() "+channel.isClosed());
byte[] tmp = new byte[1024];
while (true) {
while (in.available() > 0) {
int i = in.read(tmp, 0, 1024);
if (i < 0) break;
outputBuffer.append(new String(tmp, 0, i));
}
while (err.available() > 0) {
int i = err.read(tmp, 0, 1024);
if (i < 0) break;
errorBuffer.append(new String(tmp, 0, i));
}
if (channel.isClosed()) {
if ((in.available() > 0) || (err.available() > 0)) continue;
System.out.println("exit-status: " + channel.getExitStatus());
break;
}
try {
Thread.sleep(1000);
} catch (Exception ee) {
}
}
System.out.println("output: " + outputBuffer.toString());
System.out.println("error: " + errorBuffer.toString());
channel.disconnect();
session.disconnect();
System.out.println("Channel And Session Closed!!!");
}catch(JSchException e){
e.printStackTrace();
result = "Auth fail";
}catch(Exception e){
e.printStackTrace();
}
return result;
}
}
答案 0 :(得分:0)
我无法使用peo java做到这一点。但是我能够使用python的paramiko模块来做到这一点。我所做的是我写了两个应用程序,一个是用Java编写的,另一个是用python编写的,然后我从Java应用程序和ssh调用python应用程序,然后通过python应用程序调用gpon。