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
–
–
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}'`
–
–
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
–