添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

Stack Exchange Network

Stack Exchange network consists of 183 Q&A communities including Stack Overflow , the largest, most trusted online community for developers to learn, share their knowledge, and build their careers. Visit Stack Exchange

Ask Ubuntu is a question and answer site for Ubuntu users and developers. It only takes a minute to sign up.

Sign up to join this community

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams
#!/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 
    echo "file empty, no abuse"
if [ -n "$a" ]
    then echo "$k"
    echo "file empty, no abuse"
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
                What you need to, particularly if you are a beginner, is test each individual part in the terminal so each bit works, and build into the final script. as kos says variables are case-sensitive, but I think you may also have a issue with how the kill command is invoked (or why it is needed anyway), and also ip='...' could probably be echo $a | | awk '{print$2}' | tail -1 as you repeating the first bit. The first bit probably also use something like egrep -v "1.1.1.1|1.2.2.2|127.0.0.1|127.0.1." so you don't use grep a zillion times
– Wilf
                Jun 22, 2015 at 13:49
                What is your script supposed to do? The line k=kill -9 $p`` will actually kill the PID stored in $p. Is that what you want? I assume not or you wouldn't be running echo $k later. If you explain what you're trying to do we can give you a simpler version that works.
– terdon
                Jun 23, 2015 at 16:51

Variable names in bash are case-sensitive; you have no $IP variable set (although you have an $ip variable set): grep $IP expands to grep, which leads to the grep error and to the kill error.

Change this line

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

to this one

p=`ps ax | grep $ip | grep -v grep | grep -v /usr/sbin | awk '{print $1}'`
                now i get this error :   line 11: unexpected EOF while looking for matching ``'  line 30: syntax error: unexpected end of file    ======   line 11 being the kill command
– Vitalik Jimbei
                Jun 22, 2015 at 13:59
                @VitalikJimbei The $ip variable is not guaranteed to contain a string, and you're not testing it for empty strings. Try setting $ip to a custom value to test this, and if everything goes smoothly edit the script so that it caters for empty strings on $ip (if [ -n "$ip" ]; then [...])
– kos
                Jun 22, 2015 at 14:20
   1  #!/bin/bash
   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

Source

Well, it does check many things. But doesn't appear to cover all possible uses of e.g. multi-line assignments and here docs. – Hannu Jun 22, 2015 at 18:20