代码之家  ›  专栏  ›  技术社区  ›  crsuarezf

Linux监视命令在脚本中不起作用

  •  2
  • crsuarezf  · 技术社区  · 12 年前

    大家好。

    我正在编写一个脚本,用于定期监控与端口的连接(在本例中为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 '================================='"
    
    3 回复  |  直到 12 年前
        1
  •  5
  •   John Zwinck    12 年前

    如果你把脚本写进一个文件并执行它,它就可以工作了。这样它就不必是一个可怕的单行,但可以看起来像这样:

    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格式搞砸了一点,但如果需要的话,我相信你可以把它恢复原状。

        2
  •  2
  •   William Pursell    12 年前

    这与其说是一个答案,不如说是一条评论,因为我不会把命令传递给 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}'
    
        3
  •  1
  •   CS Pei    7 年前

    引用 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.
    
    推荐文章