本文整理汇总了Java中net.schmizz.sshj.SSHClient.connect方法的典型用法代码示例。如果您正苦于以下问题:Java SSHClient.connect方法的具体用法?Java SSHClient.connect怎么用?Java SSHClient.connect使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net.schmizz.sshj.SSHClient
的用法示例。
在下文中一共展示了SSHClient.connect方法的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: connectfromPItoServer
import net.schmizz.sshj.SSHClient; //导入方法依赖的package包/类
public static void connectfromPItoServer(int fotosAnzahl, int belichtungsDauer)
throws IOException {
final String ipServer = Inet4Address.getLocalHost().getHostAddress();
// System.out.println(Inet4Address.getAllByName("raspberrypi"));
// System.out.println(fotosAnzahl);
final SSHClient ssh = new SSHClient();
ssh.addHostKeyVerifier(new NullHostKeyVerifier());
// for(int i = 0; i < InetAddress.)
// ssh.connect("192.168.2.100", 22);
ssh.connect(Settings.raspberryIP, 22);
try {
ssh.authPassword("pi", "raspberry");
final Session session = ssh.startSession();
try {
System.out.println("BEGINNE MIt tray");
final Command cmd = session.exec("java -jar Desktop/Driver/Java/client.jar " + fotosAnzahl + " " + ipServer + " " + belichtungsDauer);
System.out.println(IOUtils.readFully(cmd.getInputStream()).toString());
cmd.join(5, TimeUnit.SECONDS);
System.out.println("\n** exit status: " + cmd.getExitStatus());
} finally {
session.close();
}
} finally {
ssh.disconnect();
ssh.close();
}
}
示例4: 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;
}
示例5: 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();
}
}
示例6: 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(), "");
}
}
示例7: 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.");
}
}
示例8: 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"));
}
示例9: upload
import net.schmizz.sshj.SSHClient; //导入方法依赖的package包/类
/**
* Uploads a local file (or directory) to the remote server via SCP.
*
* @param address the hostname or IP address of the remote server.
* @param username the username of the remote server.
* @param password the password of the remote server.
* @param localPath a local file or directory which will be uploaded.
* @param remotePath the destination path on the remote server.
* @throws Exception
*/
public void upload(String address, String username, String password, String localPath, String remotePath) throws Exception {
SSHClient client = newSSHClient();
client.connect(address);
logger.debug("Connected to hostname %s", address);
try {
client.authPassword(username, password.toCharArray());
logger.debug("Successful authentication of user %s", username);
client.newSCPFileTransfer().upload(new FileSystemFile(new File(localPath)), remotePath);
logger.debug("Successful upload from %s to %s", localPath, remotePath);
} finally {
client.disconnect();
logger.debug("Disconnected to hostname %s", address);
}
}
示例10: download
import net.schmizz.sshj.SSHClient; //导入方法依赖的package包/类
/**
* Downloads a remote file (or directory) to the local directory via SCP.
*
* @param address the hostname or IP address of the remote server.
* @param username the username of the remote server.
* @param password the password of the remote server.
* @param remotePath a remote file or directory which will be downloaded.
* @param localPath the destination directory on the local file system.
* @throws Exception
*/
public void download(String address, String username, String password, String remotePath, String localPath) throws Exception {
SSHClient client = newSSHClient();
client.connect(address);
logger.debug("Connected to hostname %s", address);
try {
client.authPassword(username, password.toCharArray());
logger.debug("Successful authentication of user %s", username);
client.newSCPFileTransfer().download(remotePath, new FileSystemFile(new File(localPath)));
logger.debug("Successful download from %s to %s", remotePath, localPath);
} finally {
client.disconnect();
logger.debug("Disconnected to hostname %s", address);
}
}
示例11: doInBackground
import net.schmizz.sshj.SSHClient; //导入方法依赖的package包/类
@Override
protected Boolean doInBackground(String... cmd) {
String ssh_command = cmd[0];
SSHClient ssh = new SSHClient();
HostKeyVerifier always_verify = new HostKeyVerifier() {
public boolean verify(String arg0, int arg1, PublicKey arg2) {
return true;
}
};
ssh.addHostKeyVerifier(always_verify);
Boolean success = false;
try {
ssh.connect(this.ssh_server);
ssh.authPassword(this.ssh_user, this.ssh_password);
Session session = ssh.startSession();
Command command = session.exec(ssh_command);
command.join();
Integer exit_status = command.getExitStatus();
if (exit_status == this.expected_exit_status) {
success = true;
}
session.close();
ssh.close();
} catch (IOException e) { }
return success;
}
示例12: 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);
}
}
}
示例13: getTempLocalInstance
import net.schmizz.sshj.SSHClient; //导入方法依赖的package包/类
public File getTempLocalInstance( String remoteFilePath )
{
File tempFile = null;
SSHClient client = new SSHClient();
// required if host is not in knownLocalHosts
client.addHostKeyVerifier(new PromiscuousVerifier());
try {
client.connect(hostName);
client.authPassword(userName, userPass);
tempFile = File.createTempFile("tmp", null, null);
tempFile.deleteOnExit();
SFTPClient sftp = client.newSFTPClient();
sftp.get(remoteFilePath, new FileSystemFile(tempFile));
sftp.close();
client.disconnect();
} catch( Exception e ) {
//TODO: this needs to be more robust than just catching all exceptions
e.printStackTrace();
logger.error( e.toString() );
return null;
}
return tempFile;
}
示例14: 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;
}
示例15: connect
import net.schmizz.sshj.SSHClient; //导入方法依赖的package包/类
protected SSHClient connect(final HostKeyCallback key, final Config configuration) throws IOException {
final SSHClient connection = new SSHClient(configuration);
final int timeout = preferences.getInteger("connection.timeout.seconds") * 1000;
connection.setTimeout(timeout);
connection.setSocketFactory(socketFactory);
connection.addHostKeyVerifier(new HostKeyVerifier() {
@Override
public boolean verify(String hostname, int port, PublicKey publicKey) {
try {
return key.verify(hostname, port, publicKey);
}
catch(ConnectionCanceledException | ChecksumException e) {
return false;
}
}
});
connection.addAlgorithmsVerifier(new AlgorithmsVerifier() {
@Override
public boolean verify(final NegotiatedAlgorithms negotiatedAlgorithms) {
log.info(String.format("Negotiated algorithms %s", negotiatedAlgorithms));
algorithms = negotiatedAlgorithms;
return true;
}
});
disconnectListener = new StateDisconnectListener();
final Transport transport = connection.getTransport();
transport.setDisconnectListener(disconnectListener);
connection.connect(HostnameConfiguratorFactory.get(host.getProtocol()).getHostname(host.getHostname()), host.getPort());
final KeepAlive keepalive = connection.getConnection().getKeepAlive();
keepalive.setKeepAliveInterval(preferences.getInteger("ssh.heartbeat.seconds"));
return connection;
}