1

I am trying to creating shell script to block some ips whether the below one is the right way to do it

   #!/bin/bash 
   iptables 
   -A OUTPUT -d xx.xx.xx.xx/29 -j DROP
   -A OUTPUT -d xx.xx.xx.xx/32 -j DROP

I am trying to save this and execute it as a1.sh but it's not working

2 Answers 2

1

You need to make those into valid iptables commands:

#!/bin/bash 
iptables -A OUTPUT -d xx.xx.xx.xx/29 -j DROP
iptables -A OUTPUT -d xx.xx.xx.xx/32 -j DROP

Note that those commands block outgoing connections. It is more common for firewalls to block incoming connections.

Also, for some uses, you may prefer to consider -j REJECT in place of -j DROP. DROP will just silently drop the packet while the REJECT action will notify the sender that the packet will not go through.

Sign up to request clarification or add additional context in comments.

4 Comments

I did that but it's not getting added to iptables (I am executing the shell script in Android)
@Shan OK. What makes you think that the rules were not added to iptables? Did you run iptables -L and examine the output? Also, did the above commands generate any error messages when run?
They didn't generate any error..I examined using iptables -L and they were not there
I was not using the right editor for writing .sh script..used Notepad++ with EOL conversion now it's working fine..thanks
0

"iptables" is a command. Commands take optional arguments. The arguments must follow the command in the same line.

iptables -A ...
iptables -A ...

Comments

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.