use of org.openmuc.j62056.Connection in project Payara by payara.
the class SSHLauncher method setupKey.
/**
* Setting up the key involves the following steps:
* -If a key exists and we can connect using the key, do nothing.
* -Generate a key pair if there isn't one
* -Connect to remote host using password auth and do the following:
* 1. create .ssh directory if it doesn't exist
* 2. copy over the key as key.tmp
* 3. Append the key to authorized_keys file
* 4. Remove the temporary key file key.tmp
* 5. Fix permissions for home, .ssh and authorized_keys
* @param node - remote host
* @param pubKeyFile - .pub file
* @param generateKey - flag to indicate if key needs to be generated or not
* @param passwd - ssh user password
* @throws IOException
* @throws InterruptedException
*/
public void setupKey(String node, String pubKeyFile, boolean generateKey, String passwd) throws IOException, InterruptedException {
boolean connected = false;
File key = new File(keyFile);
if (logger.isLoggable(Level.FINER))
logger.finer("Key = " + keyFile);
if (key.exists()) {
if (checkConnection()) {
throw new IOException("SSH public key authentication is already configured for " + userName + "@" + node);
}
} else {
if (generateKey) {
if (!generateKeyPair()) {
throw new IOException("SSH key pair generation failed. Please generate key manually.");
}
} else {
throw new IOException("SSH key pair not present. Please generate a key pair manually or specify an existing one and re-run the command.");
}
}
// password is must for key distribution
if (passwd == null) {
throw new IOException("SSH password is required for distributing the public key. You can specify the SSH password in a password file and pass it through --passwordfile option.");
}
connection = new Connection(node, port);
connection.connect();
connected = connection.authenticateWithPassword(userName, passwd);
if (!connected) {
throw new IOException("SSH password authentication failed for user " + userName + " on host " + node);
}
// We open up a second connection for scp and exec. For some reason, a hang
// is seen in MKS if we try to do everything using the same connection.
Connection conn = new Connection(node, port);
conn.connect();
boolean ret = conn.authenticateWithPassword(userName, passwd);
if (!ret) {
throw new IOException("SSH password authentication failed for user " + userName + " on host " + node);
}
// initiate scp client
SCPClient scp = new SCPClient(conn);
SFTPClient sftp = new SFTPClient(connection);
if (key.exists()) {
// fixes .ssh file mode
setupSSHDir();
if (pubKeyFile == null) {
pubKeyFile = keyFile + ".pub";
}
File pubKey = new File(pubKeyFile);
if (!pubKey.exists()) {
throw new IOException("Public key file " + pubKeyFile + " does not exist.");
}
try {
if (!sftp.exists(SSH_DIR)) {
if (logger.isLoggable(Level.FINE)) {
logger.fine(SSH_DIR + " does not exist");
}
sftp.mkdirs(".ssh", 0700);
}
} catch (Exception e) {
if (logger.isLoggable(Level.FINER)) {
e.printStackTrace();
}
throw new IOException("Error while creating .ssh directory on remote host:" + e.getMessage());
}
// copy over the public key to remote host
scp.put(pubKey.getAbsolutePath(), "key.tmp", ".ssh", "0600");
// append the public key file contents to authorized_keys file on remote host
String mergeCommand = "cd .ssh; cat key.tmp >> " + AUTH_KEY_FILE;
if (logger.isLoggable(Level.FINER)) {
logger.finer("mergeCommand = " + mergeCommand);
}
if (conn.exec(mergeCommand, new ByteArrayOutputStream()) != 0) {
throw new IOException("Failed to propogate the public key " + pubKeyFile + " to " + host);
}
logger.info("Copied keyfile " + pubKeyFile + " to " + userName + "@" + host);
// remove the public key file on remote host
if (conn.exec("rm .ssh/key.tmp", new ByteArrayOutputStream()) != 0) {
logger.warning("WARNING: Failed to remove the public key file key.tmp on remote host " + host);
}
if (logger.isLoggable(Level.FINER)) {
logger.finer("Removed the temporary key file on remote host");
}
// Lets fix all the permissions
// On MKS, chmod doesn't work as expected. StrictMode needs to be disabled
// for connection to go through
logger.info("Fixing file permissions for home(755), .ssh(700) and authorized_keys file(644)");
sftp.chmod(".", 0755);
sftp.chmod(SSH_DIR, 0700);
sftp.chmod(SSH_DIR + AUTH_KEY_FILE, 0644);
// release the connections
sftp.close();
conn.close();
}
}
use of org.openmuc.j62056.Connection in project okhttp by square.
the class HttpLoggingInterceptor method intercept.
@Override
public Response intercept(Chain chain) throws IOException {
Level level = this.level;
Request request = chain.request();
if (level == Level.NONE) {
return chain.proceed(request);
}
boolean logBody = level == Level.BODY;
boolean logHeaders = logBody || level == Level.HEADERS;
RequestBody requestBody = request.body();
boolean hasRequestBody = requestBody != null;
Connection connection = chain.connection();
Protocol protocol = connection != null ? connection.protocol() : Protocol.HTTP_1_1;
String requestStartMessage = "--> " + request.method() + ' ' + request.url() + ' ' + protocol;
if (!logHeaders && hasRequestBody) {
requestStartMessage += " (" + requestBody.contentLength() + "-byte body)";
}
logger.log(requestStartMessage);
if (logHeaders) {
if (hasRequestBody) {
// them to be included (when available) so there values are known.
if (requestBody.contentType() != null) {
logger.log("Content-Type: " + requestBody.contentType());
}
if (requestBody.contentLength() != -1) {
logger.log("Content-Length: " + requestBody.contentLength());
}
}
Headers headers = request.headers();
for (int i = 0, count = headers.size(); i < count; i++) {
String name = headers.name(i);
// Skip headers from the request body as they are explicitly logged above.
if (!"Content-Type".equalsIgnoreCase(name) && !"Content-Length".equalsIgnoreCase(name)) {
logger.log(name + ": " + headers.value(i));
}
}
if (!logBody || !hasRequestBody) {
logger.log("--> END " + request.method());
} else if (bodyEncoded(request.headers())) {
logger.log("--> END " + request.method() + " (encoded body omitted)");
} else {
Buffer buffer = new Buffer();
requestBody.writeTo(buffer);
Charset charset = UTF8;
MediaType contentType = requestBody.contentType();
if (contentType != null) {
charset = contentType.charset(UTF8);
}
logger.log("");
if (isPlaintext(buffer)) {
logger.log(buffer.readString(charset));
logger.log("--> END " + request.method() + " (" + requestBody.contentLength() + "-byte body)");
} else {
logger.log("--> END " + request.method() + " (binary " + requestBody.contentLength() + "-byte body omitted)");
}
}
}
long startNs = System.nanoTime();
Response response;
try {
response = chain.proceed(request);
} catch (Exception e) {
logger.log("<-- HTTP FAILED: " + e);
throw e;
}
long tookMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs);
ResponseBody responseBody = response.body();
long contentLength = responseBody.contentLength();
String bodySize = contentLength != -1 ? contentLength + "-byte" : "unknown-length";
logger.log("<-- " + response.code() + ' ' + response.message() + ' ' + response.request().url() + " (" + tookMs + "ms" + (!logHeaders ? ", " + bodySize + " body" : "") + ')');
if (logHeaders) {
Headers headers = response.headers();
for (int i = 0, count = headers.size(); i < count; i++) {
logger.log(headers.name(i) + ": " + headers.value(i));
}
if (!logBody || !HttpHeaders.hasBody(response)) {
logger.log("<-- END HTTP");
} else if (bodyEncoded(response.headers())) {
logger.log("<-- END HTTP (encoded body omitted)");
} else {
BufferedSource source = responseBody.source();
// Buffer the entire body.
source.request(Long.MAX_VALUE);
Buffer buffer = source.buffer();
Charset charset = UTF8;
MediaType contentType = responseBody.contentType();
if (contentType != null) {
charset = contentType.charset(UTF8);
}
if (!isPlaintext(buffer)) {
logger.log("");
logger.log("<-- END HTTP (binary " + buffer.size() + "-byte body omitted)");
return response;
}
if (contentLength != 0) {
logger.log("");
logger.log(buffer.clone().readString(charset));
}
logger.log("<-- END HTTP (" + buffer.size() + "-byte body)");
}
}
return response;
}
use of org.openmuc.j62056.Connection in project openhab1-addons by openhab.
the class Meter method read.
/**
* Reads data from meter
*
* @return a map of DataSet objects with the obis as key.
*/
public Map<String, DataSet> read() {
// the frequently executed code (polling) goes here ...
Map<String, DataSet> dataSetMap = new HashMap<String, DataSet>();
Connection connection = new Connection(config.getSerialPort(), config.getInitMessage(), config.getEchoHandling(), config.getBaudRateChangeDelay());
try {
try {
connection.open();
} catch (IOException e) {
logger.error("Failed to open serial port {}: {}", config.getSerialPort(), e.getMessage());
return dataSetMap;
}
List<DataSet> dataSets = null;
try {
dataSets = connection.read();
for (DataSet dataSet : dataSets) {
logger.debug("DataSet: {};{};{}", dataSet.getId(), dataSet.getValue(), dataSet.getUnit());
dataSetMap.put(dataSet.getId(), dataSet);
}
} catch (IOException e) {
logger.error("IOException while trying to read: {}", e.getMessage());
} catch (TimeoutException e) {
logger.error("Read attempt timed out");
}
} finally {
connection.close();
}
return dataSetMap;
}
use of org.openmuc.j62056.Connection in project CloudStack-archive by CloudStack-extras.
the class TestClientWithAPI method sshWinTest.
private static String sshWinTest(String host) {
if (host == null) {
s_logger.info("Did not receive a host back from test, ignoring win ssh test");
return null;
}
// We will retry 5 times before quitting
int retry = 1;
while (true) {
try {
if (retry > 0) {
s_logger.info("Retry attempt : " + retry + " ...sleeping 300 seconds before next attempt. Account is " + _account.get());
Thread.sleep(300000);
}
s_logger.info("Attempting to SSH into windows host " + host + " with retry attempt: " + retry + " for account " + _account.get());
Connection conn = new Connection(host);
conn.connect(null, 60000, 60000);
s_logger.info("User " + _account.get() + " ssHed successfully into windows host " + host);
boolean success = false;
boolean isAuthenticated = conn.authenticateWithPassword("Administrator", "password");
if (isAuthenticated == false) {
return "Authentication failed";
} else {
s_logger.info("Authentication is successfull");
}
try {
SCPClient scp = new SCPClient(conn);
scp.put("wget.exe", "wget.exe", "C:\\Users\\Administrator", "0777");
s_logger.info("Successfully put wget.exe file");
} catch (Exception ex) {
s_logger.error("Unable to put wget.exe " + ex);
}
if (conn == null) {
s_logger.error("Connection is null");
}
Session sess = conn.openSession();
s_logger.info("User + " + _account.get() + " executing : wget http://" + downloadUrl);
String downloadCommand = "wget http://" + downloadUrl + " && dir dump.bin";
sess.execCommand(downloadCommand);
InputStream stdout = sess.getStdout();
InputStream stderr = sess.getStderr();
byte[] buffer = new byte[8192];
while (true) {
if ((stdout.available() == 0) && (stderr.available() == 0)) {
int conditions = sess.waitForCondition(ChannelCondition.STDOUT_DATA | ChannelCondition.STDERR_DATA | ChannelCondition.EOF, 120000);
if ((conditions & ChannelCondition.TIMEOUT) != 0) {
s_logger.info("Timeout while waiting for data from peer.");
return null;
}
if ((conditions & ChannelCondition.EOF) != 0) {
if ((conditions & (ChannelCondition.STDOUT_DATA | ChannelCondition.STDERR_DATA)) == 0) {
break;
}
}
}
while (stdout.available() > 0) {
success = true;
int len = stdout.read(buffer);
if (// this check is somewhat paranoid
len > 0)
s_logger.info(new String(buffer, 0, len));
}
while (stderr.available() > 0) {
/* int len = */
stderr.read(buffer);
}
}
sess.close();
conn.close();
if (success) {
return null;
} else {
retry++;
if (retry == MAX_RETRY_WIN) {
return "SSH Windows Network test fail for account " + _account.get();
}
}
} catch (Exception e) {
s_logger.error(e);
retry++;
if (retry == MAX_RETRY_WIN) {
return "SSH Windows Network test fail with error " + e.getMessage();
}
}
}
}
use of org.openmuc.j62056.Connection in project CloudStack-archive by CloudStack-extras.
the class TestClientWithAPI method sshTest.
private static String sshTest(String host, String password, String snapshot_test) {
int i = 0;
if (host == null) {
s_logger.info("Did not receive a host back from test, ignoring ssh test");
return null;
}
if (password == null) {
s_logger.info("Did not receive a password back from test, ignoring ssh test");
return null;
}
// We will retry 5 times before quitting
String result = null;
int retry = 0;
while (true) {
try {
if (retry > 0) {
s_logger.info("Retry attempt : " + retry + " ...sleeping 120 seconds before next attempt. Account is " + _account.get());
Thread.sleep(120000);
}
s_logger.info("Attempting to SSH into linux host " + host + " with retry attempt: " + retry + ". Account is " + _account.get());
Connection conn = new Connection(host);
conn.connect(null, 60000, 60000);
s_logger.info("User + " + _account.get() + " ssHed successfully into linux host " + host);
boolean isAuthenticated = conn.authenticateWithPassword("root", password);
if (isAuthenticated == false) {
s_logger.info("Authentication failed for root with password" + password);
return "Authentication failed";
}
boolean success = false;
String linuxCommand = null;
if (i % 10 == 0)
linuxCommand = "rm -rf *; wget http://" + downloadUrl + " && ls -al dump.bin";
else
linuxCommand = "wget http://" + downloadUrl + " && ls -al dump.bin";
Session sess = conn.openSession();
s_logger.info("User " + _account.get() + " executing : " + linuxCommand);
sess.execCommand(linuxCommand);
InputStream stdout = sess.getStdout();
InputStream stderr = sess.getStderr();
byte[] buffer = new byte[8192];
while (true) {
if ((stdout.available() == 0) && (stderr.available() == 0)) {
int conditions = sess.waitForCondition(ChannelCondition.STDOUT_DATA | ChannelCondition.STDERR_DATA | ChannelCondition.EOF, 120000);
if ((conditions & ChannelCondition.TIMEOUT) != 0) {
s_logger.info("Timeout while waiting for data from peer.");
return null;
}
if ((conditions & ChannelCondition.EOF) != 0) {
if ((conditions & (ChannelCondition.STDOUT_DATA | ChannelCondition.STDERR_DATA)) == 0) {
break;
}
}
}
while (stdout.available() > 0) {
success = true;
int len = stdout.read(buffer);
if (// this check is somewhat paranoid
len > 0)
s_logger.info(new String(buffer, 0, len));
}
while (stderr.available() > 0) {
/* int len = */
stderr.read(buffer);
}
}
sess.close();
conn.close();
if (!success) {
retry++;
if (retry == MAX_RETRY_LINUX) {
result = "SSH Linux Network test fail";
}
}
if (snapshot_test.equals("no"))
return result;
else {
Long sleep = 300000L;
s_logger.info("Sleeping for " + sleep / 1000 / 60 + "minutes before executing next ssh test");
Thread.sleep(sleep);
}
} catch (Exception e) {
retry++;
s_logger.error("SSH Linux Network test fail with error");
if ((retry == MAX_RETRY_LINUX) && (snapshot_test.equals("no"))) {
return "SSH Linux Network test fail with error " + e.getMessage();
}
}
i++;
}
}
Aggregations