Created
November 13, 2021 14:13
-
-
Save cmer/86b14468fb6e48c2f5c5822e1285a903 to your computer and use it in GitHub Desktop.
Whitelist IP with Telnyx (cronjob)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# This simple bash script will auto-update Telnyx with the network's public IP address when it changes. | |
# Simply add a cron job to call it every minute or so. | |
# | |
# requirements: apt install curl dnsutils jq | |
# | |
API_KEY="{{ API_KEY_HERE }}" | |
# Our current IP address and path to our IP cache file | |
IP_ADDRESS=`dig +short myip.opendns.com @resolver1.opendns.com` | |
CACHE_PATH="/tmp/ddns-cache.txt" | |
# Fetch last value of IP address sent to server or create cache file | |
if [ ! -f $CACHE_PATH ]; then touch $CACHE_PATH; fi | |
CURRENT=$(<$CACHE_PATH) | |
# If IP address hasn't changed, exit, otherwise save the new IP | |
if [ "$IP_ADDRESS" == "$CURRENT" ]; then | |
echo "IP didn't change. Not updating. $IP_ADDRESS" | |
exit 0 | |
fi | |
echo $IP_ADDRESS > $CACHE_PATH | |
# Update Telnyx | |
echo "Updating Telnyx with new IP: $IP_ADDRESS..." | |
curl --silent --location --request POST "https://api.telnyx.com/v2/access_control_ips" -H "Authorization: Bearer $API_KEY" -H "Content-Type: application/json" --data '{"ip_address": "'$IP_ADDRESS'", "ip_address_type": "media"}' | jq | |
curl --silent --location --request POST "https://api.telnyx.com/v2/access_control_ips" -H "Authorization: Bearer $API_KEY" -H "Content-Type: application/json" --data '{"ip_address": "'$IP_ADDRESS'", "ip_address_type": "signaling"}' | jq | |
echo "Done." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment