Olá, estou tentando usar a API JSCH para conexão SSH, só que o código abaixo funciona em alguns servidores e em outros não, mostra a mensagem:
You are not allowed to run any remote commands on this machine!
Mas manualmente, eu consigo executar qualquer comando, alguém já teve algum problema parecido?
try {
String command = “show hosts”;
String host = “ip”;
String user = “user”;
String password = “passwd”;
JSch jsch = new JSch();
Session session = jsch.getSession(user, host, 22);
Properties config = new Properties();
config.put(“StrictHostKeyChecking”, “no”);
session.setConfig(config);;
session.setPassword(password);
session.setTimeout(60000);
session.connect(60000);
session.setServerAliveInterval(60000);
System.out.println("###DAT SSH Connection###\n###The session has been established to " + user + "@" + host);
//eate the excution channel over the session
ChannelExec channelExec = (ChannelExec) session.openChannel("exec");
// Gets an InputStream for this channel. All data arriving in as messages from the remote side can be read from this stream.
InputStream in = channelExec.getInputStream();
// Set the command that you want to execute
// In our case its the remote shell script
channelExec.setCommand("show hosts");
// Execute the command
channelExec.connect();
// Read the output from the input stream we set above
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
//Read each line from the buffered reader and add it to result list
// You can also simple print the result here
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
//retrieve the exit status of the remote command corresponding to this channel
int exitStatus = channelExec.getExitStatus();
//Safely disconnect channel and disconnect session. If not done then it may cause resource leak
channelExec.disconnect();
session.disconnect();
if (exitStatus < 0) {
// System.out.println("Done, but exit status not set!");
} else if (exitStatus > 0) {
// System.out.println("Done, but with error!");
} else {
// System.out.println("Done!");
}
} catch (Exception e) {
e.printStackTrace();
}
}