-
Notifications
You must be signed in to change notification settings - Fork 1
/
gcp-check-subnets.sh
executable file
·76 lines (66 loc) · 1.77 KB
/
gcp-check-subnets.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
#!/bin/bash
subnet=$1
type=$2
filter=$3
find=0
if [ "$filter" != "" ]
then
cmd='gcloud projects list --format="value(projectId)" --filter="$filter"'
else
cmd='gcloud projects list --format="value(projectId)"'
fi
echo "Date: $(date '+%Y-%m-%d %H:%M:%S')" > result.log
echo "Search: $subnet" >> result.log
echo "----------------------------------" >> result.log
echo "" > projects.log
echo "" > ranges.log
for project in $(eval $cmd)
do
echo -e "\033[1;34mChecking project: $project\033[0m"
echo "$project" >> projects.log
gcloud config set project $project
#gcloud config set disable_prompts true
has=""
for api in $(gcloud services list --enabled --filter compute.googleapis.com --format="value(NAME)")
do
has=$api
done
###echo $has
if [ "$has" = "compute.googleapis.com" ]
then
for range in $(gcloud compute networks subnets list --quiet --format="value(RANGE)")
do
echo "$range" >> ranges.log
if [ "$range" = "$subnet" ]
then
echo "Project ID: $project" >> result.log
echo "Range: $range" >> result.log
find=1
if [ "$type" = "first" ]
then
break;
fi;
fi
done
if [ $find -eq 1 ]
then
if [ "$type" = "first" ]
then
break;
fi;
fi
fi
echo -e "\033[0;34mChecked\033[0m"
done
if grep -q "Project ID:" result.log; then
echo -e "\033[0m######################################"
echo -e "\033[1;31mSubnet $subnet does exist!"
echo -e "\033[0m######################################\033[0;31m"
grep "Project ID:" result.log
grep "Range:" result.log
echo -e "\033[0m######################################";
else
echo -e "\033[0m######################################"
echo -e "\033[1;32mSubnet $subnet does not exist!"
echo -e "\033[0m######################################";
fi