use of com.jcraft.jsch.JSch in project azure-tools-for-java by Microsoft.
the class SparkSubmitHelper method sftpFileToEmulator.
public String sftpFileToEmulator(String localFile, String folderPath, IClusterDetail clusterDetail) throws IOException, HDIException, JSchException, SftpException {
EmulatorClusterDetail emulatorClusterDetail = (EmulatorClusterDetail) clusterDetail;
final File file = new File(localFile);
try (FileInputStream fileInputStream = new FileInputStream(file)) {
try (BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream)) {
String sshEndpoint = emulatorClusterDetail.getSSHEndpoint();
URL url = new URL(sshEndpoint);
String host = url.getHost();
int port = url.getPort();
JSch jsch = new JSch();
Session session = jsch.getSession(emulatorClusterDetail.getHttpUserName(), host, port);
session.setPassword(emulatorClusterDetail.getHttpPassword());
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
channel.connect();
String[] folders = folderPath.split("/");
for (String folder : folders) {
if (folder.length() > 0) {
try {
channel.cd(folder);
} catch (SftpException e) {
channel.mkdir(folder);
channel.cd(folder);
}
}
}
channel.put(bufferedInputStream, file.getName());
channel.disconnect();
session.disconnect();
return file.getName();
}
}
}
use of com.jcraft.jsch.JSch in project azure-tools-for-java by Microsoft.
the class AddNewEmulatorForm method preEmulatorSetupCheck.
private void preEmulatorSetupCheck() {
isCarryOnNextStep = true;
errorMessage = null;
emulatorSetupLog = new StringBuilder();
errorMessageField.setText("");
emulatorLogPanel.setVisible(false);
clusterName = clusterNameField.getText().trim();
userName = userNameField.getText().trim();
password = String.valueOf(passwordField.getPassword());
livyEndpoint = livyEndpointField.getText().trim().replaceAll("/+$", "");
sshEndpoint = sshEndpointField.getText().trim().replaceAll("/+$", "");
sparkHistoryEndpoint = sparkHistoryEndpointField.getText().trim().replaceAll("/+$", "");
ambariEndpoint = ambariEndpointField.getText().trim().replaceAll("/+$", "");
try {
host = new URI(sshEndpoint).getHost();
sshPort = new URI(sshEndpoint).getPort();
jsch = new JSch();
config = new Properties();
config.put("StrictHostKeyChecking", "no");
} catch (Exception exception) {
errorMessage = exception.getMessage();
isCarryOnNextStep = false;
}
if (isCarryOnNextStep && StringHelper.isNullOrWhiteSpace(clusterName) || StringHelper.isNullOrWhiteSpace(sshEndpoint) || StringHelper.isNullOrWhiteSpace(livyEndpoint) || StringHelper.isNullOrWhiteSpace(userName) || StringHelper.isNullOrWhiteSpace(password) || StringHelper.isNullOrWhiteSpace(sparkHistoryEndpoint) || StringHelper.isNullOrWhiteSpace(ambariEndpoint)) {
errorMessage = "Cluster Name, Endpoint fields, User Name, or Password shouldn't be empty";
isCarryOnNextStep = false;
} else if (isCarryOnNextStep && ClusterManagerEx.getInstance().isEmulatorClusterExist(clusterName)) {
errorMessage = "Cluster Name already exist in current list";
isCarryOnNextStep = false;
} else if (isCarryOnNextStep && !checkSshEndpoint()) {
errorMessage = "Could not ssh to emulator, please check username, password and ssh port.";
isCarryOnNextStep = false;
}
}
use of com.jcraft.jsch.JSch in project azure-tools-for-java by Microsoft.
the class AzureDockerCertVaultOps method generateSSHKeys.
public static AzureDockerCertVault generateSSHKeys(String passPhrase, String comment) throws AzureDockerException {
try {
AzureDockerCertVault result = new AzureDockerCertVault();
JSch jsch = new JSch();
KeyPair keyPair = KeyPair.genKeyPair(jsch, KeyPair.RSA);
ByteArrayOutputStream privateKeyBuff = new ByteArrayOutputStream(2048);
ByteArrayOutputStream publicKeyBuff = new ByteArrayOutputStream(2048);
keyPair.writePublicKey(publicKeyBuff, (comment != null) ? comment : "DockerSSHCerts");
if (passPhrase == null || passPhrase.isEmpty()) {
keyPair.writePrivateKey(privateKeyBuff);
} else {
keyPair.writePrivateKey(privateKeyBuff, passPhrase.getBytes());
}
result.sshKey = privateKeyBuff.toString();
result.sshPubKey = publicKeyBuff.toString();
return result;
} catch (Exception e) {
throw new AzureDockerException(e.getMessage());
}
}
use of com.jcraft.jsch.JSch in project GNS by MobilityFirst.
the class UserAuthPubKey method main.
/**
*
* @param arg
*/
public static void main(String[] arg) {
try {
JSch jsch = new JSch();
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle("Choose your privatekey(ex. ~/.ssh/id_dsa)");
chooser.setFileHidingEnabled(false);
int returnVal = chooser.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("You chose " + chooser.getSelectedFile().getAbsolutePath() + ".");
// , "passphrase"
jsch.addIdentity(// , "passphrase"
chooser.getSelectedFile().getAbsolutePath());
}
String host = null;
if (arg.length > 0) {
host = arg[0];
} else {
host = JOptionPane.showInputDialog("Enter username@hostname", System.getProperty("user.name") + "@localhost");
}
String user = host.substring(0, host.indexOf('@'));
host = host.substring(host.indexOf('@') + 1);
Session session = jsch.getSession(user, host, 22);
// username and passphrase will be given via UserInfo interface.
UserInfo ui = new UserInfoPrompted();
session.setUserInfo(ui);
session.connect();
Channel channel = session.openChannel("shell");
channel.setInputStream(System.in);
channel.setOutputStream(System.out);
channel.connect();
} catch (HeadlessException | JSchException e) {
System.out.println(e);
}
}
use of com.jcraft.jsch.JSch in project GNS by MobilityFirst.
the class SSHClient method scpTo.
/**
*
* @param user
* @param host
* @param keyFile
* @param lfile
* @param rfile
*/
public static void scpTo(String user, String host, File keyFile, String lfile, String rfile) {
if (verbose) {
System.out.println("Remote copy file from " + lfile + " to " + host + "@" + user + ":" + rfile);
}
FileInputStream fis = null;
try {
JSch jsch = new JSch();
Session session = authenticateWithKey(jsch, user, host, keyFile);
// username and password will be given via UserInfo interface.
UserInfo ui = new UserInfoPrompted();
session.setUserInfo(ui);
session.connect();
boolean ptimestamp = true;
// exec 'scp -t rfile' remotely
String command = "scp " + (ptimestamp ? "-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();
int ack = checkAck(in);
if (ack != 0) {
System.out.println("ACK was " + ack);
return;
}
File _lfile = new File(lfile);
if (ptimestamp) {
command = "T " + (_lfile.lastModified() / 1000) + " 0";
// The access time should be sent here,
// but it is not accessible with JavaAPI ;-<
command += (" " + (_lfile.lastModified() / 1000) + " 0\n");
out.write(command.getBytes());
out.flush();
ack = checkAck(in);
if (ack != 0) {
System.out.println("ACK was " + ack);
return;
}
}
// send "C0644 filesize filename", where filename should not include '/'
long filesize = _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();
ack = checkAck(in);
if (ack != 0) {
System.out.println("ACK was " + ack);
return;
}
// send a content of lfile
fis = new FileInputStream(lfile);
byte[] buf = new byte[1024];
while (true) {
int len = fis.read(buf, 0, buf.length);
if (len <= 0) {
break;
}
//out.flush();
out.write(buf, 0, len);
}
fis.close();
fis = null;
// send '\0'
buf[0] = 0;
out.write(buf, 0, 1);
out.flush();
ack = checkAck(in);
if (ack != 0) {
System.out.println("ACK was " + ack);
return;
}
out.close();
channel.disconnect();
session.disconnect();
} catch (JSchException | IOException e) {
GNSConfig.getLogger().severe(e.toString());
try {
if (fis != null) {
fis.close();
}
} catch (Exception ee) {
}
}
}
Aggregations