LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Create a bash file (https://www.linuxquestions.org/questions/programming-9/create-a-bash-file-644653/)

trolojik1 05-25-2008 10:09 AM

Create a bash file
 
Hi i want a bash file like that

#!/bin/bash
A=`netstat -an | grep ":80 "| awk '{print $5}'|cut -d":" -f1 | sort |uniq -c |sort -nr |$
for i in $A; do
if [ $i -gt 20 ]; then
echo higher value block that!!
echo item: $i
fi
done


if value is higher than 20 i want to block that ip adress

Can you help me ?

Regards;

MensaWater 05-25-2008 12:28 PM

Are you saying you want to create the file?

If so you can just decide the name of the file then type:
vi <filename>

This puts you in the vi (or vim) editor which has tons of options. You can type "man vi" before doing it to get an idea of all its capabilities.

Once in vi just insert the text you're interested in then save the file.

You have to change mode (permissions) on the file with the chmod command to make it executable. Type "man chmod" for details.

You then have to insure the directory is in your PATH variable OR remember to invoke it with fully qualified path. For example if the file is in your home directory (/home/mydir) you'd need to add /home/mydir to PATH if not already there or always type /home/mydir/<filename> whenever you want to access it.

This by the way is called a "shell script". It is similar (but more powerful) to DOS batch files. Most people but a .sh suffix on such files but Linux/UNIX doesn't require a suffix. You could name it bill.bob if you wanted to do so.

trolojik1 05-25-2008 01:10 PM

Hi buddy, please read becarefully
I dont have a problem with creating file. I Have a problem with coding. I Don't know how to coding what i want.

I Want : netstat -an | grep ":80 "| awk '{print $5}'|cut -d":" -f1 | sort |uniq -c |sort nr <-- do this command every 20 seccond by automaticly and if result is higher than 20 , i want block that ip adresses.

I Need help.

Regards

MensaWater 05-25-2008 01:37 PM

Quote:

please read becarefully
:mad:

From your original post I submit the title:

Quote:

Create a bash file
:scratch:

It seemed you had already coded what you wanted and just didn't know how to "create" the file.

Maybe in your next attempt you'll be more exact or at least learn not to annoy someone who tried to answer you. Maybe you'll even learn to edit what you write and not use the nonexistent word "becarefully". :tisk:

trolojik1 05-25-2008 03:03 PM

you right, my mistake;
I appoligise

trolojik1 05-25-2008 07:31 PM

can help anyone ?

i want a create bash file which contains that what i described

angrybanana 05-26-2008 02:48 AM

Change `echo "$ip"` with whatever command it is you use to block ip addresses.
Code:

while sleep 20;do
  while read ip;do
    echo "$ip"
  done < <(netstat -an|awk '/:80 /&&split($5,a,":"){ count[a[1]]++ } END{ for (x in count){ if (count[x]>20){ print x }}}')
done



All times are GMT -5. The time now is 11:21 AM.