forked from duo-labs/cloudmapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collect_data.sh
executable file
·110 lines (89 loc) · 3.17 KB
/
collect_data.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/bin/bash
set -euo pipefail
AWS_OPTS="--output json"
account=""
profile=""
ERROR=""
function usage {
if [[ ! -z $ERROR ]]
then
echo "ERROR: $ERROR"
fi
echo "Usage:"
echo " $0 --account my_account [--profile aws_profile]"
exit -1
}
echo "* Startup checks"
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
--account)
account="$2"
shift
shift
;;
--profile)
profile="$2"
shift
shift
;;
-h|--help)
usage
;;
*)
usage
;;
esac
done
if [[ -z "$account" ]]
then
ERROR="No account Name Specified"
usage
fi
if [[ ! -z "$profile" ]]
then
AWS_OPTS="$AWS_OPTS --profile $profile"
fi
# Ensure the aws cli exists
aws --version > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "ERROR: Install the aws cli"
exit -1
fi
# Ensure we have AWS credentials
aws $AWS_OPTS sts get-caller-identity > /dev/null
if [ $? -ne 0 ]; then
echo "ERROR: No AWS credentials set"
exit -1
fi
# Ensure jq exists
jq --help > /dev/null
if [ $? -ne 0 ]; then
echo "ERROR: jq not found"
exit -1
fi
mkdir -p "$account"
cd $account
echo "* Getting region names"
aws $AWS_OPTS ec2 describe-regions > describe-regions.json
# Create directory for each region name
cat describe-regions.json | jq -r '.Regions[].RegionName' | xargs -I{} sh -c 'mkdir -p $1' -- {}
echo "* Getting VPC info"
cat describe-regions.json | jq -r '.Regions[].RegionName' | xargs -I{} sh -c 'aws '"$AWS_OPTS"' ec2 --region "$1" describe-vpcs > "$1/describe-vpcs.json"' -- {}
echo "* Getting AZ info"
cat describe-regions.json | jq -r '.Regions[].RegionName' | xargs -I{} sh -c 'aws '"$AWS_OPTS"' ec2 --region "$1" describe-availability-zones > "$1/describe-availability-zones.json"' -- {}
echo "* Getting subnet info"
cat describe-regions.json | jq -r '.Regions[].RegionName' | xargs -I{} sh -c 'aws '"$AWS_OPTS"' ec2 --region "$1" describe-subnets > "$1/describe-subnets.json"' -- {}
echo "* Getting EC2 info"
cat describe-regions.json | jq -r '.Regions[].RegionName' | xargs -I{} sh -c 'aws '"$AWS_OPTS"' ec2 --region "$1" describe-instances > "$1/describe-instances.json"' -- {}
echo "* Getting RDS info"
cat describe-regions.json | jq -r '.Regions[].RegionName' | xargs -I{} sh -c 'aws '"$AWS_OPTS"' rds --region "$1" describe-db-instances > "$1/describe-db-instances.json"' -- {}
echo "* Getting ELB info"
cat describe-regions.json | jq -r '.Regions[].RegionName' | xargs -I{} sh -c 'aws '"$AWS_OPTS"' elb --region "$1" describe-load-balancers > "$1/describe-load-balancers.json"' -- {}
echo "* Getting security group info"
cat describe-regions.json | jq -r '.Regions[].RegionName' | xargs -I{} sh -c 'aws '"$AWS_OPTS"' ec2 --region "$1" describe-security-groups > "$1/describe-security-groups.json"' -- {}
echo "* Getting network interface info"
cat describe-regions.json | jq -r '.Regions[].RegionName' | xargs -I{} sh -c 'aws '"$AWS_OPTS"' ec2 --region "$1" describe-network-interfaces > "$1/describe-network-interfaces.json"' -- {}
echo "* Getting VPC peering info"
cat describe-regions.json | jq -r '.Regions[].RegionName' | xargs -I{} sh -c 'aws '"$AWS_OPTS"' ec2 --region "$1" describe-vpc-peering-connections > "$1/describe-vpc-peering-connections.json"' -- {}