forked from swoodford/aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ec2-classic-import-network-acl.sh
executable file
·54 lines (43 loc) · 1.67 KB
/
ec2-classic-import-network-acl.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env bash
# This script will read from the list of CIDR IPs in the file ipblacklistmaster
# Then create an AWS EC2 Classic ACL rule to deny access to each CIDR IP specified
# VERY IMPORTANT to set the correct Network ACL ID for the intended ACL
NETWORKACLID="YOUR-ACL-ID-HERE"
if [ "$NETWORKACLID" = "YOUR-ACL-ID-HERE" ]; then
tput setaf 1; echo "Failed to set Network ACL ID!" && tput sgr0
exit 1
fi
# Verify AWS CLI Credentials are setup
# http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html
if ! grep -q aws_access_key_id ~/.aws/config; then
if ! grep -q aws_access_key_id ~/.aws/credentials; then
echo "AWS config not found or CLI not installed. Please run \"aws configure\"."
exit 1
fi
fi
# exec &>> ~/vpcacl.log
echo "============================="
date '+%c'
echo "============================="
TOTALCIDR=$(wc -l ipblacklistmaster | cut -d " " -f5)
echo " "
echo "====================================================="
echo "Adding CIDR records to ACL"
echo "Records to be created: "$TOTALCIDR
echo "====================================================="
echo " "
START=1
for (( COUNT=$START; COUNT<=$TOTALCIDR; COUNT++ ))
do
echo "====================================================="
echo "Rule "\#$COUNT
CIDR=$(nl ipblacklistmaster | grep -w [^0-9][[:space:]]$COUNT | cut -f 2)
echo "CIDR="$CIDR
# echo "Network ACL ID: "$NETWORKACLID
ADDRECORD=$(aws ec2 create-network-acl-entry --network-acl-id $NETWORKACLID --rule-number $COUNT --protocol -1 --rule-action deny --ingress --cidr-block $CIDR)
echo "Record created: "$ADDRECORD
done
echo "====================================================="
echo " "
echo "Completed!"
echo " "