awk over plink 和多个服务器跃点不响应

awk over plink and multiple server hops doesnt respond

为什么 awk 不响应文本?我知道我在正确的服务器上(将 awk 命令/Object 更改为 ls -la returns 显示正确信息的文本)。而且我知道当 运行 直接在服务器上时 awk 命令有效 - 我可以复制粘贴命令,它会 运行

def tailLogs(Object object){
    def cmd = jumperBox() + " " + object
    Runtime rt = Runtime.getRuntime()
    log.info("Running: "+cmd)
    Process process = rt.exec(cmd.toString())
    StringBuilder cmdReturn = new StringBuilder()
    try{
        InputStream inputStream = process.getInputStream()
        int c
        while ((c = inputStream.read()) != -1) {
            cmdReturn.append((char) c)
        }
        log.info("CMD RESPONSE: "+ cmdReturn)
        process.waitFor()
    }catch(Exception e){
        log.warn(e.getMessage())
    }finally{
        process.destroy()

    }

日志:

2020-08-27 09:21:56.359 INFO  serverInformation  -Running: plink.exe -batch -ssh user@server1 -i C:\privateKey.ppk ssh user2@server2 -i ~/.ssh/id_rsa awk '[=11=] >= "2020-08-27 09:06:56,236" && [=11=] <= "2020-08-27 09:21:56,238"' logfile
2020-08-27 09:21:59.119 INFO  serverInformation  -CMD RESPONSE: 
2020-08-27 09:21:59.147 INFO  logTester  -2

我查看了以下链接,它们只是有点帮助,使用流程构建器意味着重写我不想做的其他功能(我尝试过但失败了,我不知道如何发送多个命令(就像使用 -i 选项的服务器跳跃一样。在看到 ls -la 的输出后,我觉得这个解决方案非常接近:(

No response when running AWK shell script using java

https://superuser.com/questions/950560/escaping-awk-with-remote-ssh-command-and-bash-that-is-already-escaped

Run a remote awk command using ssh

我使用进程构建器将上面的代码更新为下面的代码,但现在我在日志中得到 -255 :( 你很快就会明白为什么我不想走这条路:(

    def getLogs(String serverConnection,String serverKey, String fileName){
        if(osName.contains('windows')){
            ProcessBuilder pb = new ProcessBuilder(ssh(),//this could be plink or straight ssh
                    "-batch",
                    "-ssh",
                    "user@server1",
                    "-i",
                    server1key(),//changes depending on server
                    "ssh",
                    serverConnection,
                    "-i",
                    serverKey,
                    "awk",
                    "'$0 >= \"${startTime.format(DateTimeFormatter.ofPattern(pattern))}\" && $0 <= \"${endTime.format(DateTimeFormatter.ofPattern(pattern))}\"'",
                    fileName
            )
            log.info "running: "+pb.toString() //didnt show me
            pb.redirectErrorStream()
            Process process = pb.start()
            StringBuilder cmdReturn = new StringBuilder()
            try{
                InputStream inputStream = process.getInputStream()
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))
                int c
                while ((c = reader.read()) != -1) {
                    cmdReturn.append((char) c)
                }
                log.info("CMD RESPONSE: "+ cmdReturn)
                process.waitFor()
            }catch(Exception e){
                log.warn(e.getMessage())
            }finally{
                process.destroy()
            }
        }else{
            ProcessBuilder pb = new ProcessBuilder("ssh",
                    "user@server1",
                    "-i",
                    server1key(),
                    "ssh",
                    serverConnection,
                    "-i",
                    serverKey,
                    "awk",
                    "'$0 >= \"${startTime.format(DateTimeFormatter.ofPattern(pattern))}\" && $0 <= \"${endTime.format(DateTimeFormatter.ofPattern(pattern))}\"'",
                    fileName
            )
            pb.redirectErrorStream()
            Process process = pb.start()
            StringBuilder cmdReturn = new StringBuilder()
            try{
                InputStream inputStream = process.getInputStream()
                int c
                while ((c = inputStream.read()) != -1) {
                    cmdReturn.append((char) c)
                }
                log.info("CMD RESPONSE: "+ cmdReturn)
                process.waitFor()
            }catch(Exception e){
                log.warn(e.getMessage())
            }finally{
                process.destroy()
            }
        }

    }

与此的日志没有太大区别:

2020-09-02 16:50:54.520 INFO  serverInformation  -running: java.lang.ProcessBuilder@1c691f9f
2020-09-02 16:50:57.335 INFO  serverInformation  -CMD RESPONSE: 
2020-09-02 16:50:57.346 INFO  logTester  -255

根据 How do I run multiple commands in SSH through Java? 我构建流程的方式应该有效吗?

所以,在 java\groovy 代码中尝试了这么多不同的变体之后,我求助于制作一个 bash 文件:

#!/bin/bash

while getopts ?s:e:c:k:f: flag
do
    case "${flag}" in
        s) startTime=${OPTARG};;
        e) endTime=${OPTARG};;
        c) serverConnection=${OPTARG};;
        k) serverKey=${OPTARG};;
        f) fileName=${OPTARG};;
        \?)echo "This will not work without all options below:
                -s startTime format: 2020-08-27 09:06:56,236
                -e endTime format: 2020-08-27 09:16:56,236
                -c server connection eg: user@server
                -k id_rsa file
                -f logfile eg: APPLICATION/logs/log.file"
        exit;;
    esac
done


ssh $serverConnection -i $serverKey "awk '$0 >= \"$startTime\" && $0 <= \"$endTime\"' $fileName"

我在PB中是这样调用的

pb.command(ssh(),//dbl check the environment we are in
        "-batch",
        "-ssh",
        "user@server",
        "-i",
        jumperKey(),//again to dbl check the environment we are in
        "./awk.sh",
        "-s",
        startTime.format(DateTimeFormatter.ofPattern(pattern)).replaceAll(/ /,'\\ '),
        "-e",
        endTime.format(DateTimeFormatter.ofPattern(pattern)).replaceAll(/ /,'\\ '),
        "-c",
        serverConnection,
        "-k",
        serverKey,
        "-f",
        fileName
)

老实说,我对必须有一个 bash 文件不太满意,但是 tempus fugit