Skip to content

Instantly share code, notes, and snippets.

@dwelch2344
Created June 15, 2012 14:14
Show Gist options
  • Save dwelch2344/2936693 to your computer and use it in GitHub Desktop.
Save dwelch2344/2936693 to your computer and use it in GitHub Desktop.
SFTP experiments with Mina
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>SftpServer</artifactId>
<version>1.0.0-RELEASE</version>
<dependencies>
<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-core</artifactId>
<version>0.5.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.1</version>
</dependency>
</dependencies>
</project>
package com.example.sftp;
import java.util.ArrayList;
import java.util.List;
import org.apache.sshd.SshServer;
import org.apache.sshd.common.NamedFactory;
import org.apache.sshd.server.Command;
import org.apache.sshd.server.CommandFactory;
import org.apache.sshd.server.PasswordAuthenticator;
import org.apache.sshd.server.command.ScpCommandFactory;
import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
import org.apache.sshd.server.session.ServerSession;
import org.apache.sshd.server.sftp.SftpSubsystem;
public class SftpServer {
public static void main(String[] args) throws Exception {
SshServer sshd = SshServer.setUpDefaultServer();
sshd.setPort(22999);
sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider("hostkey.ser"));
sshd.setPasswordAuthenticator(new PasswordAuthenticator() {
public boolean authenticate(String username, String password,
ServerSession session) {
// TODO Auto-generated method stub
return true;
}
});
CommandFactory myCommandFactory = new CommandFactory() {
public Command createCommand(String command) {
System.out.println("Command: " + command);
return null;
}
};
sshd.setCommandFactory(new ScpCommandFactory(myCommandFactory));
List<NamedFactory<Command>> namedFactoryList = new ArrayList<NamedFactory<Command>>();
// this needs to be stubbed out based on your implementation
// namedFactoryList.add( new NamedFactory<Command>(){
//
// public Command create() {
// SftpSubsystem sftp = new SftpSubsystem();
//
// return sftp;
// }
//
// public String getName() {
// return "SFTP stuff";
// }
//
// });
namedFactoryList.add(new SftpSubsystem.Factory());
sshd.setSubsystemFactories(namedFactoryList);
sshd.start();
System.out.println("Press enter to quit");
System.in.read();
sshd.stop();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment