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
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?