forked from awslabs/route53-dynamic-dns-with-lambda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dyndns.sh
executable file
·68 lines (62 loc) · 1.95 KB
/
dyndns.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
#!/bin/bash
#Get aguments
while getopts ":u:m:h:s:" opt; do
case $opt in
u) url="$OPTARG"
;;
m) mode="$OPTARG"
;;
h) host="$OPTARG"
;;
s) secret="$OPTARG"
;;
\?) echo "Invalid option -$OPTARG" >&2
exit 1
;;
esac
case $OPTARG in
-*) echo "Option $opt needs a valid argument"
exit 1
;;
esac
done
# Call the Lambda URL in get mode to get the IP address
function getip {
#Evaluate -u argument
if [ -z "$url" ]; then
echo "Lambda url is required, pass it with argument -u, i.e. $0 -u https://xyz.lambda-url.eu-west1.on.aws/"
exit 1
fi
#Get IP
ip=`curl --ipv4 -q -s -X POST -H 'content-type: application/json' -d '{"execution_mode":"get"}' $url | egrep -o '[0-9\.]+'`
#Return error
if [ -z $ip ]; then
echo "Cannot retrieve IP, check your Lambda URL is responding"
exit 1
fi
}
#Check execution mode
if [ $mode = "get" ]; then
getip
echo $ip
exit 0
elif [ $mode = "set" ]; then
#Evaluate secret and host argument
if [ -z "$host" ]; then
echo "Host argument -h is required when in set mode, i.e. $0 -h test.aws.com -m set -s SHARED_SECRET_1 -u https://xyz.lambda-url.eu-west1.on.aws/"
fi
if [ -z "$secret" ]; then
echo "Shared secret argument -s is required when in set mode, i.e. $0 -h test.aws.com -m set -s SHARED_SECRET_1 -u https://xyz.lambda-url.eu-west1.on.aws/"
fi
if [ -z "$host" ] | [ -z "$secret" ]; then
exit 1
fi
getip
#Create hash
hash=`echo -n $ip$host$secret | shasum -a 256 | awk '{print $1}'`
#Call lambda url
curl --ipv4 -s -X POST -w ",{\"status_code\":\"%{http_code}\"}" -H 'content-type: application/json' -d '{"execution_mode":"'$mode'", "ddns_hostname":"'$host'", "validation_hash":"'$hash'"}' $url
else
echo "Mode is required as 'get' or 'set', pass it with argument -m, i.e. $0 -m get -u https://xyz.lambda-url.eu-west1.on.aws/"
exit 1
fi