awk 在通过 plink 进行多次服务器跳跃时无响应。

huangapple go评论72阅读模式
英文:

awk over plink and multiple server hops doesnt respond

问题

以下是您要翻译的内容:

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 '$0 >= "2020-08-27 09:06:56,236" && $0 <= "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

我已根据您的要求提供了翻译,只包括翻译后的部分,没有任何其他内容。

英文:

Why will awk not respond with text? I know i'm on the right server (changing the awk command/Object to ls -la returns text showing the right info). And i know the awk command works when run directly on the server - i can copy paste the command and it'll run

def tailLogs(Object object){
    def cmd = jumperBox() + &quot; &quot; + object
    Runtime rt = Runtime.getRuntime()
    log.info(&quot;Running: &quot;+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(&quot;CMD RESPONSE: &quot;+ cmdReturn)
        process.waitFor()
    }catch(Exception e){
        log.warn(e.getMessage())
    }finally{
        process.destroy()

    }

the log:

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 &#39;$0 &gt;= &quot;2020-08-27 09:06:56,236&quot; &amp;&amp; $0 &lt;= &quot;2020-08-27 09:21:56,238&quot;&#39; logfile
2020-08-27 09:21:59.119 INFO  serverInformation  -CMD RESPONSE: 
2020-08-27 09:21:59.147 INFO  logTester  -2

I have looked at the following links, they only kinda helped and using the process builder means a rewrite of my other functions i dont want to do (i attempted it and failed, i didnt know how to send multiple commands (like the server hops with the -i option). And after seeing the output of ls -la I feel so close with this solution awk 在通过 plink 进行多次服务器跳跃时无响应。

https://stackoverflow.com/questions/52254703/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

https://stackoverflow.com/questions/20011371/run-a-remote-awk-command-using-ssh

I updated the above code to the below, using the process builder, but now i'm getting -255 in the logs awk 在通过 plink 进行多次服务器跳跃时无响应。 you can quickly see why i didnt want to go this route awk 在通过 plink 进行多次服务器跳跃时无响应。

    def getLogs(String serverConnection,String serverKey, String fileName){
        if(osName.contains(&#39;windows&#39;)){
            ProcessBuilder pb = new ProcessBuilder(ssh(),//this could be plink or straight ssh
                    &quot;-batch&quot;,
                    &quot;-ssh&quot;,
                    &quot;user@server1&quot;,
                    &quot;-i&quot;,
                    server1key(),//changes depending on server
                    &quot;ssh&quot;,
                    serverConnection,
                    &quot;-i&quot;,
                    serverKey,
                    &quot;awk&quot;,
                    &quot;&#39;\$0 &gt;= \&quot;${startTime.format(DateTimeFormatter.ofPattern(pattern))}\&quot; &amp;&amp; \$0 &lt;= \&quot;${endTime.format(DateTimeFormatter.ofPattern(pattern))}\&quot;&#39;&quot;,
                    fileName
            )
            log.info &quot;running: &quot;+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(&quot;CMD RESPONSE: &quot;+ cmdReturn)
                process.waitFor()
            }catch(Exception e){
                log.warn(e.getMessage())
            }finally{
                process.destroy()
            }
        }else{
            ProcessBuilder pb = new ProcessBuilder(&quot;ssh&quot;,
                    &quot;user@server1&quot;,
                    &quot;-i&quot;,
                    server1key(),
                    &quot;ssh&quot;,
                    serverConnection,
                    &quot;-i&quot;,
                    serverKey,
                    &quot;awk&quot;,
                    &quot;&#39;\$0 &gt;= \&quot;${startTime.format(DateTimeFormatter.ofPattern(pattern))}\&quot; &amp;&amp; \$0 &lt;= \&quot;${endTime.format(DateTimeFormatter.ofPattern(pattern))}\&quot;&#39;&quot;,
                    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(&quot;CMD RESPONSE: &quot;+ cmdReturn)
                process.waitFor()
            }catch(Exception e){
                log.warn(e.getMessage())
            }finally{
                process.destroy()
            }
        }

    }

Logs from this are not much different:

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

According to https://stackoverflow.com/questions/7611183/how-do-i-run-multiple-commands-in-ssh-through-java the way i built the process is meant to work right?

答案1

得分: 0

所以,在尝试了许多不同的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 "在缺少以下任一选项的情况下无法正常工作:
-s startTime 格式:2020-08-27 09:06:56,236
-e endTime 格式:2020-08-27 09:16:56,236
-c server连接 例如:user@server
-k id_rsa 文件
-f 日志文件 例如:APPLICATION/logs/log.file"
        exit;;
    esac
done

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

然后我通过以下方式在PB中调用它:

pb.command(ssh(),
        "-batch",
        "-ssh",
        "user@server",
        "-i",
        jumperKey(),
        "./awk.sh",
        "-s",
        startTime.format(DateTimeFormatter.ofPattern(pattern)).replaceAll(/ /,'\\\\ '),
        "-e",
        endTime.format(DateTimeFormatter.ofPattern(pattern)).replaceAll(/ /,'\\\\ '),
        "-c",
        serverConnection,
        "-k",
        serverKey,
        "-f",
        fileName
)

对于必须使用Bash文件,我并不太满意,但时光荏苒。

英文:

So,after attempting so many different variation in java\groovy code i resorted to making a bash file:

#!/bin/bash

while getopts ?s:e:c:k:f: flag
do
    case &quot;${flag}&quot; in
        s) startTime=${OPTARG};;
        e) endTime=${OPTARG};;
        c) serverConnection=${OPTARG};;
        k) serverKey=${OPTARG};;
        f) fileName=${OPTARG};;
        \?)echo &quot;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&quot;
        exit;;
    esac
done


ssh $serverConnection -i $serverKey &quot;awk &#39;\$0 &gt;= \&quot;$startTime\&quot; &amp;&amp; \$0 &lt;= \&quot;$endTime\&quot;&#39; $fileName&quot;

and i call it via this way in PB

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

Honestly not too happy with having to have a bash file but tempus fugit

huangapple
  • 本文由 发表于 2020年8月27日 15:57:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63611586.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定