I am trying to test files arriving in an SFTP server. I set up a local sshd server using the following code
#Before
public void setUp() throws Exception{
SshServer sshd = SshServer.setUpDefaultServer();
sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider());
sshd.setPasswordAuthenticator(new PasswordAuthenticator() {
public boolean authenticate(String username, String password, ServerSession session) {
return true;
}
});
sshd.setPort(22);
sshd.setFileSystemFactory(new FileSystemFactory() {
#Override
public FileSystem createFileSystem(org.apache.sshd.common.session.Session session) throws IOException {
return null;
}
});
sshd.start();
}
I use Jsch as a client to try and connect to this server. Unfortunately, whenever I try to call channel.connect(), I get com.jcraft.jsch.JSchException: failed to send channel request exception. The code for the Jsch is shown below:
public void testServer(){
try{
JSch jSch = new JSch();
Session session = jSch.getSession("user", "localhost",22);
Properties configTemp = new Properties();
configTemp.put("StrictHostKeyChecking", "no");
session.setConfig(configTemp);
session.connect();
ChannelSftp channel = (ChannelSftp)session.openChannel("sftp");
channel.connect();
if(channel.isConnected()){
System.out.println("Connected");
}
}
catch (Exception e){
e.printStackTrace();
}
}
Can anyone help with what I am doing wrong and how to fix this? Thank you
You have to implement a subsystem factory, like:
sshd.setSubsystemFactories(Collections.singletonList(new SftpSubsystemFactory()));
Related
I am using JSch to fetch data from remote server on brocade switch. A new session is created and a new channel is opened with type as 'shell'. I have few commands which fetches data from this server. I created new Channel for every command and disconnects channel after fetching data.
Now in Audit log on server shows new login for every channel which i created for each command.
Should login be shown for every session created instead of every channel connected?
public Session getSession(String hostName,
String username,
String password,
Integer port) throws Exception {
Session session = null;
try {
JSch jsch = new JSch();
if (port == null) {
port = 22;
}
session = jsch.getSession(username, hostName, port);
session.setPassword(password);
session.setConfig("max_input_buffer_size", Integer.toString(100 * 1024 * 1024));
session.setConfig("StrictHostKeyChecking", "no");
session.setConfig("UserKnownHostsFile", "/dev/null");
session.connect(15 * 1000);
} catch (Exception e) {
if (session != null) {
session.disconnect();
}
throw new Exception("Error in connecting to: " + hostName, e);
}
return session;
}
private Channel getChannel(Session session,String type) throws Exception {
if (session == null || !session.isConnected()) {
session = getSession();
}
Channel channel = null;
try {
channel = session.openChannel(type);
} catch (Exception e) {
throw e;
}
channel.setInputStream(null);
return channel;
}
SSH channel does not create a new login.
But it creates a new shell session. Maybe your "audit log" logs shell session as if it were a new login. With a normal SSH client, you typically create only one shell session for each login. So it's easy to mismatch these two things.
I ran the below program and reboot the host which takes almost 6 to 8 mins to complete the reboot, but i never get the timed out.. i.e is it never go to else part.
public static void main(String a[]) throws JSchException, InterruptedException {
java.util.Properties config = new java.util.Properties();
JSch jsch = new JSch();
String user = "cli";
String host = "135.104.217.114";
String password = "cli";
int port = 22;
Session session = jsch.getSession(user,host,port);
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.setPassword(password);
session.connect();
while(true)
{
if(session.isConnected())
{
System.out.println("session connected");
}
else
{
System.out.println("sesion not connected--------------");
}
Thread.sleep(1000);
}
}
You most likely need to configure the "Sever Alive" properties.
session.setServerAliveInterval(5000); // Check if server is alive every 5 seconds
session.setServerAliveCountMax(5); // Disconnect after 5 Server Alive checks did not receive a response.
I need to check the health of the SFTP connection using JSch liberary. I have used the following approach. Please correct me if I am wrong.
public boolean checkConnection() throws JSchException {
try {
JSch jsch = new JSch();
Session session = sch.getSession(username, host);
session.setPort(PORT);
session.setPassword(password);
session.connect();
return true;
} catch(Exception ex){
LOG.error("Error while connecting to SFTP", ex);
}
return false
}
When the code below has finished running, netstat -a|grep sftp shows an open SFTP connection. It also shows up as an open connection in JProfiler.
channel.isConnected() in the finally block prints false. Any ideas why the connections is not being closed as I'm at a loss?
public static void clean() {
com.jcraft.jsch.ChannelSftp channel = null;
try {
channel = Helper.openNewTLSftpChannel();
channel.connect();
channel.cd(remoteFileDirectory);
List<ChannelSftp.LsEntry> list = channel.ls("*." + fileType);
for (ChannelSftp.LsEntry file : list) {
String fileName = file.getFilename();
DateTime fileDate = new DateTime(parseDateFromFileName(fileName));
//if this file is older than the cutoff date, delete from the SFTP share
if (fileDate.compareTo(cleanupCutoffdate) < 0) {
channel.rm(fileName);
}
}
} catch (Exception exception) {
exception.printStackTrace();
} finally {
if (channel != null) {
channel.disconnect();
System.out.println(channel.isConnected());
}
}
}
Adding openNewTLSftpChannel() below:
public static ChannelSftp openNewSftpChannel(String privateKeyFileName, String password, String username, String host, int port)
throws ConfigurationErrorException {
JSch jsch = new JSch();
File sftpPrivateFile = new File(privateKeyFileName);
Channel channel;
try {
if (!sftpPrivateFile.canRead()) {
throw new ConfigurationErrorException("File access error: " + sftpPrivateFile.getAbsolutePath());
}
jsch.addIdentity(sftpPrivateFile.getAbsolutePath(), password);
Session session = jsch.getSession(username, host, port);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
channel = session.openChannel("sftp");
} catch (JSchException jschException) {
throw new ConfigurationErrorException("File access error: " + sftpPrivateFile.getAbsolutePath());
}
return (ChannelSftp) channel;
}
If you take a look at the JSCH examples for SFTP you'll see how the session is terminated:
//setup Session here
...
session.connect();
...
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftpChannel = (ChannelSftp) channel;
...run sftp logic...
//close sessions here
sftpChannel.exit();
session.disconnect();
You'll notice that there are two parts to the connection and disconnection; the Session object and the Channel object.
In my code I use the Session object to set my authentication information, and the Channel object to execute the sftp commands I need.
In your instance, you're creating the Session object in your openNewSftpChannel method, but it is never closed, hence your session stays alive.
For further context, check out the examples.
Robert H is correct, you need to exit your channel and disconnect your session. I wanted to add that the session exists even when the channel has been closed. Since you create your session within a try block inside a method, it seems you have lost your session, but you can get it back using 'getSession' on your sftpChannel channel.
You can change your finally block to this:
} finally {
if (channel != null) {
Session session = channel.getSession();
channel.disconnect();
session.disconnect();
System.out.println(channel.isConnected());
}
}
I'm using JSch for file upload over SFTP. In its current state each thread opens and closes connection when needed.
If it possible to use connection pooling with JSch in order to avoid overhead caused by large number of connection opening and closing?
Here is a example of function called from inside of thread
public static void file_upload(String filename) throws IOException {
JSch jsch = new JSch();
Session session = null;
try {
session = jsch.getSession("user", "server_name", 22);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword("super_secre_password");
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftpChannel = (ChannelSftp) channel;
FileInputStream inputSrr = new FileInputStream(filename);
try {
sftpChannel.put(inputSrr, "/var/temp/"+filename);
} catch (SftpException e) {
e.printStackTrace();
} finally {
if (inputSrr != null) {
inputSrr.close();
}
}
sftpChannel.exit();
session.disconnect();
} catch (JSchException e) {
e.printStackTrace();
} catch (SftpException e) {
e.printStackTrace();
}
}
For that I would prefer commons-pool. ;)
Here's an implementation of Ssh Connection pool
http://www.javacodegeeks.com/2013/02/pool-of-ssh-connections-using-apache-keyedobjectpool.html
you can use grep4j to use this pool
https://code.google.com/p/grep4j/source/browse/trunk/src/main/java/org/grep4j/core/command/linux/SessionFactory.java?r=354
Also make sure you can access the server from the execution machine. For instance if the target server is not in your reach. It'll throw connection timeout.
I wold like to share with you our implementation, We have used Session Manager of jsch-extension library
First of all you need to implement pool object factory that is responsible for lifecycle of pooled objects:
public class ChannelSftpConnectionsFactory extends BasePooledObjectFactory<ChannelSftp> {
private SessionManager sessionManager;
public ChannelSftpConnectionsFactory(final SessionManager sessionManager) {
this.sessionManager = sessionManager;
}
//Create and open channel
#Override
public ChannelSftp create() throws JSchException {
ChannelSftp channelSftp = (ChannelSftp) sessionManager.getSession().openChannel("sftp");
channelSftp.connect();
return channelSftp;
}
//wrapping
#Override
public PooledObject<ChannelSftp> wrap(final ChannelSftp channelSftp) {
return new DefaultPooledObject<>(channelSftp);
}
#Override
//disconnect channel on destroy
public void destroyObject(final PooledObject<ChannelSftp> pooledObject) {
ChannelSftp sftp = pooledObject.getObject();
disconnectChannel(sftp);
}
void disconnectChannel(final ChannelSftp sftp) {
if (sftp.isConnected()) {
sftp.disconnect();
}
}
#Override
//reset channel current folder to home if someone was walking on another folders
public void passivateObject(final PooledObject<ChannelSftp> p) {
ChannelSftp sftp = p.getObject();
try {
sftp.cd(sftp.getHome());
} catch (SftpException ex) {
log.error("Could not reset channel to home folder, closing it");
disconnectChannel(sftp);
}
}
#Override
//validate object before it is borrowed from pool. If false object will be removed from pool
public boolean validateObject(final PooledObject<ChannelSftp> p) {
ChannelSftp sftp = p.getObject();
return sftp.isConnected() && !sftp.isClosed();
}
}
Now you could create pool using configured factory:
ObjectPool<ChannelSftp> createPool(final SessionManager sessionManager, final GenericObjectPoolConfig<ChannelSftp> poolConfig) {
return PoolUtils.synchronizedPool(new GenericObjectPool<>(buildFactory(sessionManager), poolConfig));
}
PooledObjectFactory<ChannelSftp> buildFactory(final SessionManager sessionManager) {
return PoolUtils.synchronizedPooledFactory(new ChannelSftpConnectionsFactory(sessionManager));
}
This java doc would help you to configure pool properly : https://commons.apache.org/proper/commons-pool/api-2.6.0/org/apache/commons/pool2/impl/BaseGenericObjectPool.html
Do not forget about correct borrowing and returning of object into pool: https://commons.apache.org/proper/commons-pool/api-2.6.0/org/apache/commons/pool2/ObjectPool.html
Object obj = null;
try {
obj = pool.borrowObject();
try {
//...use the object...
} catch(Exception e) {
// invalidate the object
pool.invalidateObject(obj);
// do not return the object to the pool twice
obj = null;
} finally {
// make sure the object is returned to the pool
if(null != obj) {
pool.returnObject(obj);
}
}
} catch(Exception e) {
// failed to borrow an object
}