Skip to content
This repository has been archived by the owner on Jan 29, 2024. It is now read-only.

Commit

Permalink
Add logging, covert sh to dash compatitable
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuaavalon committed Sep 20, 2017
1 parent 83d68df commit db57ea8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
1 change: 0 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ FROM alpine:3.6
MAINTAINER JoshuaAvalon

RUN apk add --update curl && \
apk add --update bash && \
apk add --update perl && \
rm -rf /var/cache/apk/*

Expand Down
21 changes: 12 additions & 9 deletions cloudflare.sh
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
#!/bin/sh
echo `date "+%Y-%m-%d %H:%M:%S"`

date "+%Y-%m-%d %H:%M:%S"
ip_file="ip.txt"

new_ip=$(curl -s http://ipecho.net/plain)

if [ -f $ip_file ]; then
ip=$(cat $ip_file)
if [ $ip == $new_ip ]; then
if [ "$ip" = "$new_ip" ]; then
echo "Same ip: $ip"
exit 0
fi
fi

ip="$new_ip"
zone_id=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$ZONE" -H "X-Auth-Email: $EMAIL" -H "X-Auth-Key: $API" -H "Content-Type: application/json" | perl -nle'print $& if m{(?<="id":")[^"]*}' | head -1 )
echo "IP: $ip"
zone_id=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$ZONE" -H "X-Auth-Email: $EMAIL" -H "X-Auth-Key: $API" -H "Content-Type: application/json" | perl -nle'print $& if m{(?<="id":")[^"]*}' | head -1 )
echo "Zone ID: $zone_id"
record_id=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records?name=$ZONE" -H "X-Auth-Email: $EMAIL" -H "X-Auth-Key: $API" -H "Content-Type: application/json" | perl -nle'print $& if m{(?<="id":")[^"]*}')
echo "Record ID: $record_id"
update=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records/$record_id" -H "X-Auth-Email: $EMAIL" -H "X-Auth-Key: $API" -H "Content-Type: application/json" --data "{\"id\":\"$zone_id\",\"type\":\"A\",\"name\":\"$ZONE\",\"content\":\"$ip\",\"ttl\":$TTL,\"proxied\":$PROXY}")

if [[ ${update} == *"\"success\":false"* ]]; then
echo "API UPDATE FAILED. DUMPING RESULTS:\\n$update"
exit 1
else
if echo "$update" | grep -q "\"success\":true"; then
echo "IP changed to: $ip"
echo "$ip" > $ip_file
echo "$ip" > $ip_file
else
printf "Update failed:\\n%s" "$update"
exit 1
fi

0 comments on commit db57ea8

Please sign in to comment.