use of com.jcraft.jsch.ChannelSftp in project quickstarts by jboss-switchyard.
the class CamelFtpBindingTest method startUp.
@BeforeClass
public static void startUp() throws Exception {
FtpServerFactory serverFactory = new FtpServerFactory();
ListenerFactory listenerFactory = new ListenerFactory();
listenerFactory.setPort(2222);
serverFactory.addListener("default", listenerFactory.createListener());
ListenerFactory sslListenerFactory = new ListenerFactory();
sslListenerFactory.setPort(2221);
SslConfigurationFactory ssl = new SslConfigurationFactory();
ssl.setKeystoreFile(new File("src/test/resources/ftpserver.jks"));
ssl.setKeystorePassword("password");
sslListenerFactory.setSslConfiguration(ssl.createSslConfiguration());
// Setting it to true will not read the file
sslListenerFactory.setImplicitSsl(false);
serverFactory.addListener("ftps", sslListenerFactory.createListener());
PropertiesUserManagerFactory managerFactory = new PropertiesUserManagerFactory();
managerFactory.setPasswordEncryptor(new ClearTextPasswordEncryptor());
managerFactory.setFile(new File("src/test/resources/users.properties"));
UserManager createUserManager = managerFactory.createUserManager();
serverFactory.setUserManager(createUserManager);
NativeFileSystemFactory fileSystemFactory = new NativeFileSystemFactory();
fileSystemFactory.setCreateHome(true);
serverFactory.setFileSystem(fileSystemFactory);
File file = new File("target/ftp/ftps");
file.mkdirs();
file = new File("target/ftp/sftp");
file.mkdirs();
ftpServer = serverFactory.createServer();
ftpServer.start();
SshServer sshd = SshServer.setUpDefaultServer();
sshd.setPort(2220);
sshd.setKeyPairProvider(createTestKeyPairProvider("src/test/resources/hostkey.pem"));
sshd.setSubsystemFactories(Arrays.<NamedFactory<Command>>asList(new SftpSubsystem.Factory()));
sshd.setCommandFactory(new ScpCommandFactory());
sshd.setPasswordAuthenticator(new BogusPasswordAuthenticator());
sshd.start();
JSch sch = new JSch();
Session session = sch.getSession("camel", "localhost", 2220);
session.setUserInfo(new SimpleUserInfo("isMyFriend"));
session.connect();
ChannelSftp c = (ChannelSftp) session.openChannel("sftp");
c.connect();
System.out.println("Home: " + c.getHome());
c.chmod(777, ".");
c.chmod(777, "target");
c.chmod(777, "target/ftp");
c.chmod(777, "target/ftp/sftp");
c.disconnect();
session.disconnect();
}
use of com.jcraft.jsch.ChannelSftp in project che by eclipse.
the class JschSshClient method copyFile.
private void copyFile(String sourcePath, String targetPath) throws MachineException {
ChannelSftp sftp = null;
try {
sftp = (ChannelSftp) session.openChannel("sftp");
sftp.connect(connectionTimeout);
String absoluteTargetPath = getAbsolutePath(targetPath);
copyFile(sourcePath, absoluteTargetPath, sftp);
} catch (JSchException e) {
throw new MachineException("Sftp copying failed. Error: " + e.getLocalizedMessage());
} finally {
if (sftp != null) {
sftp.disconnect();
}
}
}
use of com.jcraft.jsch.ChannelSftp in project voltdb by VoltDB.
the class ExportOnServerVerifier method checkForMoreFilesRemote.
@SuppressWarnings("unchecked")
private void checkForMoreFilesRemote(Comparator<String> comparator) throws Exception {
int onDoneRetries = 6;
long start_time = System.currentTimeMillis();
while (m_exportFiles.isEmpty()) {
/*
* Collect the list of remote files at each node
* Sort the list from each node
*/
int activeFound = 0;
List<Pair<ChannelSftp, List<String>>> pathsFromAllNodes = new ArrayList<Pair<ChannelSftp, List<String>>>();
for (RemoteHost rh : m_hosts) {
Vector<LsEntry> files = rh.channel.ls(rh.path);
List<String> paths = new ArrayList<String>();
final int trackerModifyTime = rh.channel.stat(rh.path + "/" + TRACKER_FILENAME).getMTime();
boolean activeInRemote = false;
boolean filesInRemote = false;
for (LsEntry entry : files) {
activeInRemote = activeInRemote || entry.getFilename().trim().toLowerCase().startsWith("active");
filesInRemote = filesInRemote || entry.getFilename().trim().toLowerCase().startsWith("active");
if (!entry.getFilename().equals(".") && !entry.getFilename().equals("..") && !entry.getAttrs().isDir()) {
final String entryFileName = rh.path + "/" + entry.getFilename();
final int entryModifyTime = entry.getAttrs().getMTime();
if (!entry.getFilename().contains("active")) {
Matcher mtc = EXPORT_FILENAME_REGEXP.matcher(entry.getFilename());
if (mtc.matches()) {
paths.add(entryFileName);
activeInRemote = activeInRemote || entryModifyTime > trackerModifyTime;
filesInRemote = true;
} else {
System.err.println("ERROR: " + entryFileName + " does not match expected export file name pattern");
}
} else if (entry.getFilename().trim().toLowerCase().startsWith("active-")) {
if ((trackerModifyTime - entryModifyTime) > 120) {
final String renamed = rh.path + "/" + entry.getFilename().substring("active-".length());
rh.channel.rename(entryFileName, renamed);
paths.add(renamed);
}
}
}
}
touchActiveTracker(rh);
rh.activeSeen = rh.activeSeen || activeInRemote;
rh.fileSeen = rh.fileSeen || filesInRemote;
if (activeInRemote)
activeFound++;
Collections.sort(paths, comparator);
if (!paths.isEmpty())
pathsFromAllNodes.add(Pair.of(rh.channel, paths));
}
if (!m_clientComplete.isEmpty()) {
printExportFileSituation(pathsFromAllNodes, activeFound);
}
if (pathsFromAllNodes.isEmpty() && activeFound == 0 && allActiveSeen()) {
if (--onDoneRetries <= 0)
return;
Thread.sleep(5000);
}
// add them to m_exportFiles as ordered by the comparator
TreeMap<String, Pair<ChannelSftp, String>> hadPaths = new TreeMap<String, Pair<ChannelSftp, String>>(comparator);
for (Pair<ChannelSftp, List<String>> p : pathsFromAllNodes) {
final ChannelSftp c = p.getFirst();
for (String path : p.getSecond()) {
hadPaths.put(path, Pair.of(c, path));
}
}
boolean hadOne = !hadPaths.isEmpty();
Iterator<Map.Entry<String, Pair<ChannelSftp, String>>> itr = hadPaths.entrySet().iterator();
while (itr.hasNext()) {
Map.Entry<String, Pair<ChannelSftp, String>> entry = itr.next();
m_exportFiles.offer(entry.getValue());
itr.remove();
}
long now = System.currentTimeMillis();
if ((now - start_time) > FILE_TIMEOUT_MS) {
throw new ValidationErr("Timed out waiting on new files.\n" + "This indicates a mismatch in the transaction streams between the client logs and the export data or the death of something important.", null, null);
} else if (!hadOne) {
Thread.sleep(1200);
}
}
}
use of com.jcraft.jsch.ChannelSftp in project voltdb by VoltDB.
the class ExportOnServerVerifier method verifySetup.
boolean verifySetup(String[] args) throws Exception {
String[] remoteHosts = args[0].split(",");
final String homeDir = System.getProperty("user.home");
final String sshDir = homeDir + File.separator + ".ssh";
final String sshConfigPath = sshDir + File.separator + "config";
//Oh yes...
loadAllPrivateKeys(new File(sshDir));
OpenSshConfig sshConfig = null;
if (new File(sshConfigPath).exists()) {
sshConfig = new OpenSshConfig(new File(sshConfigPath));
}
final String defaultKnownHosts = sshDir + "/known_hosts";
if (new File(defaultKnownHosts).exists()) {
m_jsch.setKnownHosts(defaultKnownHosts);
}
for (String hostString : remoteHosts) {
String[] split = hostString.split(":");
String host = split[0];
RemoteHost rh = new RemoteHost();
rh.path = split[1];
String user = System.getProperty("user.name");
int port = 22;
File identityFile = null;
String configHost = host;
if (sshConfig != null) {
OpenSshConfig.Host hostConfig = sshConfig.lookup(host);
if (hostConfig.getUser() != null) {
user = hostConfig.getUser();
}
if (hostConfig.getPort() != -1) {
port = hostConfig.getPort();
}
if (hostConfig.getIdentityFile() != null) {
identityFile = hostConfig.getIdentityFile();
}
if (hostConfig.getHostName() != null) {
configHost = hostConfig.getHostName();
}
}
Session session = null;
if (identityFile != null) {
JSch jsch = new JSch();
jsch.addIdentity(identityFile.getAbsolutePath());
session = jsch.getSession(user, configHost, port);
} else {
session = m_jsch.getSession(user, configHost, port);
}
rh.session = session;
session.setConfig("StrictHostKeyChecking", "no");
session.setDaemonThread(true);
session.connect();
final ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
rh.channel = channel;
channel.connect();
touchActiveTracker(rh);
m_hosts.add(rh);
}
m_partitions = Integer.parseInt(args[1]);
for (int i = 0; i < m_partitions; i++) {
m_rowTxnIds.put(i, new TreeMap<Long, Long>());
m_maxPartTxId.put(i, Long.MIN_VALUE);
m_checkedUpTo.put(i, 0);
m_readUpTo.put(i, new AtomicLong(0));
}
m_clientPath = new File(args[2]);
if (!m_clientPath.exists() || !m_clientPath.isDirectory()) {
if (!m_clientPath.mkdir()) {
throw new IOException("Issue with transaction ID path");
}
}
for (RemoteHost rh : m_hosts) {
boolean existsOrIsDir = true;
try {
SftpATTRS stat = rh.channel.stat(rh.path);
if (!stat.isDir()) {
existsOrIsDir = false;
}
} catch (SftpException e) {
if (e.id == ChannelSftp.SSH_FX_NO_SUCH_FILE) {
existsOrIsDir = false;
} else {
Throwables.propagate(e);
}
}
if (!existsOrIsDir) {
rh.channel.mkdir(rh.path);
}
}
boolean skinny = false;
if (args.length > 3 && args[3] != null && !args[3].trim().isEmpty()) {
skinny = Boolean.parseBoolean(args[3].trim().toLowerCase());
}
return skinny;
}
use of com.jcraft.jsch.ChannelSftp in project GNS by MobilityFirst.
the class SFTPUpload method authenticateSftp.
private static ChannelSftp authenticateSftp(String user, String host, File keyFile) throws JSchException {
Session session;
Channel channel;
JSch jsch = new JSch();
session = SSHClient.authenticateWithKey(jsch, user, host, keyFile);
session.connect();
channel = session.openChannel("sftp");
channel.connect();
return (ChannelSftp) channel;
}
Aggregations