use of com.jcraft.jsch.JSchException in project ats-framework by Axway.
the class SftpClient method doConnect.
private void doConnect(String publicKeyAlias) throws FileTransferException {
// make new SFTP object for every new connection
disconnect();
this.jsch = new JSch();
/* NOTE: Due to logging being global (static), if one thread enables debug mode, all threads will log messages,
* and if another thread disables it, logging will be stopped for all threads,
* until at least one thread enables it again
*/
if (isDebugMode()) {
JSch.setLogger(new SftpListener());
debugProgressMonitor = new SftpFileTransferProgressMonitor();
} else {
debugProgressMonitor = null;
}
try {
if (username != null) {
this.session = jsch.getSession(this.username, this.hostname, this.port);
this.session.setPassword(this.password);
} else {
this.jsch.addIdentity(this.keystoreFile, this.publicKeyAlias, this.keystorePassword.getBytes());
this.session = this.jsch.getSession(this.username, this.hostname, this.port);
}
// skip checking of known hosts and verifying RSA keys
this.session.setConfig("StrictHostKeyChecking", "no");
// make keyboard-interactive last authentication method
this.session.setConfig("PreferredAuthentications", "publickey,password,keyboard-interactive");
this.session.setTimeout(this.timeout);
if (this.ciphers != null && this.ciphers.size() > 0) {
StringBuilder ciphers = new StringBuilder();
for (SshCipher cipher : this.ciphers) {
ciphers.append(cipher.getSshAlgorithmName() + ",");
}
this.session.setConfig("cipher.c2s", ciphers.toString());
this.session.setConfig("cipher.s2c", ciphers.toString());
this.session.setConfig("CheckCiphers", ciphers.toString());
}
if (this.listener != null) {
this.listener.setResponses(new ArrayList<String>());
JSch.setLogger((com.jcraft.jsch.Logger) this.listener);
}
/* SFTP reference implementation does not support transfer mode.
Due to that, JSch does not support it either.
For now setTransferMode() has no effect. It may be left like that,
implemented to throw new FileTransferException( "Not implemented" )
or log warning, that SFTP protocol does not support transfer mode change.
*/
this.session.connect();
this.channel = (ChannelSftp) this.session.openChannel("sftp");
this.channel.connect();
} catch (JSchException e) {
throw new FileTransferException("Unable to connect!", e);
}
}
use of com.jcraft.jsch.JSchException in project GNS by MobilityFirst.
the class Sudo method main.
/**
*
* @param arg
*/
public static void main(String[] arg) {
try {
JSch jsch = new JSch();
Session session = SSHClient.authenticateWithKey(jsch, null, null, null);
UserInfo ui = new UserInfoPrompted();
session.setUserInfo(ui);
session.connect();
String command = JOptionPane.showInputDialog("Enter command, execed with sudo", "printenv SUDO_USER");
String sudo_pass = null;
{
JTextField passwordField = new JPasswordField(8);
Object[] ob = { passwordField };
int result = JOptionPane.showConfirmDialog(null, ob, "Enter password for sudo", JOptionPane.OK_CANCEL_OPTION);
if (result != JOptionPane.OK_OPTION) {
System.exit(-1);
}
sudo_pass = passwordField.getText();
}
Channel channel = session.openChannel("exec");
// man sudo
// -S The -S (stdin) option causes sudo to read the password from the
// standard input instead of the terminal device.
// -p The -p (prompt) option allows you to override the default
// password prompt and use a custom one.
((ChannelExec) channel).setCommand("sudo -S -p '' " + command);
InputStream in = channel.getInputStream();
OutputStream out = channel.getOutputStream();
((ChannelExec) channel).setErrStream(System.err);
((ChannelExec) channel).setPty(true);
channel.connect();
out.write((sudo_pass + "\n").getBytes());
out.flush();
byte[] tmp = new byte[1024];
while (true) {
while (in.available() > 0) {
int i = in.read(tmp, 0, 1024);
if (i < 0) {
break;
}
System.out.print(new String(tmp, 0, i));
}
if (channel.isClosed()) {
System.out.println("exit-status: " + channel.getExitStatus());
break;
}
try {
Thread.sleep(1000);
} catch (Exception ee) {
}
}
channel.disconnect();
session.disconnect();
} catch (JSchException | HeadlessException | IOException e) {
System.out.println(e);
}
}
use of com.jcraft.jsch.JSchException in project gerrit by GerritCodeReview.
the class SshDaemon method computeHostKeys.
private List<HostKey> computeHostKeys() {
if (listen.isEmpty()) {
return Collections.emptyList();
}
final List<PublicKey> keys = myHostKeys();
final List<HostKey> r = new ArrayList<>();
for (final PublicKey pub : keys) {
final Buffer buf = new ByteArrayBuffer();
buf.putRawPublicKey(pub);
final byte[] keyBin = buf.getCompactData();
for (final String addr : advertised) {
try {
r.add(new HostKey(addr, keyBin));
} catch (JSchException e) {
sshDaemonLog.warn(String.format("Cannot format SSHD host key [%s]: %s", pub.getAlgorithm(), e.getMessage()));
}
}
}
return Collections.unmodifiableList(r);
}
use of com.jcraft.jsch.JSchException in project cdap by caskdata.
the class SFTPConnectionPool method connect.
public ChannelSftp connect(String host, int port, String user, String password, String keyFile) throws IOException {
// get connection from pool
ConnectionInfo info = new ConnectionInfo(host, port, user);
ChannelSftp channel = getFromPool(info);
if (channel != null) {
if (channel.isConnected()) {
return channel;
} else {
channel = null;
synchronized (this) {
--liveConnectionCount;
con2infoMap.remove(channel);
}
}
}
// create a new connection and add to pool
JSch jsch = new JSch();
Session session = null;
try {
if (user == null || user.length() == 0) {
user = System.getProperty("user.name");
}
if (password == null) {
password = "";
}
if (keyFile != null && keyFile.length() > 0) {
jsch.addIdentity(keyFile);
}
if (port <= 0) {
session = jsch.getSession(user, host);
} else {
session = jsch.getSession(user, host, port);
}
session.setPassword(password);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
channel = (ChannelSftp) session.openChannel("sftp");
channel.connect();
synchronized (this) {
con2infoMap.put(channel, info);
liveConnectionCount++;
}
return channel;
} catch (JSchException e) {
throw new IOException(StringUtils.stringifyException(e));
}
}
use of com.jcraft.jsch.JSchException in project cdap by caskdata.
the class SFTPConnectionPool method disconnect.
void disconnect(ChannelSftp channel) throws IOException {
if (channel != null) {
// close connection if too many active connections
boolean closeConnection = false;
synchronized (this) {
if (liveConnectionCount > maxConnection) {
--liveConnectionCount;
con2infoMap.remove(channel);
closeConnection = true;
}
}
if (closeConnection) {
if (channel.isConnected()) {
try {
Session session = channel.getSession();
channel.disconnect();
session.disconnect();
} catch (JSchException e) {
throw new IOException(StringUtils.stringifyException(e));
}
}
} else {
returnToPool(channel);
}
}
}
Aggregations