package cn.com.ultrapower.utils;
import java.io.IOException;
import java.security.PublicKey;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import com.hierynomus.sshj.transport.kex.DHGroups;
import com.hierynomus.sshj.transport.kex.ExtendedDHGroups;
import net.schmizz.sshj.DefaultConfig;
import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.common.Factory.Named;
import net.schmizz.sshj.common.IOUtils;
import net.schmizz.sshj.connection.ConnectionException;
import net.schmizz.sshj.connection.channel.direct.Session;
import net.schmizz.sshj.connection.channel.direct.Session.Command;
import net.schmizz.sshj.transport.TransportException;
import net.schmizz.sshj.transport.compression.Compression;
import net.schmizz.sshj.transport.compression.DelayedZlibCompression;
import net.schmizz.sshj.transport.compression.NoneCompression;
import net.schmizz.sshj.transport.compression.ZlibCompression;
import net.schmizz.sshj.transport.kex.Curve25519SHA256;
import net.schmizz.sshj.transport.kex.DHGexSHA1;
import net.schmizz.sshj.transport.kex.DHGexSHA256;
import net.schmizz.sshj.transport.kex.ECDHNistP;
import net.schmizz.sshj.transport.kex.KeyExchange;
import net.schmizz.sshj.transport.verification.HostKeyVerifier;
import net.schmizz.sshj.userauth.UserAuthException;
public class Connect2SSHDWithSSHJ {
public static void main(String args[]){
//ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256
String host = "IP地址";
int port = 22;
int timeout = 10;
String username = "用户名";
String password = "密码";
try {
connSSHD(username, password, host, port, timeout);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//diffie-hellman-group15-sha256
System.out.println(ExtendedDHGroups.Group15SHA256().getName());
//[email protected]
System.out.println(ExtendedDHGroups.Group15SHA256AtSSH().getName());
}
public static void connSSHD(String username,String password,String host,int port,int timeout)
throws IOException{
DefaultConfig defaultConfig = new DefaultConfig();
defaultConfig.setKeyExchangeFactories(Arrays.>asList(
ExtendedDHGroups.Group18SHA512AtSSH(),
ExtendedDHGroups.Group16SHA512AtSSH(),
ExtendedDHGroups.Group16SHA384AtSSH(),
ExtendedDHGroups.Group16SHA256(),
ExtendedDHGroups.Group15SHA384AtSSH(),
ExtendedDHGroups.Group15SHA256AtSSH(),
ExtendedDHGroups.Group15SHA256(),
ExtendedDHGroups.Group14SHA256AtSSH(),
new Curve25519SHA256.Factory(),
new ECDHNistP.Factory521(),
new ECDHNistP.Factory384(),
new ECDHNistP.Factory256(),
new DHGexSHA256.Factory(),
new DHGexSHA1.Factory(),
DHGroups.Group18SHA512(),
DHGroups.Group17SHA512(),
DHGroups.Group16SHA512(),
DHGroups.Group15SHA512(),
DHGroups.Group14SHA256(),
DHGroups.Group14SHA1(),
DHGroups.Group1SHA1()));
//如果客户端和服务器端的压缩算法不一致,将导致connect timed out,连三次握手都没有建立
defaultConfig.setCompressionFactories(Arrays.>asList(
// new ZlibCompression.Factory()//,
// new NoneCompression.Factory(),
new DelayedZlibCompression.Factory()
));
SSHClient sshclt = new SSHClient(defaultConfig);
sshclt.addHostKeyVerifier(
new HostKeyVerifier() {
@Override
public boolean verify(String arg0, int arg1, PublicKey arg2) {
// TODO Auto-generated method stub
return false;
}
@Override
public List findExistingAlgorithms(String arg0, int arg1) {
// TODO Auto-generated method stub
return null;
}
});
//使用上面的代码替代,因为这里有HostKeyVerifier,忽略之
// sshclt.loadKnownHosts();
// sshclt.setTimeout(timeout);
//上面那个超时时间函数不管用
sshclt.setConnectTimeout(timeout);
sshclt.connect(host, port);
try {
sshclt.authPassword(username, password);
Session session = sshclt.startSession();
try {
Command cmd = session.exec("uname -a && date && uptime && w");
System.out.println(IOUtils.readFully(cmd.getInputStream()).toString());
cmd.join(5, TimeUnit.SECONDS);
System.out.println("\n** exit status: " + cmd.getExitStatus());
cmd.close();
} catch (ConnectionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
session.close();
}
} catch (UserAuthException | TransportException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
sshclt.disconnect();
}
}
}