SshClient ssh = new SshClient(); ssh.connect(host, port); //Authenticate PasswordAuthenticationClient passwordAuthenticationClient = new PasswordAuthenticationClient(); passwordAuthenticationClient.setUsername(userName); passwordAuthenticationClient.setPassword(password); int result = ssh.authenticate(passwordAuthenticationClient); if(result != AuthenticationProtocolState.COMPLETE){ throw new SFTPException("Login to " + host + ":" + port + " " + userName + "/" + password + " failed"); } //Open the SFTP channel SftpClient client = ssh.openSftpClient(); //Send the file client.put(filePath); //disconnect client.quit(); ssh.disconnect();
public ExecResponse refreshAndRunAction(String action) { checkState(ssh != null, "please call init() before invoking call"); try { ssh.connect(); return runAction(action); } finally { if (ssh != null) ssh.disconnect(); } }
public ExecResponse refreshAndRunAction(String action) { checkState(ssh != null, "please call init() before invoking call"); try { ssh.connect(); return runAction(action); } finally { if (ssh != null) ssh.disconnect(); } }
@Override public ExecResponse call() { checkState(ssh != null, "please call init() before invoking call"); try { ssh.connect(); ExecResponse returnVal = doCall(); eventBus.post(new StatementOnNodeCompletion(init, node, returnVal)); return returnVal; } finally { if (ssh != null) ssh.disconnect(); } }
@Override public ExecResponse call() { checkState(ssh != null, "please call init() before invoking call"); try { ssh.connect(); ExecResponse returnVal = doCall(); eventBus.post(new StatementOnNodeCompletion(init, node, returnVal)); return returnVal; } finally { if (ssh != null) ssh.disconnect(); } }
public ExecResponse refreshAndRunAction(String action) { checkState(ssh != null, "please call init() before invoking call"); try { if (!ssh.isConnected()) { ssh.connect(); } return runAction(action); } finally { if (ssh != null) ssh.disconnect(); } }
@Test public void testExecHostname() throws IOException { SshClient client = setupClient(); try { ExecResponse response = client.exec("hostname"); assertEquals(response.getExitStatus(), 0); assertEquals(response.getError(), ""); assertEquals(response.getOutput().trim(), "localhost".equals(sshHost) ? InetAddress.getLocalHost().getHostName() : sshHost); } finally { client.disconnect(); } }
private void runScriptAndInstallSsh(SshClient client, String scriptName, int nodeId) { client.connect(); try { runScript(client, scriptName, Strings2.toStringAndClose(StubComputeServiceIntegrationTest.class .getResourceAsStream("/initscript_with_java.sh")), nodeId); } catch (IOException e) { Throwables.propagate(e); } client.disconnect(); }
protected void checkSSH(HostAndPort socket) { socketTester.apply(socket); SshClient client = sshFactory.create(socket, loginCredentials); try { client.connect(); ExecResponse exec = client.exec("echo hello"); System.out.println(exec); assertEquals(exec.getOutput().trim(), "hello"); } finally { if (client != null) client.disconnect(); } }
private void helloAndJava(SshClient client) { client.connect(); expect(client.exec("echo hello")).andReturn(new ExecResponse("hello", "", 0)); expect(client.exec("java -version")).andReturn(new ExecResponse("", "OpenJDK", 0)); client.disconnect(); }
private void helloAndJava(SshClient client) { client.connect(); expect(client.exec("echo hello")).andReturn(new ExecResponse("hello", "", 0)); expect(client.exec("java -version")).andReturn(new ExecResponse("", "OpenJDK", 0)); client.disconnect(); }
@Test public void testExecInvalidCommand() throws IOException { SshClient client = setupClient(); try { ExecResponse response = client.exec("thisCommandDoesNotExist"); assertNotEquals(response.getExitStatus(), 0); assertTrue(response.getOutput().contains("not found") || response.getError().contains("not found"), "stdout="+response.getOutput()+"; stderr="+response.getError()); } finally { client.disconnect(); } }
public void simpleTest() { RunScriptOnNodeUsingSsh testMe = new RunScriptOnNodeUsingSsh(sshFactory, eventBus, node, exec("echo $USER\necho $USER"), wrapInInitScript(false).runAsRoot(false)); testMe.init(); sshClient.connect(); expect(sshClient.getUsername()).andReturn("tester"); expect(sshClient.getHostAddress()).andReturn("somewhere.example.com"); expect(sshClient.exec("echo $USER\n" + "echo $USER\n")).andReturn(new ExecResponse("tester\ntester\n", null, 0)); sshClient.disconnect(); replay(sshClient); testMe.call(); }
protected void refreshSshIfNewAdminCredentialsConfigured(AdminAccess input) { if (input.getAdminCredentials() != null && input.shouldGrantSudoToAdminUser()) { ssh.disconnect(); logger.debug(">> reconnecting as %s@%s", input.getAdminCredentials().identity, ssh.getHostAddress()); ssh = sshFactory.apply(node = NodeMetadataBuilder.fromNodeMetadata(node) .credentials(LoginCredentials.fromCredentials(input.getAdminCredentials())).build()); ssh.connect(); setupLinkToInitFile(); } }
protected void refreshSshIfNewAdminCredentialsConfigured(AdminAccess input) { if (input.getAdminCredentials() != null && input.shouldGrantSudoToAdminUser()) { ssh.disconnect(); logger.debug(">> reconnecting as %s@%s", input.getAdminCredentials().identity, ssh.getHostAddress()); ssh = sshFactory.apply(node = NodeMetadataBuilder.fromNodeMetadata(node) .credentials(LoginCredentials.fromCredentials(input.getAdminCredentials())).build()); ssh.connect(); setupLinkToInitFile(); } }
protected void refreshSshIfNewAdminCredentialsConfigured(AdminAccess input) { if (input.getAdminCredentials() != null && input.shouldGrantSudoToAdminUser()) { ssh.disconnect(); logger.debug(">> reconnecting as %s@%s", input.getAdminCredentials().identity, ssh.getHostAddress()); ssh = sshFactory.apply(node = NodeMetadataBuilder.fromNodeMetadata(node) .credentials(LoginCredentials.fromCredentials(input.getAdminCredentials())).build()); ssh.connect(); setupLinkToInitFile(); } }
private void doCheckKey(String address) { SshClient ssh = sshFactory.create(HostAndPort.fromParts(address, 22), LoginCredentials.builder().user("ubuntu").privateKey(keyPair.getKeyMaterial()).build()); try { ssh.connect(); ExecResponse hello = ssh.exec("echo hello"); assertEquals(hello.getOutput().trim(), "hello"); } finally { if (ssh != null) ssh.disconnect(); } }
private void doCheckKey(String address) { SshClient ssh = sshFactory.create(HostAndPort.fromParts(address, 22), LoginCredentials.builder().user("root").privateKey(keyPair.getKeyMaterial()).build()); try { ssh.connect(); ExecResponse hello = ssh.exec("echo hello"); assertEquals(hello.getOutput().trim(), "hello"); } finally { if (ssh != null) ssh.disconnect(); } }
private void doCheckKey(String address) { SshClient ssh = sshFactory.create(HostAndPort.fromParts(address, 22), LoginCredentials.builder().user("ubuntu").privateKey(keyPair.getKeyMaterial()).build()); try { ssh.connect(); ExecResponse hello = ssh.exec("echo hello"); assertEquals(hello.getOutput().trim(), "hello"); } finally { if (ssh != null) ssh.disconnect(); } }
private void doCheckKey(String address) { SshClient ssh = sshFactory.create(HostAndPort.fromParts(address, 22), LoginCredentials.builder().user("root").privateKey(keyPair.getKeyMaterial()).build()); try { ssh.connect(); ExecResponse hello = ssh.exec("echo hello"); assertEquals(hello.getOutput().trim(), "hello"); } finally { if (ssh != null) ssh.disconnect(); } }