Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Suppose you are on linux internet gateway/router. What would you do to block a client with specific IP using bash script. I prefer something like ./block_client.sh 192.168.1.123 to block and ./unblock_client.sh 192.168.1.123

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted
$ cat block_client.sh
#!/bin/bash
iptables -I INPUT -s 192.168.1.123 -j DROP
$ cat unblock_client.sh
#!/bin/bash
iptables -D INPUT -s 192.168.1.123 -j DROP
share|improve this answer
3  
The IP block will not survive a reboot unless they are saved on update and restored on boot. look up iptables-save and iptables-restore. On Redhat like systems, you can get away with a simple "/etc/init.d/iptables save". –  Shawn Chin Aug 23 '10 at 14:13
add comment

You might want to deploy something like apf which you'll be able to execute apf -d 192.168.1.123 and apf -u 192.168.1.123 to block and unblock respectively. In addition APF keeps it's blocks list local and will insert them into the iptables on each reboot.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.