Linux iptables mask specifies IP and IP segment

Source: Internet
Author: User

Iptables Shielded IP

The code is as follows Copy Code

Iptables-i input-s 202.77.176.106-j DROP

An entire paragraph, the order from 192.0.0.1 to 192.255.255.254.

The code is as follows Copy Code
Iptables-i input-s 192.0.0.0/8-j DROP

Prohibit IP paragraph when to see the amount of clearance, do not bar their own IP added to the amount of OH

Block a country IP segment

Now that you have all the IP addresses of the country, it's easy to block these IPs by writing a script to read Cn.zone files line-by-row and add them to the iptables:

The code is as follows Copy Code

#!/bin/bash
# block traffic from a specific country
# Written by Www.111cn.net

COUNTRY = "cn"
IPTABLES =/sbin/iptables
Egrep =/bin/egrep

If ["$ (id-u)"!= "0"]; Then
echo "You must be root" 1>&2
Exit 1
Fi

Resetrules () {
$IPTABLES-F
$IPTABLES-T Nat-f
$IPTABLES-T Mangle-f
$IPTABLES-X
}

Resetrules

For C in $COUNTRY
Todo
Country_file = $c. Zone

IPS = $ ($EGREP-V "^#|^$" $country _file)
For IP in $IPS
Todo
echo "Blocking $ip"
$IPTABLES-A input-s $ip-j DROP
Done
Done

Exit 0

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.