本文整理汇总了Java中net.schmizz.sshj.SSHClient.authPublickey方法的典型用法代码示例。如果您正苦于以下问题:Java SSHClient.authPublickey方法的具体用法?Java SSHClient.authPublickey怎么用?Java SSHClient.authPublickey使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net.schmizz.sshj.SSHClient
的用法示例。
在下文中一共展示了SSHClient.authPublickey方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: uploadFile
import net.schmizz.sshj.SSHClient; //导入方法依赖的package包/类
@Test
public void uploadFile() throws IOException, NoSuchProviderException, NoSuchAlgorithmException {
final SSHClient ssh = new SSHClient();
ssh.addHostKeyVerifier(SecurityUtils.getFingerprint(subject.getHostsPublicKey()));
ssh.connect("localhost", subject.getPort());
try {
ssh.authPublickey("this_is_ignored", new KeyPairWrapper(
KeyPairGenerator.getInstance("DSA", "SUN").generateKeyPair()));
final SFTPClient sftp = ssh.newSFTPClient();
File testFile = local.newFile("fred.txt");
try {
sftp.put(new FileSystemFile(testFile), "/fred.txt");
} finally {
sftp.close();
}
} finally {
ssh.disconnect();
}
assertThat(sftpHome.getRoot().list(), is(new String[]{"fred.txt"}));
}
示例2: executeCommand
import net.schmizz.sshj.SSHClient; //导入方法依赖的package包/类
@Override
public String executeCommand(ShellCommand aCommand) throws IOException {
SSHClient ssh = new SSHClient();
ssh.loadKnownHosts();
ssh.connect("localhost", 2222);
try {
ssh.authPublickey("root");
try (Session session = ssh.startSession()) {
Session.Command command = session.exec(aCommand.getCommand());
String text = IOUtils.readFully(command.getInputStream()).toString();
command.join(5, TimeUnit.SECONDS);
return text;
}
} finally {
ssh.disconnect();
}
}
示例3: build
import net.schmizz.sshj.SSHClient; //导入方法依赖的package包/类
private void build() throws IOException {
if (init) {
return;
}
ssh = new SSHClient();
ssh.addHostKeyVerifier(new HostKeyVerifier() {
@Override
public boolean verify(String arg0, int arg1, PublicKey arg2) {
return true;
}
});
ssh.connect(hostname, port);
if (privateKey != null) {
privateKeyFile = File.createTempFile("zstack", "tmp");
FileUtils.writeStringToFile(privateKeyFile, privateKey);
ssh.authPublickey(username, privateKeyFile.getAbsolutePath());
} else {
ssh.authPassword(username, password);
}
init = true;
}
示例4: connect_to_virtual_box
import net.schmizz.sshj.SSHClient; //导入方法依赖的package包/类
@Test
public void connect_to_virtual_box() throws IOException {
SSHClient ssh = new SSHClient();
ssh.loadKnownHosts();
ssh.connect("localhost", 2222);
try {
ssh.authPublickey("root");
try (Session session = ssh.startSession()) {
Session.Command command = session.exec("ls -l");
System.out.println(IOUtils.readFully(command.getInputStream()).toString());
command.join(5, TimeUnit.SECONDS);
}
} finally {
ssh.disconnect();
}
}
示例5: SshConnection
import net.schmizz.sshj.SSHClient; //导入方法依赖的package包/类
/**
* Connects to a ssh server
*
* <p>
* Do not call this constructor directly, use {@link SshConnector} instead for IoC
*
* @param config the ssh configuration
* @param client an instance of {@link SSHClient} to use
* @throws IOException failed to connect to the server
*/
public SshConnection(SshConfig config, SSHClient client) throws IOException {
// TODO see if the SSHClient can be injected differently
this.client = client;
this.config = config;
if (!config.getVerifyHostKey()) {
client.addHostKeyVerifier(new PromiscuousVerifier());
}
client.connect(config.getHost(), config.getPort());
if (config.getPassword() != null) {
client.authPassword(config.getUser(), config.getPassword());
} else if (config.getKeyFile() != null) {
client.authPublickey(config.getUser(), config.getKeyFile().toString());
} else {
client.authPassword(config.getUser(), "");
}
}
示例6: connect
import net.schmizz.sshj.SSHClient; //导入方法依赖的package包/类
@Override
public void connect(String host) throws IOException {
sshClient = new SSHClient();
if (knownHosts == null) {
sshClient.loadKnownHosts();
} else {
sshClient.loadKnownHosts(knownHosts.toFile());
}
sshClient.connect(host);
if (privateKey != null) {
sshClient.authPublickey(user, privateKey.toString());
} else if (password != null) {
sshClient.authPassword(user, password);
} else {
throw new RuntimeException("Either privateKey nor password is set. Please call one of the auth method.");
}
}
示例7: downloadFile
import net.schmizz.sshj.SSHClient; //导入方法依赖的package包/类
@Test
public void downloadFile() throws IOException, NoSuchProviderException, NoSuchAlgorithmException {
FileUtils.write(sftpHome.newFile("fred.txt"), "Electric boogaloo");
final SSHClient ssh = new SSHClient();
ssh.addHostKeyVerifier(SecurityUtils.getFingerprint(subject.getHostsPublicKey()));
ssh.connect("localhost", subject.getPort());
try {
ssh.authPublickey("this_is_ignored", new KeyPairWrapper(
KeyPairGenerator.getInstance("DSA", "SUN").generateKeyPair()));
final SFTPClient sftp = ssh.newSFTPClient();
try {
sftp.get("fred.txt", new FileSystemFile(new File(local.getRoot(), "fred.txt")));
} finally {
sftp.close();
}
} finally {
ssh.disconnect();
}
assertThat(FileUtils.readFileToString(new File(local.getRoot(), "fred.txt")),
is("Electric boogaloo"));
}
示例8: prepareSshConnection
import net.schmizz.sshj.SSHClient; //导入方法依赖的package包/类
private void prepareSshConnection(SSHClient ssh, String ip, int port, HostKeyVerifier hostKeyVerifier, String user,
String privateKeyString, Stack stack) throws CloudbreakException, IOException {
ssh.addHostKeyVerifier(hostKeyVerifier);
ssh.connect(ip, port);
if (stack.getStackAuthentication().passwordAuthenticationRequired()) {
ssh.authPassword(user, stack.getStackAuthentication().getLoginPassword());
} else {
try {
KeyPair keyPair = KeyStoreUtil.createKeyPair(privateKeyString);
KeyPairWrapper keyPairWrapper = new KeyPairWrapper(keyPair);
ssh.authPublickey(user, keyPairWrapper);
} catch (Exception e) {
throw new CloudbreakException("Couldn't prepare SSH connection", e);
}
}
}
示例9: newClient
import net.schmizz.sshj.SSHClient; //导入方法依赖的package包/类
/**
* Create a new {@code SSHClient} connected to the remote machine using the
* AdminAccess credentials as provided
*/
public static SSHClient newClient(
Machine machine, AdminAccess adminAccess, int timeoutInMillis
) throws IOException {
checkArgument(timeoutInMillis >= 0, "timeoutInMillis should be positive or 0");
final SSHClient client = new SSHClient();
client.addHostKeyVerifier(AcceptAnyHostKeyVerifier.INSTANCE);
if (timeoutInMillis != 0) {
client.setConnectTimeout(DEFAULT_CONNECT_TIMEOUT);
client.setTimeout(timeoutInMillis);
}
client.connect(machine.getPublicDnsName(), machine.getSshPort());
OpenSSHKeyFile key = new OpenSSHKeyFile();
key.init(adminAccess.getPrivateKey(), adminAccess.getPublicKey());
client.authPublickey(adminAccess.getUsername(), key);
return client;
}
示例10: configureSsh
import net.schmizz.sshj.SSHClient; //导入方法依赖的package包/类
private SSHClient configureSsh() throws IOException {
SSHClient sshClient = new SSHClient();
sshClient.loadKnownHosts();
sshClient.connect(eyeballsConfiguration.getSftpDestinationHost(), eyeballsConfiguration.getSftpRemotePort());
sshClient.authPublickey(eyeballsConfiguration.getSftpUsername());
sshClient.useCompression();
return sshClient;
}
示例11: getConnectedClient
import net.schmizz.sshj.SSHClient; //导入方法依赖的package包/类
private SSHClient getConnectedClient() throws Exception {
if (password == null && key == null)
throw new Exception("You need to provide one among the key and the password to be used.");
DefaultConfig defaultConfig = new DefaultConfig();
defaultConfig.setKeepAliveProvider(KeepAliveProvider.KEEP_ALIVE);
final SSHClient ssh = new SSHClient(defaultConfig);
ssh.addHostKeyVerifier(new PromiscuousVerifier());
ssh.connect(ip, SSH_PORT);
if (key != null) {
if (!key.endsWith(".pem"))
key = key.concat(".pem");
Path p = Configuration.getPathToFile(key);
if (p != null)
ssh.authPublickey(user, p.toString());
} else {
ssh.authPassword(user, password);
}
ssh.getConnection().getKeepAlive().setKeepAliveInterval(5);
return ssh;
}
示例12: getSshClient
import net.schmizz.sshj.SSHClient; //导入方法依赖的package包/类
private static SSHClient getSshClient(HostInfo hostInfo) throws IOException {
long sleepTimeMs = INITIAL_SLEEP_TIME_MS;
for (int attempt = 1; attempt <= MAX_ATTEMPTS; attempt++) {
try {
SSHClient ssh = new SSHClient(DEFAULT_CONFIG);
ssh.addHostKeyVerifier(new PromiscuousVerifier());
ssh.connect(hostInfo.host);
ssh.authPublickey(hostInfo.user, hostInfo.privateKeyPath);
ssh.useCompression();
return ssh;
} catch (IOException exception) {
System.out.format("Failed to login to host %s as user %s. Exception: %s.%n", hostInfo.host, hostInfo.user, exception.getMessage());
System.out.format("Attempt %d of %d. Sleeping for %d ms.%n", attempt, MAX_ATTEMPTS, sleepTimeMs);
boolean lastAttempt = attempt == MAX_ATTEMPTS;
if (lastAttempt) {
throw exception;
}
try {
Thread.sleep(sleepTimeMs);
} catch (InterruptedException e) {
}
sleepTimeMs *= BACKOFF_FACTOR;
}
}
throw new RuntimeException(String.format("Unable to login to host %s as user %s after %d attempts.", hostInfo.host, hostInfo.user, MAX_ATTEMPTS));
}
示例13: main
import net.schmizz.sshj.SSHClient; //导入方法依赖的package包/类
public static void main(String... args)
throws IOException {
String privateKey = "-----BEGIN RSA PRIVATE KEY-----\n"
+ "MIIEogIBAAKCAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzI\n"
+ "w+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoP\n"
+ "kcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2\n"
+ "hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NO\n"
+ "Td0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcW\n"
+ "yLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQIBIwKCAQEA4iqWPJXtzZA68mKd\n"
+ "ELs4jJsdyky+ewdZeNds5tjcnHU5zUYE25K+ffJED9qUWICcLZDc81TGWjHyAqD1\n"
+ "Bw7XpgUwFgeUJwUlzQurAv+/ySnxiwuaGJfhFM1CaQHzfXphgVml+fZUvnJUTvzf\n"
+ "TK2Lg6EdbUE9TarUlBf/xPfuEhMSlIE5keb/Zz3/LUlRg8yDqz5w+QWVJ4utnKnK\n"
+ "iqwZN0mwpwU7YSyJhlT4YV1F3n4YjLswM5wJs2oqm0jssQu/BT0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n"
+ "-----END RSA PRIVATE KEY-----";
String publicKey = "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzIw+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoPkcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NOTd0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcWyLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQ== vagrant insecure public key";
SSHClient client = new SSHClient();
client.addHostKeyVerifier(new PromiscuousVerifier());
KeyProvider keys = client.loadKeys(privateKey, publicKey, null);
client.connect("192.168.33.10", 22);
client.authPublickey("vagrant", keys);
try {
final Session session = client.startSession();
try {
session.allocateDefaultPTY();
final Shell shell = session.startShell();
new StreamCopier(shell.getInputStream(), System.out)
.bufSize(shell.getLocalMaxPacketSize())
.spawn("stdout");
new StreamCopier(shell.getErrorStream(), System.err)
.bufSize(shell.getLocalMaxPacketSize())
.spawn("stderr");
// Now make System.in act as stdin. To exit, hit Ctrl+D (since that results in an EOF on System.in)
// This is kinda messy because java only allows console input after you hit return
// But this is just an example... a GUI app could implement a proper PTY
new StreamCopier(System.in, shell.getOutputStream())
.bufSize(shell.getRemoteMaxPacketSize())
.copy();
} finally {
session.close();
}
} finally {
client.disconnect();
}
}
示例14: listFiles
import net.schmizz.sshj.SSHClient; //导入方法依赖的package包/类
@Test
public void listFiles() throws IOException, NoSuchProviderException, NoSuchAlgorithmException {
sftpHome.newFile("fred.txt");
List<RemoteResourceInfo> files;
final SSHClient ssh = new SSHClient();
ssh.addHostKeyVerifier(SecurityUtils.getFingerprint(subject.getHostsPublicKey()));
ssh.connect("localhost", subject.getPort());
try {
ssh.authPublickey("this_is_ignored", new KeyPairWrapper(
KeyPairGenerator.getInstance("DSA", "SUN").generateKeyPair()));
final SFTPClient sftp = ssh.newSFTPClient();
try {
files = sftp.ls("/");
} finally {
sftp.close();
}
} finally {
ssh.disconnect();
}
assertThat(files.size(), is(1));
assertThat(files.get(0).getName(), is("fred.txt"));
}
示例15: connectWithSSH
import net.schmizz.sshj.SSHClient; //导入方法依赖的package包/类
private SSHClient connectWithSSH() throws IOException {
SSHClient ssh = mSSHClientFactory.newSSHClient();
ssh.loadKnownHosts();
ssh.connect(mRemoteInetAddress);
ssh.authPublickey(mUsername, mPrivateKeyPath.toString());
return ssh;
}