how to write script to update the ip address

Hi all,


I need a help to write a script write on my Mac OS 10.10.1


I have a dynamic DNS that need to update through a URL for the new public IP address.


The script to get the current external IP address


curl -s checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//' 


This is the URL need to update everything 5 minutes with the output of the current public Ip address or it can detect the change if change then do update if no then do nothing,


https://domain.com/update?host=ab&domain=abc.com&password=xx&ip=1.1.1.1

Thanks for the help

MacBook Air, Mac OS X (10.7.2), 256GB SSD, 1.8GHZ i7

Posted on Jan 21, 2015 8:12 AM

Reply
12 replies

Jan 21, 2015 11:13 AM in response to Francis Yap

Storing passwords in a shell script is an EXTREMELY bad idea. The following will get the current IP address, validate the IP address, test whether the IP address has change by reading the file /var/tmp/stored_ip or create the file if it doesn't exits- DO YOUR COMMAND HERE is the line that I am going to let you figure out.



#!/bin/ksh


new_ip="$(/usr/bin/curl -s https://icanhazip.com/)"


if [ -f /var/tmp/stored_ip ]; then

if /usr/bin/host $new_ip > /dev/null 2>&1; then

printf "%s\n" "Valid IP address found"

else

printf "%s\n" "Invalid IP address"

exit 1

fi

else

printf "%s\n" "$new_ip" > /var/tmp/stored_ip

exit 1

fi


read -r stored_ip < /var/tmp/stored_ip


if [ "$new_ip" = "$stored_ip" ]; then

printf "%s\n" "IP address has not changed"

else

DO YOUR COMMAND HERE

fi


printf "%s\n" "$new_ip" > /var/tmp/stored_ip


exit 0

Jan 21, 2015 7:25 PM in response to Francis Yap

Francis Yap wrote:


Hi Drew,


I'm not using DYN, this is other dynamic DNS provider.

Care to be specific? Have you looked at their site for a Mac client or script, how about their forums? Linux examples can usually be adapted quickly.


Many other examples are around too…

http://techgeekjay.blogspot.co.uk/2013/03/no-ip-automatic-update-bash-script-for .html

https://freedns.afraid.org/scripts/freedns.clients.php


If you can't figure out the curl command then maybe you should be using a pre made script?

Jan 21, 2015 7:28 PM in response to Francis Yap

I'm not using DYN, this is other dynamic DNS provider.

Does your dynamic DNS provider supply a utility to do this update for you. DynDNS.com and No-IP.com do provide a program you can run on your Mac to keep your dynamic DNS name updated.


Or if your dynamic DNS vendor uses the standard dynamic DNS update protocols, then maybe you can have your home router do the service for you. A lot of routers offer this service.

Jan 22, 2015 7:59 PM in response to Francis Yap

Other people have the need for wheels, some people are really good at making them too…


https://www.namecheap.com/support/knowledgebase/article.aspx/5/11/are-there-any- alternate-dynamic-dns-clients

Follow the Mac link to the forum post & it leads you to…

http://idb.gosmd.net


Or if that is no use there is another variant that uses a shell script.

http://mactips.lotsaoxen.com/2012/09/ddns-dynamic-dns-updater-for-namecheap.html

Jan 23, 2015 10:12 AM in response to Francis Yap

The other replies about alternative providers notwithstanding (and may be better solutions), here's an example that meets your specific request.


You already have a script that identifies your current IP address, so all you need to do is capture that result and pass it to a second command that updates your DNS provider.

There are many ways to do this. Here's an AppleScript-based solution:


-- get your IP and store the result

set myIP to do shell script "curl -s checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//'"

-- now update your DNS provider

do shell script "https://domain.com/update?host=ab&domain=abc.com&password=xx&ip=" & myIP


Or a hacky shell-based solution:


#! /bin/bash

MYIP=`/usr/bin/curl -s checkip.dyndns.org|/usr/bin/sed -e 's/.*Current IP Address: //' -e 's/<.*$//'`

/usr/bin/curl https://domain.com/update?host=ab\&domain=abc.com\&password=xx\&ip=${MYIP}


Both do the same thing. Both can be scheduled to run automatically/periodically.

Dec 28, 2015 6:05 PM in response to Francis Yap

I modified someone's code I found online as it wasn't working for me. This seems to do the trick:


*NOTE I bashed my head for over an hour trying to figure out why it was failing and it seems you need to use the Updater Client Key and not your Dyn.com password to make this work.

The Updater Client Key can be found here: https://account.dyn.com/profile/

#!/bin/bash
#
# DynDNS for Namecheap
# including check between externel IP and existing DNS entries in the internet
#

HOSTNAME="hostname.com"                        # to update others make separate by comma
USERNAME="dynusername"                        # DYN.com username
PASSWORD="xxxxxxxxxxxxxxxxxxxxxxxxxx"        # DYN.com Updater Client Key

# get external IP
GETEXTIP=$(curl -0 --silent http://checkip.dyndns.org | awk '{print $6}' | cut -f 1 -d "<")

# get external IP from DNS
GETDNSIP=$(dig +short $HOSTNAME @8.8.8.8)


# Update DNS entry
if [ "$GETDNSIP" != "$GETEXTIP" ]; then
curl -0 --silent "http://$USERNAME:$PASSWORD@members.dyndns.org/nic/update?hostname=$HOSTNAME&myip=$GETEXTIP$PASSWORD@members.dyndns.org/nic/update?hostname=$HOSTNAME&myip=$GETEXTIP" > /dev/null
fi
exit 0

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

how to write script to update the ip address

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.