protected SshjSshClient createClient() throws UnknownHostException { Injector i = Guice.createInjector(module()); SshClient.Factory factory = i.getInstance(SshClient.Factory.class); SshjSshClient ssh = SshjSshClient.class.cast( factory.create(new IPSocket("localhost", 22), new Credentials("username", "password"))); return ssh; }
protected SshjSshClient createClient(final Properties props) { Injector i = Guice.createInjector( module(), new AbstractModule() { @Override protected void configure() { bindProperties(binder(), props); } }, new SLF4JLoggingModule()); SshClient.Factory factory = i.getInstance(SshClient.Factory.class); SshjSshClient ssh = SshjSshClient.class.cast( factory.create( HostAndPort.fromParts("localhost", 22), LoginCredentials.builder().user("username").password("password").build())); return ssh; }
private void doCheckKey(String address) { SshClient ssh = sshFactory.create( new IPSocket(address, 22), new Credentials("ubuntu", keyPair.getKeyMaterial())); try { ssh.connect(); ExecResponse hello = ssh.exec("echo hello"); assertEquals(hello.getOutput().trim(), "hello"); } finally { if (ssh != null) ssh.disconnect(); } }
private void doCreateMarkerFile(Slice newDetails, String pass) throws IOException { String ip = getIp(newDetails); IPSocket socket = new IPSocket(ip, 22); socketTester.apply(socket); SshClient client = sshFactory.create(socket, "root", pass); try { client.connect(); client.put("/etc/jclouds.txt", Payloads.newStringPayload("slicehost")); } finally { if (client != null) client.disconnect(); } }
protected void checkSSH(IPSocket socket) { socketTester.apply(socket); SshClient client = sshFactory.create(socket, new Credentials(osUsername, osPassword)); try { client.connect(); ExecResponse exec = client.exec("touch /tmp/hello_" + System.currentTimeMillis()); exec = client.exec("echo hello"); System.out.println(exec); assertEquals(exec.getOutput().trim(), "hello"); } finally { if (client != null) client.disconnect(); } }
protected void doCheckJavaIsInstalledViaSsh(NodeMetadata node) throws IOException { IPSocket socket = new IPSocket(Iterables.get(node.getPublicAddresses(), 0), 22); socketTester.apply(socket); // TODO add transitionTo option that accepts // a socket conection // state. SshClient ssh = sshFactory.create(socket, node.getCredentials().account, keyPair.get("private").getBytes()); try { ssh.connect(); ExecResponse hello = ssh.exec("echo hello"); assertEquals(hello.getOutput().trim(), "hello"); ExecResponse exec = ssh.exec("java -version"); assert exec.getError().indexOf("OpenJDK") != -1 : exec; } finally { if (ssh != null) ssh.disconnect(); } }
@Test(enabled = false, dependsOnMethods = "testCreateAndAttachVolume") void testBundleInstance() { SshClient ssh = sshFactory.create( new IPSocket(instance.getIpAddress(), 22), new Credentials("ubuntu", keyPair.getKeyMaterial())); try { ssh.connect(); } catch (SshException e) { // try twice in case there is a network timeout try { Thread.sleep(10 * 1000); } catch (InterruptedException e1) { } ssh.connect(); } try { System.out.printf( "%d: %s writing ebs script%n", System.currentTimeMillis(), instance.getId()); String script = "/tmp/mkebsboot-init.sh"; ssh.put(script, Payloads.newStringPayload(mkEbsBoot)); System.out.printf( "%d: %s launching ebs script%n", System.currentTimeMillis(), instance.getId()); ssh.exec("chmod 755 " + script); ssh.exec(script + " init"); ExecResponse output = ssh.exec("sudo " + script + " start"); System.out.println(output); output = ssh.exec(script + " status"); assert !output.getOutput().trim().equals("") : output; RetryablePredicate<String> scriptTester = new RetryablePredicate<String>( new ScriptTester(ssh, SCRIPT_END), 600, 10, TimeUnit.SECONDS); scriptTester.apply(script); } finally { if (ssh != null) ssh.disconnect(); } }
@BeforeGroups(groups = {"live"}) public SshClient setupClient() throws NumberFormatException, FileNotFoundException, IOException { int port = Integer.parseInt(sshPort); if (sshUser == null || ((sshPass == null || sshPass.trim().equals("")) && (sshKeyFile == null || sshKeyFile.trim().equals(""))) || sshUser.trim().equals("")) { System.err.println("ssh credentials not present. Tests will be lame"); return new SshClient() { public void connect() {} public void disconnect() {} public Payload get(String path) { if (path.equals("/etc/passwd")) { return Payloads.newStringPayload("root"); } else if (path.equals(temp.getAbsolutePath())) { return Payloads.newStringPayload("rabbit"); } throw new RuntimeException("path " + path + " not stubbed"); } public ExecResponse exec(String command) { if (command.equals("hostname")) { return new ExecResponse(sshHost, "", 0); } throw new RuntimeException("command " + command + " not stubbed"); } @Override public void put(String path, Payload contents) {} @Override public String getHostAddress() { return null; } @Override public String getUsername() { return null; } @Override public void put(String path, String contents) {} }; } else { Injector i = Guice.createInjector(new JschSshClientModule(), new SLF4JLoggingModule()); SshClient.Factory factory = i.getInstance(SshClient.Factory.class); SshClient connection; if (Strings.emptyToNull(sshKeyFile) != null) { connection = factory.create( new IPSocket(sshHost, port), LoginCredentials.builder() .user(sshUser) .privateKey(Strings2.toStringAndClose(new FileInputStream(sshKeyFile))) .build()); } else { connection = factory.create( new IPSocket(sshHost, port), LoginCredentials.builder().user(sshUser).password(sshPass).build()); } connection.connect(); return connection; } }