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

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

awk over plink and multiple server hops doesnt respond

问题

以下是您要翻译的内容:

  1. def tailLogs(Object object){
  2. def cmd = jumperBox() + " " + object
  3. Runtime rt = Runtime.getRuntime()
  4. log.info("Running: "+cmd)
  5. Process process = rt.exec(cmd.toString())
  6. StringBuilder cmdReturn = new StringBuilder()
  7. try{
  8. InputStream inputStream = process.getInputStream()
  9. int c
  10. while ((c = inputStream.read()) != -1) {
  11. cmdReturn.append((char) c)
  12. }
  13. log.info("CMD RESPONSE: "+ cmdReturn)
  14. process.waitFor()
  15. }catch(Exception e){
  16. log.warn(e.getMessage())
  17. }finally{
  18. process.destroy()
  19. }
  20. }

日志:

  1. 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
  2. 2020-08-27 09:21:59.119 INFO serverInformation -CMD RESPONSE:
  3. 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

  1. def tailLogs(Object object){
  2. def cmd = jumperBox() + &quot; &quot; + object
  3. Runtime rt = Runtime.getRuntime()
  4. log.info(&quot;Running: &quot;+cmd)
  5. Process process = rt.exec(cmd.toString())
  6. StringBuilder cmdReturn = new StringBuilder()
  7. try{
  8. InputStream inputStream = process.getInputStream()
  9. int c
  10. while ((c = inputStream.read()) != -1) {
  11. cmdReturn.append((char) c)
  12. }
  13. log.info(&quot;CMD RESPONSE: &quot;+ cmdReturn)
  14. process.waitFor()
  15. }catch(Exception e){
  16. log.warn(e.getMessage())
  17. }finally{
  18. process.destroy()
  19. }

the log:

  1. 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
  2. 2020-08-27 09:21:59.119 INFO serverInformation -CMD RESPONSE:
  3. 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 进行多次服务器跳跃时无响应。

  1. def getLogs(String serverConnection,String serverKey, String fileName){
  2. if(osName.contains(&#39;windows&#39;)){
  3. ProcessBuilder pb = new ProcessBuilder(ssh(),//this could be plink or straight ssh
  4. &quot;-batch&quot;,
  5. &quot;-ssh&quot;,
  6. &quot;user@server1&quot;,
  7. &quot;-i&quot;,
  8. server1key(),//changes depending on server
  9. &quot;ssh&quot;,
  10. serverConnection,
  11. &quot;-i&quot;,
  12. serverKey,
  13. &quot;awk&quot;,
  14. &quot;&#39;\$0 &gt;= \&quot;${startTime.format(DateTimeFormatter.ofPattern(pattern))}\&quot; &amp;&amp; \$0 &lt;= \&quot;${endTime.format(DateTimeFormatter.ofPattern(pattern))}\&quot;&#39;&quot;,
  15. fileName
  16. )
  17. log.info &quot;running: &quot;+pb.toString() //didnt show me
  18. pb.redirectErrorStream()
  19. Process process = pb.start()
  20. StringBuilder cmdReturn = new StringBuilder()
  21. try{
  22. InputStream inputStream = process.getInputStream()
  23. BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))
  24. int c
  25. while ((c = reader.read()) != -1) {
  26. cmdReturn.append((char) c)
  27. }
  28. log.info(&quot;CMD RESPONSE: &quot;+ cmdReturn)
  29. process.waitFor()
  30. }catch(Exception e){
  31. log.warn(e.getMessage())
  32. }finally{
  33. process.destroy()
  34. }
  35. }else{
  36. ProcessBuilder pb = new ProcessBuilder(&quot;ssh&quot;,
  37. &quot;user@server1&quot;,
  38. &quot;-i&quot;,
  39. server1key(),
  40. &quot;ssh&quot;,
  41. serverConnection,
  42. &quot;-i&quot;,
  43. serverKey,
  44. &quot;awk&quot;,
  45. &quot;&#39;\$0 &gt;= \&quot;${startTime.format(DateTimeFormatter.ofPattern(pattern))}\&quot; &amp;&amp; \$0 &lt;= \&quot;${endTime.format(DateTimeFormatter.ofPattern(pattern))}\&quot;&#39;&quot;,
  46. fileName
  47. )
  48. pb.redirectErrorStream()
  49. Process process = pb.start()
  50. StringBuilder cmdReturn = new StringBuilder()
  51. try{
  52. InputStream inputStream = process.getInputStream()
  53. int c
  54. while ((c = inputStream.read()) != -1) {
  55. cmdReturn.append((char) c)
  56. }
  57. log.info(&quot;CMD RESPONSE: &quot;+ cmdReturn)
  58. process.waitFor()
  59. }catch(Exception e){
  60. log.warn(e.getMessage())
  61. }finally{
  62. process.destroy()
  63. }
  64. }
  65. }

