/** * Attempts loading the user's {@code known_hosts} file from the default locations, i.e. {@code ~/.ssh/known_hosts} * and {@code ~/.ssh/known_hosts2} on most platforms. Adds the resulting {@link OpenSSHKnownHosts} object as a host * key verifier. * <p/> * For finer control over which file is used, see {@link #loadKnownHosts(File)}. * * @throws IOException if there is an error loading from <em>both</em> locations */ public void loadKnownHosts() throws IOException { boolean loaded = false; final File sshDir = OpenSSHKnownHosts.detectSSHDir(); if (sshDir != null) { for (File loc : Arrays.asList(new File(sshDir, "known_hosts"), new File(sshDir, "known_hosts2"))) { try { loadKnownHosts(loc); loaded = true; } catch (IOException e) { // Ignore for now } } } if (!loaded) throw new IOException("Could not load known_hosts"); }
SSHClient ssh = new SSHClient(); //ssh.useCompression(); ssh.loadKnownHosts(); ssh.connect("localhost"); try { ssh.authPublickey(System.getProperty("user.name")); new SCPDownloadClient(ssh).copy("ten", "/tmp"); } finally { ssh.disconnect(); }
SSHClient ssh = new SSHClient(); ssh.loadKnownHosts(); ssh.connect("nameOfServer"); ssh.authPublickey("userId"); Session session = ssh.startSession(); Command cmd = session.exec("yourCommand"); System.out.println(cmd.getOutputAsString()); session.close(); ssh.disconnect();
/** * Attempts loading the user's {@code known_hosts} file from the default locations, i.e. {@code ~/.ssh/known_hosts} * and {@code ~/.ssh/known_hosts2} on most platforms. Adds the resulting {@link OpenSSHKnownHosts} object as a host * key verifier. * <p/> * For finer control over which file is used, see {@link #loadKnownHosts(File)}. * * @throws IOException if there is an error loading from <em>both</em> locations */ public void loadKnownHosts() throws IOException { boolean loaded = false; final File sshDir = OpenSSHKnownHosts.detectSSHDir(); if (sshDir != null) { for (File loc : Arrays.asList(new File(sshDir, "known_hosts"), new File(sshDir, "known_hosts2"))) { try { loadKnownHosts(loc); loaded = true; } catch (IOException e) { // Ignore for now } } } if (!loaded) throw new IOException("Could not load known_hosts"); }
/** * Attempts loading the user's {@code known_hosts} file from the default locations, i.e. {@code ~/.ssh/known_hosts} * and {@code ~/.ssh/known_hosts2} on most platforms. Adds the resulting {@link OpenSSHKnownHosts} object as a host * key verifier. * <p/> * For finer control over which file is used, see {@link #loadKnownHosts(File)}. * * @throws IOException if there is an error loading from <em>both</em> locations */ public void loadKnownHosts() throws IOException { boolean loaded = false; final File sshDir = OpenSSHKnownHosts.detectSSHDir(); if (sshDir != null) { for (File loc : Arrays.asList(new File(sshDir, "known_hosts"), new File(sshDir, "known_hosts2"))) { try { loadKnownHosts(loc); loaded = true; } catch (IOException e) { // Ignore for now } } } if (!loaded) throw new IOException("Could not load known_hosts"); }
public static void main(String[] args) throws IOException { final SSHClient ssh = new SSHClient(); ssh.loadKnownHosts(); ssh.connect("10.x.x.x"); try { //ssh.authPublickey(System.getProperty("root")); ssh.authPassword("user", "xxxx"); final Session session = ssh.startSession(); try { final Command cmd = session.exec("cd /backup; ls; ./backup.sh"); 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(); } }
SSHClient ssh = new SSHClient(); ssh.loadKnownHosts(); ssh.connect("your-server"); ssh.authPublickey("userId"); //START SESSION Session session = ssh.startSession(); //SEND THE COMMAND Command cmd = session.exec("shutdown"); //GET THE OUTPUT OF COMMAND cmd.getOutputAsString(); //CLOSE SESSION session.close(); ssh.disconnect();
private void configureConnection(String serverFingerPrint, Boolean checkIdentity) throws IOException { createKnownHosts(); ssh = new SSHClient(); ssh.loadKnownHosts(); if (!checkIdentity) { ssh.addHostKeyVerifier(new PromiscuousVerifier()); } else if (serverFingerPrint != null) { ssh.addHostKeyVerifier(serverFingerPrint); } else { File khFile = new File(OpenSSHKnownHosts.detectSSHDir(), "known_hosts"); ssh.addHostKeyVerifier(new OpenSSHKnownHosts(khFile)); } }
import net.schmizz.sshj.SSHClient; import net.schmizz.sshj.xfer.FileSystemFile; import java.io.File; import java.io.IOException; /** This example demonstrates uploading of a file over SCP to the SSH server. */ public class SCPUpload { public static void main(String[] args) throws IOException, ClassNotFoundException { SSHClient ssh = new SSHClient(); ssh.loadKnownHosts(); ssh.connect("localhost"); try { ssh.authPublickey("/path/to/key.pem")); // Present here to demo algorithm renegotiation - could have just put this before connect() // Make sure JZlib is in classpath for this to work ssh.useCompression(); final String src = System.getProperty("user.home") + File.separator + "test_file"; ssh.newSCPFileTransfer().upload(new FileSystemFile(src), "/tmp/"); } finally { ssh.disconnect(); } } }
final SSHClient ssh = new SSHClient(); ssh.loadKnownHosts(); // or, to skip host verification: ssh.addHostKeyVerifier(new PromiscuousVerifier()) ssh.connect("localhost"); try { ssh.authPassword("user", "password"); // or ssh.authPublickey(System.getProperty("user.name")) final SFTPClient sftp = ssh.newSFTPClient(); try { sftp.get("test_file", "/tmp/test.tmp"); } finally { sftp.close(); } } finally { ssh.disconnect(); }
private Session getSession(InvocationContext context) throws IOException { try { /* * if it is connected, create a session Note: one client can have multiple session (on one channel) */ if (ssh.isConnected()) return ssh.startSession(); if (sshContext == null) { sshContext = ((SSHSecurityContextImpl) context.getSecurityContext(SSH_SECURITY_CONTEXT)); } KeyProvider pkey = ssh.loadKeys(sshContext.getPrivateKeyLoc(), sshContext.getKeyPass()); ssh.loadKnownHosts(); ssh.authPublickey(sshContext.getUsername(), pkey); ssh.connect(context.getExecutionDescription().getHost().getType().getHostAddress()); return ssh.startSession(); } catch (NullPointerException ne) { throw new SecurityException("Cannot load security context for SSH", ne); } }
public Session getSession(String hostAddress) throws IOException { try { if (sshClient == null) { sshClient = new SSHClient(); } if (getSSHClient().isConnected()) return getSSHClient().startSession(); KeyProvider pkey = getSSHClient().loadKeys(getPrivateKeyLoc(), getKeyPass()); getSSHClient().loadKnownHosts(); getSSHClient().connect(hostAddress); getSSHClient().authPublickey(getUsername(), pkey); session = getSSHClient().startSession(); return session; } catch (NullPointerException ne) { throw new SecurityException("Cannot load security context for SSH", ne); } }