use of com.jn.agileway.ssh.client.SshConnectionConfig in project agileway by fangjinuo.
the class SshCommandTest method testUseAgilewayJschAPI.
@Test
public void testUseAgilewayJschAPI() throws Throwable {
SshConnectionFactoryRegistry registry = new SshConnectionFactoryRegistry();
SshConnectionFactory connectionFactory = registry.get("jsch");
SshConnectionConfig connectionConfig = connectionFactory.newConfig();
String password = "fjn13570";
String username = "fangjinuo";
// String host = "192.168.1.79";
String host = "192.168.234.128";
int port = 22;
connectionConfig.setHost(host);
connectionConfig.setPort(port);
connectionConfig.setPassword(password);
connectionConfig.setUser(username);
SshConnection connection = connectionFactory.get(connectionConfig);
executeAndDump(connection, "route");
executeAndDump(connection, "ifconfig");
executeAndDump(connection, "cd ~/.java;ls -al");
connection.close();
}
use of com.jn.agileway.ssh.client.SshConnectionConfig in project agileway by fangjinuo.
the class SftpTests method _test.
void _test(SshConnectionFactory connectionFactory, final String testWorkingDirectory) throws IOException {
SshConnectionConfig connectionConfig = connectionFactory.newConfig();
// connectionConfig.setHost("192.168.234.128");
connectionConfig.setHost("192.168.1.70");
connectionConfig.setPort(22);
connectionConfig.setUser("fangjinuo");
connectionConfig.setPassword("fjn13570");
SshConnection connection = null;
SftpSession _session = null;
try {
connection = connectionFactory.get(connectionConfig);
final SftpSession session = connection.openSftpSession();
_session = session;
FileAttrs attrs = session.stat("/home/fangjinuo");
System.out.println(attrs);
// 确保testWorkingDirectory 存在,并且是 empty的
boolean testWorkingDirectoryExist = Sftps.existDirectory(session, testWorkingDirectory);
logger.info("directory exist? {}", testWorkingDirectoryExist);
if (!testWorkingDirectoryExist) {
session.mkdir(testWorkingDirectory, null);
testWorkingDirectoryExist = Sftps.existDirectory(session, testWorkingDirectory);
logger.info("directory exist? {}", testWorkingDirectoryExist);
}
List<SftpResourceInfo> children = session.listFiles(testWorkingDirectory);
if (!children.isEmpty()) {
Sftps.removeDir(session, testWorkingDirectory, true);
}
// 拷贝 agileway-sshclient 模块下所有的文件到 testWorkingDirectory
String localRootPath = SystemPropertys.getUserWorkDir();
File localRootDir = new File(localRootPath);
_copyDir(session, localRootDir, testWorkingDirectory);
} catch (Throwable ex) {
logger.error(ex.getMessage(), ex);
} finally {
IOs.close(_session);
IOs.close(connection);
}
}
use of com.jn.agileway.ssh.client.SshConnectionConfig in project agileway by fangjinuo.
the class Sftps method scp.
public static void scp(@NotEmpty String localPath, @NotEmpty String remotePath, @NotEmpty String remoteHost, int remotePort, @NotEmpty String remoteUser, String remotePswd, boolean reverse) throws IOException, SftpException {
Preconditions.checkNotEmpty(localPath, "the local path is required");
Preconditions.checkNotEmpty(remotePath, "the remote path is required");
if (remotePort <= 0) {
remotePort = 22;
}
SshConnectionFactory connectionFactory = SSH_CONNECTION_FACTORY_REGISTRY.getDefault();
SshConnectionConfig connectionConfig = connectionFactory.newConfig();
connectionConfig.setHost(remoteHost);
connectionConfig.setPort(remotePort);
connectionConfig.setUser(remoteUser);
connectionConfig.setPassword(remotePswd);
SshConnection connection = null;
SftpSession session = null;
try {
connection = connectionFactory.get(connectionConfig);
session = connection.openSftpSession();
if (reverse) {
File localDir = new File(localPath);
if (localDir.exists() && !localDir.isDirectory()) {
String error = StringTemplates.formatWithIndex("local path is not a directory: {}", localPath);
throw new IOException(error);
}
reverseCopy(session, localDir, remotePath);
} else {
File localFile = new File(localPath);
if (!localFile.exists()) {
logger.error("local path is not exists: {}", localPath);
throw new FileNotFoundException(localPath);
}
copy(session, localFile, remotePath);
}
} finally {
IOs.close(session);
IOs.close(connection);
}
}
use of com.jn.agileway.ssh.client.SshConnectionConfig in project agileway by fangjinuo.
the class SftpFileProvider method doCreateFileSystem.
@Override
protected FileSystem doCreateFileSystem(FileName rootName, FileSystemOptions fileSystemOptions) throws FileSystemException {
// Create the file system
final GenericFileName root = (GenericFileName) rootName;
SshConnectionFactory sshConnectionFactory = new SshConnectionFactoryRegistry().getDefault();
SshConnectionConfig connectionConfig = sshConnectionFactory.newConfig();
connectionConfig.setHost(root.getHostName());
connectionConfig.setPort(root.getPort());
connectionConfig.setUser(root.getUserName());
connectionConfig.setPassword(root.getPassword());
SftpFileSystemConfigBuilder configBuilder = (SftpFileSystemConfigBuilder) getConfigBuilder();
final File knownHostsFile = configBuilder.getKnownHosts(fileSystemOptions);
if (knownHostsFile != null) {
connectionConfig.setKnownHostsPath(knownHostsFile.getAbsolutePath());
}
// JSCH 特有属性:
Integer timout = configBuilder.getTimeout(fileSystemOptions);
if (timout != null) {
connectionConfig.setProperty("ConnectTimeout", timout);
}
String strictHostKeyChecking = configBuilder.getStrictHostKeyChecking(fileSystemOptions);
if (Strings.isNotEmpty(strictHostKeyChecking)) {
connectionConfig.setProperty("StrictHostKeyChecking", strictHostKeyChecking);
}
final String preferredAuthentications = configBuilder.getPreferredAuthentications(fileSystemOptions);
if (preferredAuthentications != null) {
connectionConfig.setProperty("PreferredAuthentications", preferredAuthentications);
}
// set compression property
final String compression = configBuilder.getCompression(fileSystemOptions);
if (compression != null) {
connectionConfig.setProperty("compression.s2c", compression);
connectionConfig.setProperty("compression.c2s", compression);
}
SshConnection sshConnection = sshConnectionFactory.get(connectionConfig);
SftpSession session = sshConnection.openSftpSession();
return new SftpFileSystem(root, session, null, fileSystemOptions);
}
Aggregations