Skip to content

Instantly share code, notes, and snippets.

@playerjamesbattleground
Last active June 2, 2016 13:52
Show Gist options
  • Save playerjamesbattleground/e58de50d8fc41f9ed2e53181de0ab8ff to your computer and use it in GitHub Desktop.
Save playerjamesbattleground/e58de50d8fc41f9ed2e53181de0ab8ff to your computer and use it in GitHub Desktop.
<!-- http://mvnrepository.com/artifact/org.apache.sshd/apache-sshd -->
<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>apache-sshd</artifactId>
<version>1.2.0</version>
<type>pom</type>
</dependency>
<!-- http://mvnrepository.com/artifact/org.apache.sshd/sshd-sftp -->
<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-sftp</artifactId>
<version>0.11.0</version>
</dependency>
package test.me.sftp;
import java.io.IOException;
import java.security.PublicKey;
import java.util.Arrays;
import org.apache.sshd.common.NamedFactory;
import org.apache.sshd.server.Command;
import org.apache.sshd.server.SshServer;
import org.apache.sshd.server.auth.password.PasswordAuthenticator;
import org.apache.sshd.server.auth.password.PasswordChangeRequiredException;
import org.apache.sshd.server.auth.pubkey.PublickeyAuthenticator;
import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
import org.apache.sshd.server.scp.ScpCommandFactory;
import org.apache.sshd.server.session.ServerSession;
import org.apache.sshd.server.shell.InteractiveProcessShellFactory;
import org.apache.sshd.server.subsystem.sftp.SftpSubsystemFactory;
public class SFTPServer {
public static void main(String[] args) {
SshServer sshd = SshServer.setUpDefaultServer();
sshd.setPort(22001);
sshd.setSubsystemFactories(Arrays.<NamedFactory<Command>>asList(new SftpSubsystemFactory()));
sshd.setCommandFactory(new ScpCommandFactory());
sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider());
sshd.setPasswordAuthenticator(new PasswordAuthenticator() {
public boolean authenticate(String arg0, String arg1, ServerSession arg2)
throws PasswordChangeRequiredException {
System.out.println("Password auth: "+arg0);
return true;
}
});
sshd.setPublickeyAuthenticator(new PublickeyAuthenticator() {
public boolean authenticate(String arg0, PublicKey arg1, ServerSession arg2) {
System.out.println("Key auth");
return true;
}
});
sshd.setShellFactory(new InteractiveProcessShellFactory());
try {
sshd.start();
System.out.println(sshd.isOpen());
Thread.sleep(600000);
} catch (IOException e) {
System.out.println("Failed to start ssh server."+e);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment