英文:
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() + " " + 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
https://stackoverflow.com/questions/52254703/no-response-when-running-awk-shell-script-using-java
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 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 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 "${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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论