use of org.geotoolkit.sml.xml.v100.Connection in project eladmin by lWoHvYe.
the class ScpClientUtil method putFile.
public void putFile(String localFile, String remoteFileName, String remoteTargetDirectory, String mode) {
Connection conn = new Connection(ip, port);
File file = new File(localFile);
if (file.isDirectory()) {
throw new RuntimeException(localFile + " is not a file");
}
String fileName = file.getName();
try {
conn.connect();
boolean isAuthenticated = conn.authenticateWithPassword(username, password);
if (!isAuthenticated) {
System.err.println("authentication failed");
}
var client = conn.createSCPClient();
if ((mode == null) || (mode.length() == 0))
mode = "0600";
var scpOutputStream = client.put(StrUtil.isNotEmpty(remoteFileName) ? remoteFileName : fileName, file.length(), remoteTargetDirectory, mode);
String content = IOUtils.toString(new FileInputStream(file), String.valueOf(StandardCharsets.UTF_8));
scpOutputStream.write(content.getBytes());
scpOutputStream.flush();
scpOutputStream.close();
} catch (IOException ex) {
Logger.getLogger(ScpClientUtil.class.getName()).log(Level.SEVERE, null, ex);
} finally {
conn.close();
}
}
use of org.geotoolkit.sml.xml.v100.Connection in project eladmin by lWoHvYe.
the class ScpClientUtil method getFile.
public void getFile(String remoteFile, String localTargetDirectory) {
Connection conn = new Connection(ip, port);
try {
conn.connect();
boolean isAuthenticated = conn.authenticateWithPassword(username, password);
if (!isAuthenticated) {
System.err.println("authentication failed");
}
var client = conn.createSCPClient();
var scpInputStream = client.get(remoteFile);
FileUtil.writeFromStream(scpInputStream, localTargetDirectory);
} catch (IOException ex) {
Logger.getLogger(SCPClient.class.getName()).log(Level.SEVERE, null, ex);
} finally {
conn.close();
}
}
use of org.geotoolkit.sml.xml.v100.Connection in project eladmin by lWoHvYe.
the class RemoteShellExecutor method login.
/**
* 登录
* @return
* @throws IOException
*/
private boolean login() throws IOException {
conn = new Connection(ip);
conn.connect();
return conn.authenticateWithPassword(osUsername, password);
}
use of org.geotoolkit.sml.xml.v100.Connection in project cosmic by MissionCriticalCloud.
the class SshHelper method scpTo.
public static void scpTo(final String host, final int port, final String user, final File pemKeyFile, final String password, final String remoteTargetDirectory, final byte[] data, final String remoteFileName, final String fileMode, final int connectTimeoutInMs, final int kexTimeoutInMs) throws Exception {
com.trilead.ssh2.Connection conn = null;
com.trilead.ssh2.SCPClient scpClient = null;
try {
conn = new com.trilead.ssh2.Connection(host, port);
conn.connect(null, connectTimeoutInMs, kexTimeoutInMs);
if (pemKeyFile == null) {
if (!conn.authenticateWithPassword(user, password)) {
final String msg = "Failed to authentication SSH user " + user + " on host " + host;
s_logger.error(msg);
throw new Exception(msg);
}
} else {
if (!conn.authenticateWithPublicKey(user, pemKeyFile, password)) {
final String msg = "Failed to authentication SSH user " + user + " on host " + host;
s_logger.error(msg);
throw new Exception(msg);
}
}
scpClient = conn.createSCPClient();
if (fileMode != null) {
scpClient.put(data, remoteFileName, remoteTargetDirectory, fileMode);
} else {
scpClient.put(data, remoteFileName, remoteTargetDirectory);
}
} finally {
if (conn != null) {
conn.close();
}
}
}
use of org.geotoolkit.sml.xml.v100.Connection in project cosmic by MissionCriticalCloud.
the class SshHelper method scpTo.
public static void scpTo(final String host, final int port, final String user, final File pemKeyFile, final String password, final String remoteTargetDirectory, final String localFile, final String fileMode, final int connectTimeoutInMs, final int kexTimeoutInMs) throws Exception {
com.trilead.ssh2.Connection conn = null;
com.trilead.ssh2.SCPClient scpClient = null;
try {
conn = new com.trilead.ssh2.Connection(host, port);
conn.connect(null, connectTimeoutInMs, kexTimeoutInMs);
if (pemKeyFile == null) {
if (!conn.authenticateWithPassword(user, password)) {
final String msg = "Failed to authentication SSH user " + user + " on host " + host;
s_logger.error(msg);
throw new Exception(msg);
}
} else {
if (!conn.authenticateWithPublicKey(user, pemKeyFile, password)) {
final String msg = "Failed to authentication SSH user " + user + " on host " + host;
s_logger.error(msg);
throw new Exception(msg);
}
}
scpClient = conn.createSCPClient();
if (fileMode != null) {
scpClient.put(localFile, remoteTargetDirectory, fileMode);
} else {
scpClient.put(localFile, remoteTargetDirectory);
}
} finally {
if (conn != null) {
conn.close();
}
}
}
Aggregations