use of com.jcraft.jsch.JSch in project jcabi-github by jcabi.
the class RtDeployKeysITCase method key.
/**
* Generates a random public key for test.
*
* @return The encoded SSH public key.
* @throws Exception If a problem occurs.
*/
private static String key() throws Exception {
final ByteArrayOutputStream stream = new ByteArrayOutputStream();
try {
final KeyPair kpair = KeyPair.genKeyPair(new JSch(), KeyPair.DSA);
kpair.writePublicKey(stream, "");
kpair.dispose();
} finally {
stream.close();
}
return new String(stream.toByteArray());
}
use of com.jcraft.jsch.JSch in project nimbus by nimbus-org.
the class SFTPClientImpl method connect.
public void connect(String user, String host, int port, String password) throws SFTPException {
if (jsch != null) {
throw new SFTPException("It is already connected!");
}
jsch = new JSch();
try {
session = jsch.getSession(user, host, port);
if (configProperties != null) {
session.setConfig(configProperties);
}
if (proxy != null) {
session.setProxy(proxy);
}
if (timeout >= 0) {
session.setTimeout(timeout);
}
if (serverAliveInterval >= 0) {
session.setServerAliveInterval(serverAliveInterval);
}
if (serverAliveCountMax >= 0) {
session.setServerAliveCountMax(serverAliveCountMax);
}
if (password != null) {
session.setPassword(password);
}
session.connect();
channel = (ChannelSftp) session.openChannel("sftp");
if (fileNameEncoding != null) {
channel.setFilenameEncoding(fileNameEncoding);
}
channel.connect();
if (homeDir != null) {
channel.lcd(homeDir.getPath());
}
} catch (JSchException e) {
if (channel != null) {
channel.disconnect();
channel = null;
}
if (session != null) {
session.disconnect();
session = null;
}
jsch = null;
throw new SFTPException("It failed to connect!", e);
} catch (SftpException e) {
if (channel != null) {
channel.disconnect();
channel = null;
}
if (session != null) {
session.disconnect();
session = null;
}
jsch = null;
throw new SFTPException("It failed to connect!", e);
}
}
use of com.jcraft.jsch.JSch in project syndesis by syndesisio.
the class SftpVerifierExtension method verifyCredentials.
private void verifyCredentials(ResultBuilder builder, Map<String, Object> parameters) {
final String host = (String) parameters.get("host");
final int port = Integer.parseInt((String) parameters.get("port"));
final String userName = (String) parameters.get("username");
final String password = (parameters.get("password") == null) ? "" : (String) parameters.get("password");
JSch jsch = new JSch();
Session session = null;
try {
session = jsch.getSession(userName, host, port);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(password);
session.connect();
} catch (JSchException e) {
builder.error(ResultErrorBuilder.withCodeAndDescription(VerificationError.StandardCode.AUTHENTICATION, e.getMessage()).build());
} finally {
if (session != null) {
session.disconnect();
jsch = null;
}
}
}
use of com.jcraft.jsch.JSch in project AJSC by att.
the class GenerateKeys method generateKeyPair.
public static void generateKeyPair(com.att.cdp.zones.model.KeyPair kp) throws IOException, JSchException, ZoneException {
KeyPair kpair;
kpair = KeyPair.genKeyPair(new JSch(), KeyPair.RSA);
// String publicKeyFilename = PUBLIC_KEY;
// String privateKeyFilename = PRIVATE_KEY;
OutputStream os = new ByteArrayOutputStream();
kpair.writePrivateKey(os);
String str = "";
try {
str = reformatSSHKey(os.toString());
} catch (Exception e) {
LOG.error(e.getMessage());
}
kp.setPrivateKey(str);
os = new ByteArrayOutputStream();
kpair.writePublicKey(os, "");
kp.setPublicKey(os.toString());
kpair.getFingerPrint();
kpair.dispose();
}
use of com.jcraft.jsch.JSch in project onebusaway-application-modules by camsys.
the class GtfsRealtimeSource method readFeedFromUrl.
/**
* This method was added to allow accessing an SFTP feed, since the Java URL
* class does not yet support the SFTP protocol.
*
* @param url of the SFTP feed to read
* @return a {@link FeedMessage} constructed from the protocol buffer content
* of the specified url
* @throws IOException
*/
private FeedMessage readFeedFromUrl(String url) throws IOException {
Session session = null;
Channel channel = null;
ChannelSftp downloadChannelSftp = null;
InputStream in = null;
JSch jsch = new JSch();
// Parse SFTP URL
int idx = url.indexOf("//") + 2;
int idx2 = url.indexOf(":", idx);
String user = url.substring(idx, idx2);
idx = idx2 + 1;
idx2 = url.indexOf("@");
String pw = url.substring(idx, idx2);
url = url.substring(idx2 + 1);
idx = url.indexOf(":");
String host = url.substring(0, idx);
String rdir = "";
idx = url.indexOf("/") + 1;
idx2 = url.lastIndexOf("/");
if (idx2 > idx) {
rdir = url.substring(idx, idx2);
} else {
idx2 = idx - 1;
}
String rfile = url.substring(idx2 + 1);
try {
session = jsch.getSession(user, host, 22);
session.setPassword(pw);
session.setConfig("StrictHostKeyChecking", "no");
// Set timeout to 10 seconds
session.connect(10000);
channel = session.openChannel("sftp");
channel.connect();
downloadChannelSftp = (ChannelSftp) channel;
downloadChannelSftp.cd(downloadChannelSftp.getHome() + "/" + rdir);
File downloadFile = new File(downloadChannelSftp.getHome() + "/" + rfile);
in = downloadChannelSftp.get(downloadFile.getName());
return FeedMessage.parseFrom(in, _registry);
} catch (JSchException ex) {
_log.error("connection issue with sftp url " + url);
return getDefaultFeedMessage();
} catch (SftpException e) {
_log.error("connection issue with sftp");
e.printStackTrace();
return getDefaultFeedMessage();
} finally {
try {
if (channel != null)
channel.disconnect();
if (session != null)
session.disconnect();
if (in != null)
in.close();
} catch (IOException ex) {
_log.error("error closing url stream " + url);
}
}
}
Aggregations