Search in sources :

Example 1 with SCPClient

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

the class ChainActionItem method scp.

public EChainResponse scp(List<KeyValue> list) throws Exception {
    List<KeyValue> scpList = list;
    KeyValue scpHost = scpList.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.HOST)).findFirst().orElse(null);
    KeyValue scpPort = scpList.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.PORT)).findFirst().orElse(KeyValue.builder().key(CONSTANTS_PATTERN.PORT).value("22").build());
    KeyValue scpUser = scpList.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.USER)).findFirst().orElse(null);
    KeyValue scpPassword = scpList.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.PASSWORD)).findFirst().orElse(KeyValue.builder().key(CONSTANTS_PATTERN.PASSWORD).value("").build());
    KeyValue scpSrc = scpList.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.SOURCE)).findFirst().orElse(null);
    KeyValue scpDst = scpList.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.DESTINATION)).findFirst().orElse(null);
    ctx.addToLog(type, "HOST: " + scpHost.getValue());
    ctx.addToLog(type, "PORT: " + scpPort.getValue());
    ctx.addToLog(type, "User: " + scpUser.getValue());
    ctx.addToLog(type, "Password: " + StringUtils.join(IntStream.range(0, scpPassword.getValue().length()).mapToObj(e -> "*").toArray(String[]::new)));
    ctx.addToLog(type, "SRC: " + scpSrc.getValue());
    ctx.addToLog(type, "DST: " + scpDst.getValue());
    SCPClient scpClient = new SCPClient(Host.builder().address(scpHost.getValue()).port(Integer.parseInt(scpPort.getValue())).username(scpUser.getValue()).password(scpPassword.getValue()).build());
    try {
        scpClient.connect();
    } catch (Exception e2) {
        ctx.addToLog(type, "failed to connect to Host " + e2);
        return EChainResponse.Failed;
    }
    FileTransferData data = null;
    // establish transfer
    try {
        if (type == EActionType.SCP_Download) {
            data = scpClient.download(scpDst.getValue(), scpSrc.getValue());
        } else {
            data = scpClient.upload(scpDst.getValue(), scpSrc.getValue());
        }
    } catch (Exception e2) {
        ctx.addToLog(type, "failed to transfer data " + e2);
        data = FileTransferData.builder().build();
    }
    ctx.addToLog(type, "Transfer: " + data.info());
    return EChainResponse.Continue;
}
Also used : SCPClient(com.fo0.robot.connector.SCPClient) KeyValue(com.fo0.robot.model.KeyValue) FileTransferData(com.fo0.robot.model.FileTransferData)

Aggregations

SCPClient (com.fo0.robot.connector.SCPClient)1 FileTransferData (com.fo0.robot.model.FileTransferData)1 KeyValue (com.fo0.robot.model.KeyValue)1