1

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() + " " + 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()

    }

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 '$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

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 :(

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

I updated the above code to the below, using the process builder, but now i'm getting -255 in the logs :( you can quickly see why i didnt want to go this route :(

    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()
            }
        }

    }

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 How do I run multiple commands in SSH through Java? the way i built the process is meant to work right?

imp
  • 435
  • 6
  • 20
  • `pb.redirectErrorStream()` should of been `pb.redirectErrorStream(true)`. I now see the actual error is awk: cmd. line:1: fatal: cannot open file `2020-09-07' for reading (No such file or directory) – imp Sep 07 '20 at 16:23
  • it seems process builder does not like the line `"'\$0 >= \"${startTime.format(DateTimeFormatter.ofPattern(pattern))}\" && \$0 <= \"${endTime.format(DateTimeFormatter.ofPattern(pattern))}\"'",` why? – imp Sep 07 '20 at 16:52
  • this works so far from bash `"awk '\$0 >= \"2020-09-15 07:06:56,236\" && \$0 <= \"2020-09-15 09:32:56,236\"' filename"` – imp Sep 15 '20 at 09:40

1 Answers1

0

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 "${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"

and i call it via this way in 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
)

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

imp
  • 435
  • 6
  • 20