Skip to content

Commit f1ce149

Browse files
committed
Adding new scripts
1 parent 66433d0 commit f1ce149

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed

Management-Utilities/fsx-ontap-aws-cli-scripts/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ Before running the UNIX based scripts, make sure the following package is instal
2121
|list_fsx_filesystems.ps1 | List all the FSx for NetApp ONTAP filesystems that the user has access to, written in PowerShell. |
2222
|list_fsxn_volumes | List all the FSx for NetApp ONTAP volumes that the user has access to. |
2323
|list_fsxn_svms | List all the storage virtual machines that the user access to. |
24+
|list_aws_subnets | List all the aws subnets. |
25+
|list_aws_vpcs | List all the aws vpcs. |
2426
|delete_fsxn_filesystem | Deletes an FSx for NetApp ONTAP filesystem. |
2527
|delete_fsxn_svm | Deletes an svm. |
2628
|delete_fsxn_volume | Deletes a volume. |
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/bin/bash
2+
#
3+
# This script is used to list all VPCs in the AWS account.
4+
################################################################################
5+
#
6+
usage () {
7+
cat 1>&2 <<EOF
8+
Usage: $(basename $0) [-a] [-r region] [-v vpcId]
9+
Where:
10+
vpcId is the VPC id to list the subnets for.
11+
EOF
12+
exit 1
13+
}
14+
#
15+
# Check if the required tools are installed
16+
for tool in aws jq; do
17+
if which $tool > /dev/null 2>&1; then
18+
:
19+
else
20+
echo "Error, $tool command is rquired to run this script."
21+
exit 1
22+
fi
23+
done
24+
25+
allRegions=False
26+
regions=""
27+
vpcId=""
28+
while getopts "ar:hv:" opt; do
29+
case $opt in
30+
a) allRegions=True
31+
;;
32+
r) regions=$OPTARG
33+
;;
34+
v) vpcId=$OPTARG
35+
;;
36+
*) usage
37+
;;
38+
esac
39+
done
40+
41+
if [ "$allRegions" == "True" ]; then
42+
regions=$(aws ec2 describe-regions --query "Regions[].RegionName" --output=text)
43+
else
44+
if [ -z "$regions" ]; then
45+
regions=$(aws configure get region)
46+
fi
47+
fi
48+
49+
for region in $regions; do
50+
if [ "$allRegions" == "True" ]; then
51+
printf "\nRegion: $region\n"
52+
fi
53+
if [ -z "$vpcId" ]; then
54+
aws ec2 describe-subnets | jq -r '.Subnets[] | .VpcId + "," + .SubnetId + "," + .CidrBlock + "," + (first(.Tags[] | select(.Key == "Name").Value) // "")' | awk -F, 'BEGIN {formatStr="%21s %24s %18s %s\n"; printf(formatStr, "VPC Id", "Subnet ID", "CIDR", "Name")} {printf(formatStr , $1, $2, $3, $4)}'
55+
else
56+
aws ec2 describe-subnets --filters '[{"Name": "vpc-id", "Values": ["'$vpcId'"]}]' | jq -r '.Subnets[] | .VpcId + "," + .SubnetId + "," + .CidrBlock + "," + (first(.Tags[] | select(.Key == "Name").Value) // "")' | awk -F, 'BEGIN {formatStr="%21s %24s %18s %s\n"; printf(formatStr, "VPC Id", "Subnet ID", "CIDR", "Name")} {printf(formatStr , $1, $2, $3, $4)}'
57+
fi
58+
done
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!/bin/bash
2+
#
3+
# This script is used to list all VPCs in the AWS account.
4+
################################################################################
5+
#
6+
usage () {
7+
cat 1>&2 <<EOF
8+
Usage: $(basename $0) [-a] [-r region] [-s]
9+
where: -a list - All regions
10+
-r region - List VPCs in the specified region
11+
-s - List all the subnets in the VPC
12+
-q - Supress extraineous output
13+
-h - Print this help message
14+
EOF
15+
exit 1
16+
}
17+
18+
# Check if the required tools are installed
19+
for tool in aws jq; do
20+
if which $tool > /dev/null 2>&1; then
21+
:
22+
else
23+
echo "Error, $tool command is rquired to run this script."
24+
exit 1
25+
fi
26+
done
27+
28+
allRegions=False
29+
regions=""
30+
subnets=False
31+
quiet=False
32+
while getopts "qsar:h" opt; do
33+
case $opt in
34+
a) allRegions=True
35+
;;
36+
r) regions=$OPTARG
37+
;;
38+
s) subnets=True
39+
;;
40+
q) quiet=True
41+
;;
42+
*) usage
43+
;;
44+
esac
45+
done
46+
47+
tmpout=/tmp/list_aws_vpcs.$$
48+
trap 'rm -f $tmpout' exit
49+
50+
if [ "$allRegions" == "True" ]; then
51+
regions=$(aws ec2 describe-regions --query "Regions[].RegionName" --output=text)
52+
else
53+
if [ -z "$regions" ]; then
54+
regions=$(aws configure get region)
55+
fi
56+
fi
57+
58+
vpcFormatStr="%21s %19s %s\n"
59+
for region in $regions; do
60+
[ "$quiet" != "True" ] && printf "\nRegion: $region\n"
61+
first=True
62+
aws ec2 describe-vpcs --region $region | jq -r '.Vpcs[] | .VpcId + " " + .CidrBlock + " " + (if (.Tags != null) then (.Tags[] | (select(.Key == "Name") .Value)) else "" end)' | \
63+
while read vpcId cidr name; do
64+
if [ "$quiet" != "True" -a "$first" == "True" ]; then
65+
printf "\n$vpcFormatStr" "VPC IP" "CIDR" "Name"
66+
first=False
67+
fi
68+
echo "$vpcId,$cidr,$name" | awk -F, '{printf "'"$vpcFormatStr"'", $1, $2, $3}'
69+
70+
if [ "$subnets" == "True" ]; then
71+
printf "\n\tSubnets:\n"
72+
aws ec2 describe-subnets --filters '[{"Name": "vpc-id", "Values": ["'$vpcId'"]}]' | jq -r '.Subnets[] | .VpcId + " " + .SubnetId + " " + .CidrBlock + " " + (first(.Tags[] | select(.Key == "Name").Value) // "")' | awk 'BEGIN {formatStr="\t\t%24s %18s %s\n"; printf(formatStr, "Subnet ID", "CIDR", "Name")} {name=$4; for (i=5; i<=NF; i++) {name=name " " $(i)}; printf(formatStr , $2, $3, name)}'
73+
first=True
74+
fi
75+
done
76+
done

0 commit comments

Comments
 (0)