簡體   English   中英

Linux watch命令在腳本中不起作用

[英]Linux watch command not working in script

大家好

我正在做一個腳本,用於定期監視與端口(在本例中為80)的連接。

我寫了這個簡短的腳本。

echo '=================================';a=`sudo lsof -i :80`;echo $a | awk '{print $1," ",$2," ",$3," ",$8}'; b=`echo $a | wc -l`; b=$(($b - 1));echo Total SSH Connections: $b;echo '================================='

輸出為:

=================================  
COMMAND   PID   USER   NODE  
acwebseca   90   root   TCP  
acwebseca   90   root   TCP  
acwebseca   90   root   TCP  
acwebseca   90   root   TCP  
acwebseca   90   root   TCP  
acwebseca   90   root   TCP  
acwebseca   90   root   TCP  
acwebseca   90   root   TCP  
acwebseca   90   root   TCP  
acwebseca   90   root   TCP  
acwebseca   90   root   TCP  
acwebseca   90   root   TCP  
acwebseca   90   root   TCP  
acwebseca   90   root   TCP  
acwebseca   90   root   TCP  
acwebseca   90   root   TCP  
acwebseca   90   root   TCP  
acwebseca   90   root   TCP  
acwebseca   90   root   TCP  
Total SSH Connections: 19  
=================================  

但是當嘗試使用watch命令時,它會拋出錯誤,並且在取消命令時看不到輸出,我會看到如下錯誤:

sh: PID: command not found
                          sh: -c: line 1: syntax error near unexpected token `('
                                                                                sh: -c: line 1: `acwebseca  90 root   37u  IPv4 0x81ae738f91e7bed9      0t0  TCP 192.168.0.11:49915->108.160.163.33:http (ESTABLISHED)'

我怎樣才能解決這個問題。

watch -n 2 "echo '=================================';a=`sudo lsof -i :80`;echo $a | awk '{print $1," ",$2," ",$3," ",$8}'; b=`echo $a | wc -l`; b=$(($b - 1));echo Total SSH Connections: $b;echo '================================='"

如果將腳本寫入文件並執行它,它將起作用。 然后,它不必是一個可怕的單行代碼,但可以看起來像這樣:

echo '================================='
a=`sudo lsof -i :80`
echo $a | awk '{print $1," ",$2," ",$3," ",$8}'
b=`echo $a | wc -l`
b=$(($b - 1))
echo Total SSH Connections: $b
echo '================================='

只需將其放在文件中,然后運行watch my-script.sh 這樣就解決了問題,並使代碼同時可讀。

編輯:如果您真的想要單線,這是一個壞主意,則可以嘗試以下操作:

watch 'echo =================================;a=`lsof -i :80`;echo $a | awk "{print \$1, \$2, \$3, \$8}"; b=`echo $a | wc -l`; b=$(($b - 1));echo Total SSH Connections: $b;echo ================================='

基本上,我調整了報價以使其正常運行。 我可能會稍微弄亂awk格式,但是我敢肯定,如果需要,您可以將其改編成形狀。

這更多是評論,而不是答案,因為我不會處理傳遞命令watch 但是格式化注釋很難。 您可以通過在awk中執行更多操作來大大簡化命令:

 sudo lsof -i :80 | awk '
      BEGIN { d="================================="; print d}
      {print $1," ",$2," ",$3," ",$8}'
      END { print "Total SSH Connections:", NR-1; print d}'

引自man watch

Note that command is given to "sh -c" which means that you may need
to use extra quoting to get the desired effect.  You can disable this
with the -x or --exec option, which passes the command to exec(2) instead.

Note that POSIX option processing is used (i.e., option processing stops
at the first non-option argument).  This means that
flags after command don't get interpreted by watch itself.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM