forked from raycast/script-commands
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhatismyregion.sh
executable file
·93 lines (77 loc) · 2.26 KB
/
whatismyregion.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
#!/bin/bash
# Dependency: requires curl (https://curl.se/)
# Install via homebrew: `brew install curl`
# Dependency: requires jq (https://stedolan.github.io/jq/)
# Install via homebrew: `brew install jq`
# Dependency: requires grepcidr (http://www.pc-tools.net/unix/grepcidr/)
# Install via homebrew: `brew install grepcidr`
# @raycast.schemaVersion 1
# @raycast.author Oğuzhan Yılmaz
# @raycast.authorURL https://github.com/c1982
# @raycast.title Find AWS Region by IP
# @raycast.mode fullOutput
# @raycast.packageName Developer Utilities
# @raycast.icon 🤖
# @raycast.description Copies the AWS IPv4 to the clipboard.
IPADDR=$(pbpaste)
IPRANGEFILE="${PWD}/ip-ranges.json"
download_ipranges_file(){
curl -o $IPRANGEFILE https://ip-ranges.amazonaws.com/ip-ranges.json
}
check_file_existence(){
if [ ! -f "$IPRANGEFILE" ]; then
echo "$IPRANGEFILE does not exists. Downloading..."
download_ipranges_file
fi
}
check_file_older_than_7days(){
if test `find "$IPRANGEFILE" -cmin +10080`
then
echo "$IPRANGEFILE file too old. Downloading..."
download_ipranges_file
fi
}
find_aws_prefix(){
for range in $AWS_RANGES; do
prefix=$(grepcidr "$range" <(echo "$IPADDR") >/dev/null && echo "$range")
if [[ ! -z $prefix ]]; then
echo $prefix
exit
fi
done
}
check_requirements(){
if [ -z $(which grepcidr) ];
then
echo "grepcidr not installed"
exit
fi
if [ -z $(which curl) ];
then
echo "curl not installed"
exit
fi
if [ -z $(which jq) ];
then
echo "jq not installed"
exit
fi
}
check_valid_ipv4(){
if [[ ! $IPADDR =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo "Couldn't read valid IPv4 address from clipboard"
exit
fi
}
check_requirements
check_file_existence
check_file_older_than_7days
check_valid_ipv4
AWS_RANGES=$(cat $IPRANGEFILE | jq -r '.prefixes[] | select(.service=="EC2") | select(.ip_prefix) | .ip_prefix')
PREFIX=$(find_aws_prefix)
if [[ -z $PREFIX ]]; then
echo "$IPADDR is not in AWS range"
exit
fi
SHOW_RANGE=$(cat $IPRANGEFILE | jq '.prefixes[] | select(.service=="EC2" and .ip_prefix=="'$PREFIX'")')
echo $SHOW_RANGE | jq --arg ip $IPADDR '. + {"ip":"'$IPADDR'"}'