Logs from this are not much different:

  1. 2020-09-02 16:50:54.520 INFO serverInformation -running: java.lang.ProcessBuilder@1c691f9f
  2. 2020-09-02 16:50:57.335 INFO serverInformation -CMD RESPONSE:
  3. 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文件:

  1. #!/bin/bash
  2. while getopts "s:e:c:k:f:" flag
  3. do
  4. case "${flag}" in
  5. s) startTime=${OPTARG};;
  6. e) endTime=${OPTARG};;
  7. c) serverConnection=${OPTARG};;
  8. k) serverKey=${OPTARG};;
  9. f) fileName=${OPTARG};;
  10. \?) echo "在缺少以下任一选项的情况下无法正常工作:
  11. -s startTime 格式:2020-08-27 09:06:56,236
  12. -e endTime 格式:2020-08-27 09:16:56,236
  13. -c server连接 例如:user@server
  14. -k id_rsa 文件
  15. -f 日志文件 例如:APPLICATION/logs/log.file"
  16. exit;;
  17. esac
  18. done
  19. ssh $serverConnection -i $serverKey "awk '\$0 >= \"$startTime\" && \$0 <= \"$endTime\"' $fileName"

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

  1. pb.command(ssh(),
  2. "-batch",
  3. "-ssh",
  4. "user@server",
  5. "-i",
  6. jumperKey(),
  7. "./awk.sh",
  8. "-s",
  9. startTime.format(DateTimeFormatter.ofPattern(pattern)).replaceAll(/ /,'\\\\ '),
  10. "-e",
  11. endTime.format(DateTimeFormatter.ofPattern(pattern)).replaceAll(/ /,'\\\\ '),
  12. "-c",
  13. serverConnection,
  14. "-k",
  15. serverKey,
  16. "-f",
  17. fileName
  18. )

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

英文:

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

  1. #!/bin/bash
  2. while getopts ?s:e:c:k:f: flag
  3. do
  4. case &quot;${flag}&quot; in
  5. s) startTime=${OPTARG};;
  6. e) endTime=${OPTARG};;
  7. c) serverConnection=${OPTARG};;
  8. k) serverKey=${OPTARG};;
  9. f) fileName=${OPTARG};;
  10. \?)echo &quot;This will not work without all options below:
  11. -s startTime format: 2020-08-27 09:06:56,236
  12. -e endTime format: 2020-08-27 09:16:56,236
  13. -c server connection eg: user@server
  14. -k id_rsa file
  15. -f logfile eg: APPLICATION/logs/log.file&quot;
  16. exit;;
  17. esac
  18. done
  19. 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

  1. pb.command(ssh(),//dbl check the environment we are in
  2. &quot;-batch&quot;,
  3. &quot;-ssh&quot;,
  4. &quot;user@server&quot;,
  5. &quot;-i&quot;,
  6. jumperKey(),//again to dbl check the environment we are in
  7. &quot;./awk.sh&quot;,
  8. &quot;-s&quot;,
  9. startTime.format(DateTimeFormatter.ofPattern(pattern)).replaceAll(/ /,&#39;\\\\ &#39;),
  10. &quot;-e&quot;,
  11. endTime.format(DateTimeFormatter.ofPattern(pattern)).replaceAll(/ /,&#39;\\\\ &#39;),
  12. &quot;-c&quot;,
  13. serverConnection,
  14. &quot;-k&quot;,
  15. serverKey,
  16. &quot;-f&quot;,
  17. fileName
  18. )

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:

确定