Ajuda com script

-1
#!/bin/bash

a='netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n | grep -v '1 ' | grep -v '2 ' | grep -v '3 ' | grep -v '1.1.1.1' | grep -v '1.2.2.2' | grep -v '127.0.0.1' | grep -v '127.0.1.''
ip='netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n | grep -v '1 ' | grep -v '2 ' | grep -v '3 ' | grep -v '1.1.1.1' | grep -v '1.2.2.2' | grep -v '127.0.0.1' | grep -v '127.0.1.' | awk '{print$2}' | tail -1'
d='date'
p='ps ax | grep $IP | grep -v grep | grep -v /usr/sbin | awk '{print $1}''
k='kill -9 $p'
if [ -n "$a" ]
    then echo -e "file exists\n IPs copied in .../log folder" && echo -e "\n Current date: $d \n$a" >> /var/log/offendersips.log 
else 
    echo "file empty, no abuse"
fi
if [ -n "$a" ]
    then echo "$k"
else 
    echo "file empty, no abuse"
fi

O erro que recebo:

Usage: grep [OPTION]... PATTERN [FILE]...
Try 'grep --help' for more information.
kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]
file exists
IPs copied in .../log folder
    
por Vitalik Jimbei 22.06.2015 / 15:26

2 respostas

1

Aqui está uma análise estática para o seu script de shell:

   1  #!/bin/bash
   2  
   3  a='netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n | grep -v '1 ' | grep -v '2 ' | grep -v '3 ' | grep -v '1.1.1.1' | grep -v '1.2.2.2' | grep -v '127.0.0.1' | grep -v '127.0.1.''
        ^––SC2006 Use $(..) instead of legacy '..'.
   4  ip='netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n | grep -v '1 ' | grep -v '2 ' | grep -v '3 ' | grep -v '1.1.1.1' | grep -v '1.2.2.2' | grep -v '127.0.0.1' | grep -v '127.0.1.' | awk '{print$2}' | tail -1'
      ^––SC2034 ip appears unused. Verify it or export it.
         ^––SC2006 Use $(..) instead of legacy '..'.
   5  d='date'
        ^––SC2006 Use $(..) instead of legacy '..'.
   6  p='ps ax | grep $IP | grep -v grep | grep -v /usr/sbin | awk '{print $1}''
        ^––SC2006 Use $(..) instead of legacy '..'.
         ^––SC2009 Consider using pgrep instead of grepping ps output.
                      ^––SC2086 Double quote to prevent globbing and word splitting.
   7  k='kill -9 $p'
        ^––SC2006 Use $(..) instead of legacy '..'.
                 ^––SC2086 Double quote to prevent globbing and word splitting.
   8  if [ -n "$a" ]
   9      then echo -e "file exists\n IPs copied in .../log folder" && echo -e "\n Current date: $d \n$a" >> /var/log/offendersips.log 
  10  else 
  11      echo "file empty, no abuse"
  12  fi
  13  if [ -n "$a" ]
  14      then echo "$k"
  15  else 
  16      echo "file empty, no abuse"
  17  fi

Fonte

    
por A.B. 22.06.2015 / 17:12
3

Nomes de variáveis em bash fazem distinção entre maiúsculas e minúsculas; você não tem nenhum conjunto de variáveis $IP (embora tenha uma variável $ip definida): grep $IP expande para grep , o que leva ao erro grep e ao erro kill .

Alterar esta linha

p='ps ax | grep $IP | grep -v grep | grep -v /usr/sbin | awk '{print $1}''

para este

p='ps ax | grep $ip | grep -v grep | grep -v /usr/sbin | awk '{print $1}''
    
por kos 22.06.2015 / 15:41