#!/bin/bash

#define path to private key
keypath=/path/to/jailed/key
#define where you want your publicip file to be stored on the nginx server
thetext=/home/user/publicip

ip1=$(curl icanhazip.com 2>/dev/null)

#backup for if curl icanhazip.com fails utilizing dig and opendns

backupcheck() {

local ip2=$(dig +short myip.opendns.com @resolver1.opendns.com) 2>/dev/null \
&& echo "$ip2" > "$thetext"

}

#waiting 60 seconds before trying the dig fall back method 
#this is in case the initial failure was due to a network blip
[ -z "$ip1" ] && sleep 60 && backupcheck || echo "$ip1" > "$thetext"

#update scp to use the actual remote ip of your nameserver
#the -z check paired with the logical OR does nothing if both curl and dig failed to return an ip
[ -z "$(cat "$thetext")" ] || scp -i "$keypath" "$thetext" jaileduser@1.2.3.4:/nginx/homeip