LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   need help with bash scripting (https://www.linuxquestions.org/questions/programming-9/need-help-with-bash-scripting-126661/)

rich2oo1 12-17-2003 11:25 AM

need help with bash scripting
 
Okay, here's what i'm trying to do, im trying to write a script, so that when i run it with a range of ip adresses, it will block them from being masqueraded. the code i'm using for that is:

Code:

iptables -A FORWARD -s 10.30.20.25 -j DROP
and it works fine. but what i want to do is, be able to run the script and pass a range of ip-addresses (like, say the script is named killinet, i wanna be able to run killinet 10.30.10.25-10.30.10.46) and deny internet access to all of the addresses in that range. thanks

druuna 12-17-2003 12:18 PM

You can do something like this:
Code:

#!/bin/bash

BOT_RANGE="`echo $1 | cut -d. -f4`"
TOP_RANGE="`echo $2 | cut -d. -f4`"

echo "BOT_RANGE: "${BOT_RANGE}
echo "TOP_RANGE: "${TOP_RANGE}

for (( i=${BOT_RANGE}; i<=${TOP_RANGE}; i++ ))
do
    echo "($i) your commands go here"
done

$ ./range.sh 1.2.3.4 1.2.3.8
BOT_RANGE: 4
TOP_RANGE: 8
(4) your commands go here
(5) your commands go here
(6) your commands go here
(7) your commands go here
(8) your commands go here

2 things:

1) I did not use the hyphen in the range. ie 1.2.3.4 1.2.3.8 vs. 1.2.3.4-1.2.3.8
If you cannot get rid og the hyphen, change the following:
Remove these 2 lines:
BOT_RANGE="`echo $1 | cut -d. -f4`"
TOP_RANGE="`echo $2 | cut -d. -f4`"
Replace them with these 4:
LOW_RANGE="`echo $1 | cut -d- -f1`"
HIG_RANGE="`echo $1 | cut -d- -f2`"
BOT_RANGE="`echo ${LOW_RANGE} | cut -d. -f4`"
TOP_RANGE="`echo ${HIG_RANGE} | cut -d. -f4`"

2) This only works if only the last field of the ip ranges are different. But I guess you can take it from here to fix that :)

unSpawn 12-17-2003 12:50 PM

Since the range is kinda small you could do
"seq 25 46|xargs -iS iptables -A FORWARD -s 10.30.10.S -j DROP"


All times are GMT -5. The time now is 01:20 AM.