2

I use a simple shell script to block IP addresses that just substitutes my input (an IP) for a variable in an iptables, IPFW, etc. (depending on which platform on) command, but they all basically follow this format:

read -p "IP to Block: " ip

ipfw add deny ip from $ip to any

Is it possible to simply execute, sequentially, a group of user-defined variables. For example, three IP addresses, and have the script execute, basically, three times for each of the variables, but limit my input to a single prompt (being asked to input one time)?

1 Answer 1

1

Sounds like you want to do something like this:

for ip in $@; do
    ipfw add deny ip from $ip to any
done

I'm not sure if that's the exact usage of ipfw, but $@ is every word in a command except the first, so it would work with:

your-script-name 1.2.3.4   1.3.4.5   1.4.5.6
Sign up to request clarification or add additional context in comments.

2 Comments

Excellent, thank you. For IPFW, as long as each variable prints as a single IP address for each instance of the command, then this should suffice for the level of firewall administration that the script is intended to cover.
You can use it with read with prompt also, in the script read -p "IPs to Block (separate with space): " ips and then for ip in $ips; do ... instead. And then run it without arguments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.