Skip to content

Instantly share code, notes, and snippets.

@kdelfour
Last active April 7, 2022 16:47
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save kdelfour/5f1fde64c3d23daea704 to your computer and use it in GitHub Desktop.
Save kdelfour/5f1fde64c3d23daea704 to your computer and use it in GitHub Desktop.
SSH connector example using JSCH. JSch allows you to connect to an sshd server and use port forwarding, X11 forwarding, file transfer, etc., and you can integrate its functionality into your own Java programs. JSch is licensed under BSD style license.
/**
*
*/
package com.kdstudio.snippets.connector.ssh;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import org.apache.log4j.Logger;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelShell;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
/**
* This is an example of SSH connector using JSCh library
*
* JSch allows you to connect to an sshd server and use port forwarding, X11
* forwarding, file transfer, etc., and you can integrate its functionality into
* your own Java programs. JSch is licensed under BSD style license.
*
* @author kdelfour
*
*/
public class AnSshConnector implements AutoCloseable {
// Default logger
private static final Logger LOG = Logger.getLogger(SshConnector.class);
// Constants
private static final String STRICT_HOSTKEY_CHECKIN_KEY = "StrictHostKeyChecking";
private static final String STRICT_HOSTKEY_CHECKIN_VALUE = "no";
private static final String CHANNEL_TYPE = "shell";
// SSH server ip
private String ip;
// SSH server port
private int port;
// User login
private String login;
// User password
private String password;
// Connection timeout
private int timeout;
private Session session;
private PrintStream ps;
private InputStream input;
private OutputStream ops;
private Channel channel;
/**
* Basic constructor
*
* @param ip
* the ssh server IP
* @param port
* the ssh server port
* @param login
* , the ssh user login
* @param password
* , the ssh user password
* @param timeout
* , the connection timeout
*/
public AnSshConnector(String ip, int port, String login, String password,
int timeout) {
super();
this.ip = ip;
this.port = port;
this.login = login;
this.password = password;
this.timeout = timeout;
}
/**
* Open a connection
*
* @throws JSchException
* if a error due to the ssh server connection...
* @throws IOException
*
*/
public void open() throws JSchException, IOException {
// Prepare session
final Log4JOutputStream log4JOutputStream = new Log4JOutputStream();
final JSch jsch = new JSch();
session = jsch.getSession(login, ip, port);
session.setPassword(password);
session.setTimeout(timeout);
session.setConfig(STRICT_HOSTKEY_CHECKIN_KEY,
STRICT_HOSTKEY_CHECKIN_VALUE);
// Start a connection
LOG.debug("-- Try to connect to the server " + ip + ":" + port
+ " with user " + login);
session.connect();
LOG.debug("-- Connexion OK");
LOG.debug("-- Open SSH channel");
channel = session.openChannel(CHANNEL_TYPE);
input = channel.getInputStream();
// Redirection of output stream to log4jOutputStream
((ChannelShell) channel).setExtOutputStream(log4JOutputStream);
ops = channel.getOutputStream();
ps = new PrintStream(ops, true);
channel.connect();
LOG.debug("-- Open SSH channel OK");
}
/**
* Execute a command and return the result as a String
*
* @param command
* the command to execute
* @return the result as a String
* @throws IOException
*/
public String executeCommand(String command) throws IOException {
ps.println(command);
int size = 1024;
final byte[] tmp = new byte[size];
final StringBuilder sb = new StringBuilder();
while (true) {
while (input.available() > 0) {
int i = input.read(tmp, 0, 1024);
if (i < 0) {
break;
}
sb.append(new String(tmp, 0, i));
}
final String output = sb.toString();
if (output.contains("object")) {
break;
}
if (channel.isClosed()) {
if (input.available() > 0) {
int i = input.read(tmp, 0, 1024);
sb.append(new String(tmp, 0, i));
}
break;
}
try {
Thread.sleep(1000);
} catch (Exception e) {
LOG.error(e);
}
}
return sb.toString();
}
/*
* (non-Javadoc)
*
* @see java.lang.AutoCloseable#close()
*/
public void close() throws Exception {
// Close channel
channel.disconnect();
// Close session
session.disconnect();
}
}
package com.kdstudio.snippets.connector.ssh;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.log4j.Logger;
/**
* Redirection of output stream to log4j
* @author kdelfour
*
*/
public class Log4JOutputStream extends OutputStream {
// The default logger
private static final Logger LOG = Logger.getLogger(Log4JOutputStream.class
.getName());
private final StringBuilder stringBuilder = new StringBuilder();
/*
* (non-Javadoc)
*
* @see java.io.OutputStream#write(int)
*/
@Override
public void write(int b) throws IOException {
char current = (char) b;
if (current == '\n') {
LOG.error(stringBuilder.toString());
// Reset it
stringBuilder.setLength(0);
} else {
stringBuilder.append(current);
}
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dkstudio</groupId>
<artifactId>snippets</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Snippets</name>
<description>JSch allows you to connect to an sshd server and use port forwarding, X11 forwarding, file transfer, etc.,
and you can integrate its functionality into your own Java programs. JSch is licensed under BSD style license.</description>
<developers>
<developer>
<id>delfour.k</id>
<name>Kevin DELFOUR</name>
<email>delfour.k@gmail.com</email>
<organization>DK</organization>
<organizationUrl>http://kevin.delfour.eu</organizationUrl>
</developer>
</developers>
<organization>
<name>DK</name>
<url>http://kevin.delfour.eu</url>
</organization>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.build.sourceVersion>1.7</project.build.sourceVersion>
</properties>
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.51</version>
</dependency>
</dependencies>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment