LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-17-2003, 11:25 AM   #1
rich2oo1
LQ Newbie
 
Registered: May 2003
Distribution: Redhat 9.0
Posts: 5

Rep: Reputation: 0
Question 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
 
Old 12-17-2003, 12:18 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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
 
Old 12-17-2003, 12:50 PM   #3
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

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


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash scripting help (su ...) shwong Linux - General 1 11-02-2005 12:26 PM
Bash scripting pete1234 Programming 1 09-27-2005 01:48 AM
Bash Scripting help jgtg32a Programming 5 09-06-2005 09:38 AM
bash scripting vadon Linux - Newbie 6 05-10-2005 04:07 AM
HELP with BASH scripting atwah Linux - Newbie 6 09-09-2003 01:10 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:17 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration