#!/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
我收到的错误:
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
答案1
中的变量名bash
区分大小写;您没有$IP
设置变量(尽管您有一个$ip
变量集):grep $IP
扩展为grep
,这会导致grep
错误和错误kill
。
更改此行
p=`ps ax | grep $IP | grep -v grep | grep -v /usr/sbin | awk '{print $1}'`
到这个
p=`ps ax | grep $ip | grep -v grep | grep -v /usr/sbin | awk '{print $1}'`
答案2
以下是对您的 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