Search in sources :

Example 1 with SimpleSSHClient

use of com.fo0.robot.connector.SimpleSSHClient in project Robot by fo0.

the class ChainActionItem method ssh.

public EChainResponse ssh(List<KeyValue> list) throws Exception {
    List<KeyValue> sshList = list;
    KeyValue sshHost = sshList.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.HOST)).findFirst().orElse(null);
    KeyValue sshPort = sshList.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.PORT)).findFirst().orElse(KeyValue.builder().key("$PORT").value("22").build());
    KeyValue sshUser = sshList.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.USER)).findFirst().orElse(null);
    KeyValue sshPassword = sshList.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.PASSWORD)).findFirst().orElse(KeyValue.builder().key(CONSTANTS_PATTERN.PASSWORD).value("").build());
    KeyValue sshCmd = sshList.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.CMD)).findFirst().orElse(null);
    ctx.addToLog(type, "HOST: " + sshHost.getValue());
    ctx.addToLog(type, "PORT: " + sshPort.getValue());
    ctx.addToLog(type, "User: " + sshUser.getValue());
    ctx.addToLog(type, "Password: " + StringUtils.join(IntStream.range(0, sshPassword.getValue().length()).mapToObj(e -> "*").toArray(String[]::new)));
    ctx.addToLog(type, "CMD: " + sshCmd.getValue());
    SimpleSSHClient sshClient = new SimpleSSHClient(Host.builder().address(sshHost.getValue()).port(Integer.parseInt(sshPort.getValue())).username(sshUser.getValue()).password(sshPassword.getValue()).build());
    try {
        sshClient.connect();
    } catch (Exception e2) {
        ctx.addToLog(type, "failed to connect to Host " + e2);
        return EChainResponse.Failed;
    }
    sshClient.command(sshCmd.getValue(), out -> {
        ctx.addToLogPlain(type, out);
    }, error -> {
        ctx.addToLogPlain(type, error);
    });
    return EChainResponse.Continue;
}
Also used : KeyValue(com.fo0.robot.model.KeyValue) SimpleSSHClient(com.fo0.robot.connector.SimpleSSHClient)

Aggregations

SimpleSSHClient (com.fo0.robot.connector.SimpleSSHClient)1 KeyValue (com.fo0.robot.model.KeyValue)1