use of com.jcraft.jsch.JSch in project nifi by apache.
the class SFTPUtils method connectSftp.
public static SFTPConnection connectSftp(final SFTPConfiguration conf) throws JSchException, SftpException, IOException {
final JSch jsch = new JSch();
final Session session = SFTPUtils.createSession(conf, jsch);
final ChannelSftp sftp = (ChannelSftp) session.openChannel("sftp");
sftp.connect();
return new SFTPConnection(session, sftp);
}
use of com.jcraft.jsch.JSch in project OsmAnd-tools by osmandapp.
the class IndexUploader method uploadToSSH.
public void uploadToSSH(File f, String description, String size, String date, UploadSSHCredentials cred) throws IOException, JSchException {
log.info("Uploading file " + f.getName() + " " + size + " MB " + date + " of " + description);
// Upload to ftp
JSch jSch = new JSch();
boolean knownHosts = false;
if (cred.knownHosts != null) {
jSch.setKnownHosts(cred.knownHosts);
knownHosts = true;
}
if (cred.privateKey != null) {
jSch.addIdentity(cred.privateKey);
}
String serverName = cred.url;
if (serverName.startsWith("ssh://")) {
serverName = serverName.substring("ssh://".length());
}
Session session = jSch.getSession(cred.user, serverName);
if (cred.password != null) {
session.setPassword(cred.password);
}
if (!knownHosts) {
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
}
String rfile = cred.path + "/" + f.getName();
String lfile = f.getAbsolutePath();
session.connect();
// exec 'scp -t rfile' remotely
String command = "scp -p -t \"" + rfile + "\"";
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command);
// get I/O streams for remote scp
OutputStream out = channel.getOutputStream();
InputStream in = channel.getInputStream();
channel.connect();
if (checkAck(in) != 0) {
channel.disconnect();
session.disconnect();
return;
}
// send "C0644 filesize filename", where filename should not include '/'
long filesize = (new File(lfile)).length();
command = "C0644 " + filesize + " ";
if (lfile.lastIndexOf('/') > 0) {
command += lfile.substring(lfile.lastIndexOf('/') + 1);
} else {
command += lfile;
}
command += "\n";
out.write(command.getBytes());
out.flush();
if (checkAck(in) != 0) {
channel.disconnect();
session.disconnect();
return;
}
// send a content of lfile
FileInputStream fis = new FileInputStream(lfile);
byte[] buf = new byte[1024];
try {
int len;
while ((len = fis.read(buf, 0, buf.length)) > 0) {
// out.flush();
out.write(buf, 0, len);
}
} finally {
fis.close();
}
fis = null;
// send '\0'
buf[0] = 0;
out.write(buf, 0, 1);
out.flush();
if (checkAck(in) != 0) {
channel.disconnect();
session.disconnect();
return;
}
out.close();
channel.disconnect();
session.disconnect();
log.info("Finish uploading file index");
}
use of com.jcraft.jsch.JSch in project MGit by maks.
the class PrivateKeyGenerate method generateKey.
private void generateKey() {
String newFilename = mNewFilename.getText().toString().trim();
if (newFilename.equals("")) {
showToastMessage(R.string.alert_new_filename_required);
mNewFilename.setError(getString(R.string.alert_new_filename_required));
return;
}
if (newFilename.contains("/")) {
showToastMessage(R.string.alert_filename_format);
mNewFilename.setError(getString(R.string.alert_filename_format));
return;
}
int key_size = Integer.parseInt(mKeyLength.getText().toString());
if (key_size < 1024) {
showToastMessage(R.string.alert_too_short_key_size);
mNewFilename.setError(getString(R.string.alert_too_short_key_size));
return;
}
if (key_size > 16384) {
showToastMessage(R.string.alert_too_long_key_size);
mNewFilename.setError(getString(R.string.alert_too_long_key_size));
return;
}
int type = mDSAButton.isChecked() ? KeyPair.DSA : KeyPair.RSA;
File newKey = new File(PrivateKeyUtils.getPrivateKeyFolder(), newFilename);
File newPubKey = new File(PrivateKeyUtils.getPublicKeyFolder(), newFilename);
try {
JSch jsch = new JSch();
KeyPair kpair = KeyPair.genKeyPair(jsch, type, key_size);
kpair.writePrivateKey(new FileOutputStream(newKey));
kpair.writePublicKey(new FileOutputStream(newPubKey), "sgit");
kpair.dispose();
} catch (Exception e) {
// TODO
e.printStackTrace();
}
((PrivateKeyManageActivity) getActivity()).refreshList();
}
use of com.jcraft.jsch.JSch in project testcases by coheigea.
the class SCPTest method setupKnownHosts.
// Taken from Apache Camel test source
private void setupKnownHosts(int port) {
String knownHostsFile = SCP_ROOT_DIR + "/" + KNOWN_HOSTS;
// For security reasons (avoiding man in the middle attacks),
// camel-jsch will only connect to known hosts. For unit testing
// we use a known key, but since the port is dynamic, the
// known_hosts file will be generated by the following code and
// should contain a line like below (if
// "HashKnownHosts"=="yes" the hostname:port part will be
// hashed and look a bit more complicated).
//
// [localhost]:21000 ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDd \
// fIWeSV4o68dRrKSzFd/Bk51E65UTmmSrmW0O1ohtzi6HzsDPjXgCtlTt3F \
// qTcfFfI92IlTr4JWqC9UK1QT1ZTeng0MkPQmv68hDANHbt5CpETZHjW5q4 \
// OOgWhVvj5IyOC2NZHtKlJBkdsMAa15ouOOJLzBvAvbqOR/yUROsEiQ==
JSch jsch = new JSch();
try {
jsch.setKnownHosts(knownHostsFile);
Session s = jsch.getSession("alice", "localhost", port);
s.setConfig("StrictHostKeyChecking", "ask");
// TODO: by the current jsch (0.1.51) setting "HashKnownHosts" to "no" is a workaround
// to make the tests run green, see also http://sourceforge.net/p/jsch/bugs/63/
s.setConfig("HashKnownHosts", "no");
s.setUserInfo(new UserInfo() {
@Override
public String getPassphrase() {
return null;
}
@Override
public String getPassword() {
return "security";
}
@Override
public boolean promptPassword(String message) {
return true;
}
@Override
public boolean promptPassphrase(String message) {
return false;
}
@Override
public boolean promptYesNo(String message) {
// accept host authenticity
return true;
}
@Override
public void showMessage(String message) {
}
});
// in the process of connecting, "[localhost]:<port>" is added to the knownHostsFile
s.connect();
s.disconnect();
} catch (JSchException e) {
e.printStackTrace();
}
}
use of com.jcraft.jsch.JSch in project arduino-eclipse-plugin by Sloeber.
the class SSHUpload method uploadUsingPreferences.
@Override
public boolean uploadUsingPreferences(IFile hexFile, BoardDescriptor boardDescriptor, IProgressMonitor monitor) {
boolean ret = true;
if (boardDescriptor.usesProgrammer()) {
this.myHighLevelConsoleStream.println(Messages.Upload_error_network);
return false;
}
Session session = null;
SCP scp = null;
try {
JSch jSch = new JSch();
SSHClientSetupChainRing sshClientSetupChain = new SSHConfigFileSetup(new SSHPwdSetup());
BoardPort boardPort = new BoardPort();
boardPort.setBoardName(this.myHost);
session = sshClientSetupChain.setup(boardPort, jSch);
if (session != null) {
// $NON-NLS-1$ //$NON-NLS-2$
session.setConfig("PreferredAuthentications", "publickey,keyboard-interactive,password");
session.connect(30000);
scp = new SCP(session);
SSH ssh = new SSH(session);
this.myHighLevelConsoleStream.println(Messages.Upload_sending_sketch + hexFile + Messages.Upload_to + this.myHost);
scpFiles(scp, hexFile);
this.myHighLevelConsoleStream.println(Messages.Upload_sketch_on_yun);
String remoteUploadCommand = Common.getBuildEnvironmentVariable(this.myProject, // $NON-NLS-1$ //$NON-NLS-2$
"A.TOOLS." + this.myUpLoadTool.toUpperCase() + "_REMOTE.UPLOAD.PATTERN", // $NON-NLS-1$
"run-avrdude /tmp/sketch.hex ");
// $NON-NLS-1$
this.myHighLevelConsoleStream.println("merge-sketch-with-bootloader.lua /tmp/sketch.hex");
ret = // $NON-NLS-1$
ssh.execSyncCommand(// $NON-NLS-1$
"merge-sketch-with-bootloader.lua /tmp/sketch.hex", // $NON-NLS-1$
this.myOutconsole, this.myErrconsole);
// $NON-NLS-1$
this.myHighLevelConsoleStream.println("kill-bridge");
// $NON-NLS-1$
ssh.execSyncCommand("kill-bridge", this.myOutconsole, this.myErrconsole);
this.myHighLevelConsoleStream.println(remoteUploadCommand);
ret = ret && ssh.execSyncCommand(remoteUploadCommand, this.myOutconsole, this.myErrconsole);
}
} catch (JSchException e) {
String message = e.getMessage();
String errormessage = new String();
if (Messages.Upload_auth_cancel.equals(message) || Messages.Upload_auth_fail.equals(message)) {
errormessage = new String(Messages.Upload_error_auth_fail) + this.myHost;
// TODO add to ask if if the user wants to remove the password
PasswordManager.ErasePassword(this.myHost);
}
if (e.getMessage().contains(Messages.Upload_connection_refused)) {
errormessage = new String(Messages.Upload_error_connection_refused) + this.myHost;
}
this.myHighLevelConsoleStream.println(errormessage);
this.myHighLevelConsoleStream.println(message);
return false;
} catch (Exception e) {
this.myHighLevelConsoleStream.println(e.getMessage());
return false;
} finally {
if (scp != null) {
scp.close();
}
if (session != null) {
session.disconnect();
}
}
return ret;
}
Aggregations