大家好。
我正在编写一个脚本来定期监视与端口的连接(在本例中为 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 '================================='"