140 lines
4.2 KiB
Java
140 lines
4.2 KiB
Java
package de.gurkengewuerz.monitoring.object;
|
|
|
|
import com.jcraft.jsch.*;
|
|
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.util.logging.Level;
|
|
import java.util.logging.Logger;
|
|
|
|
|
|
/*
|
|
* SSHManager
|
|
*
|
|
* @author cabbott
|
|
* @version 1.0
|
|
*/
|
|
public class SSHManager {
|
|
private static final Logger LOGGER =
|
|
Logger.getLogger(SSHManager.class.getName());
|
|
private JSch jschSSHChannel;
|
|
private String strUserName;
|
|
private String strConnectionIP;
|
|
private int intConnectionPort;
|
|
private String strPassword;
|
|
private Session sesConnection;
|
|
private String errorMessage = null;
|
|
private int intTimeOut;
|
|
|
|
public SSHManager(String userName, String password,
|
|
String connectionIP, String knownHostsFileName) {
|
|
doCommonConstructorActions(userName, password,
|
|
connectionIP, knownHostsFileName);
|
|
intConnectionPort = 22;
|
|
intTimeOut = 60000;
|
|
}
|
|
|
|
public SSHManager(String userName, String password, String connectionIP,
|
|
String knownHostsFileName, int connectionPort) {
|
|
doCommonConstructorActions(userName, password, connectionIP,
|
|
knownHostsFileName);
|
|
intConnectionPort = connectionPort;
|
|
intTimeOut = 60000;
|
|
}
|
|
|
|
public SSHManager(String userName, String password, String connectionIP,
|
|
String knownHostsFileName, int connectionPort, int timeOutMilliseconds) {
|
|
doCommonConstructorActions(userName, password, connectionIP,
|
|
knownHostsFileName);
|
|
intConnectionPort = connectionPort;
|
|
intTimeOut = timeOutMilliseconds;
|
|
}
|
|
|
|
private void doCommonConstructorActions(String userName,
|
|
String password, String connectionIP, String knownHostsFileName) {
|
|
jschSSHChannel = new JSch();
|
|
|
|
try {
|
|
jschSSHChannel.addIdentity(password);
|
|
jschSSHChannel.setKnownHosts(knownHostsFileName);
|
|
} catch (JSchException jschX) {
|
|
logError(jschX.getMessage());
|
|
}
|
|
|
|
strUserName = userName;
|
|
strPassword = password;
|
|
strConnectionIP = connectionIP;
|
|
}
|
|
|
|
public String connect() {
|
|
errorMessage = null;
|
|
|
|
try {
|
|
sesConnection = jschSSHChannel.getSession(strUserName,
|
|
strConnectionIP, intConnectionPort);
|
|
//sesConnection.setPassword(strPassword);
|
|
// UNCOMMENT THIS FOR TESTING PURPOSES, BUT DO NOT USE IN PRODUCTION
|
|
sesConnection.setConfig("StrictHostKeyChecking", "no");
|
|
sesConnection.connect(intTimeOut);
|
|
} catch (JSchException jschX) {
|
|
errorMessage = jschX.getMessage();
|
|
}
|
|
|
|
return errorMessage;
|
|
}
|
|
|
|
public boolean hasError() {
|
|
return errorMessage != null;
|
|
}
|
|
|
|
public String getErrorMessage() {
|
|
return errorMessage;
|
|
}
|
|
|
|
private String logError(String errorMessage) {
|
|
if (errorMessage != null) {
|
|
LOGGER.log(Level.SEVERE, "{0}:{1} - {2}",
|
|
new Object[]{strConnectionIP, intConnectionPort, errorMessage});
|
|
}
|
|
|
|
return errorMessage;
|
|
}
|
|
|
|
private String logWarning(String warnMessage) {
|
|
if (warnMessage != null) {
|
|
LOGGER.log(Level.WARNING, "{0}:{1} - {2}",
|
|
new Object[]{strConnectionIP, intConnectionPort, warnMessage});
|
|
}
|
|
|
|
return warnMessage;
|
|
}
|
|
|
|
public String sendCommand(String command) {
|
|
StringBuilder outputBuffer = new StringBuilder();
|
|
|
|
try {
|
|
Channel channel = sesConnection.openChannel("exec");
|
|
((ChannelExec) channel).setCommand(command);
|
|
InputStream commandOutput = channel.getInputStream();
|
|
channel.connect();
|
|
int readByte = commandOutput.read();
|
|
|
|
while (readByte != 0xffffffff) {
|
|
outputBuffer.append((char) readByte);
|
|
readByte = commandOutput.read();
|
|
}
|
|
|
|
channel.disconnect();
|
|
} catch (IOException | JSchException ioX) {
|
|
logWarning(ioX.getMessage());
|
|
return null;
|
|
}
|
|
|
|
return outputBuffer.toString();
|
|
}
|
|
|
|
public void close() {
|
|
sesConnection.disconnect();
|
|
}
|
|
|
|
} |