DNS-O-Matic

james

Guru
Joined
Oct 18, 2007
Messages
374
Reaction score
38
https://www.dnsomatic.com/dnsomatic will update most popular dynamic dns servives. I named the script dns-o-matic and put it in usr/bin. It is called via Cron every night 1 minute to midnight:
59 23 * * * /usr/bin/dns-o-matic>/dev/null 2>&1

Code:
#/bin/bash
USERNAME="dnsomaticuser"
PASSWORD="dnsomaticpass"
ip=$(curl -s www.wieistmeineip.de | awk '(/[0-9]?[0-9]?[0-9]\.[0-9]?[0-9]?[0-9]\.[0-9]?[0-9]?[0-9]\.[0-9]?[0-9]?[0-9]/) {print}' | awk 'gsub(/[>||<]/," ")' | awk '{print $3}')
echo $ip
result=$(curl -s https://${USERNAME}:${PASSWORD}@updates.dnsomatic.com/nic/update?hostname=all.dnsomatic.com&myip=$ip&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG)
echo $result
 

TomS

Guru
Joined
Oct 18, 2007
Messages
240
Reaction score
6
Just another way to just get the IP address

I tested your script and it works well - Thanks very much!

I wanted to see if there was another source to get the IP Address and went to "Whatismyip.com". In the page it has a link to:
http://www.whatismyip.com/automation/n09230945.asp
This just gives you your IP Address only so no parsing is needed.

I also added a logging feature so you can test for your IP easily in a script.

Here is what it looks like so far: (I always add .sh to shell scripts)

# dns-o-matic.sh DNS-O-MATIC by James at Rhino

# "crontab -e" and then insert the appropriate line.
# Insert in crontab to run every night 1 minute to midnight:
# 59 23 * * * /usr/bin/dns-o-matic.sh >/dev/null 2>&1
# Remember to place the program in /usr/bin
# Make it executable "chmod 755 /usr/bin/dns-o-matic.sh"

USERNAME="dnsomaticuser"
PASSWORD="dnsomaticpass"

# Bring up the webpage that shows your IP address
# Select out your IP Address
ip=$(curl -s http://www.whatismyip.com/automation/n09230945.asp)
# Or ip=$(curl -s http://myip.dnsomatic.com)

# Print your IP Address
echo $ip

# Update dns-o-matic
# Don't forget to create an account at dnsomatic.com and select the appropriate service e.g. dyndns
# Use the same line as in the original script above:
result=$(curl -s https://${USERNAME}:${PASSWORD}@upd...myip=$ip&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG)

# Log the IP Address (dot filename will not be deleted on reboot)
echo -e "`date`:$ip" >> /tmp/.DNSOMATIC-LOG
chmod 666 /tmp/.DNSOMATIC-LOG

Thanks TomS
 

james

Guru
Joined
Oct 18, 2007
Messages
374
Reaction score
38
I am going to work this in to a freepbx module that allows you to set the username, password, and schedule the cron from the gui... well that's the goal. So I have converted this to php.....

PHP:
#!/usr/bin/php -q 
<?php
/*DNSOMATIC ala php
By James Finstrom
"crontab -e" and then insert the appropriate line.
Insert in crontab to run every night 1 minute to midnight:
59 23 * * * /usr/bin/dnsomatic.php username password >/dev/null 2>&1
Remember to place the program in /usr/bin
Make it executable "chmod 755 /usr/bin/dnsomatic.php"
*/
//logger
function ip_log($data){
$fp = fopen('/tmp/.DNSOMATIC-LOG', 'a');
$stamp = date(r);
fwrite($fp,"$stamp $data \n");
fclose($fp);
}
//get ip
function get_ip() {
$handle = fopen("http://www.whatismyip.com/automation/n09230945.asp",r);
$ip = fread($handle, 256);
fclose($handle);
ip_log("Received $ip");
return $ip;
}
//updates ip
function update_ip($user, $pass, $ip){
$handle = fopen("https://$user:[email protected]/nic/update?hostname=all.dnsomatic.com&myip=$ip&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG",r);
$result = fread($handle, 256);
fclose($handle);
return $result;
}
//calls the functions above
$result = update_ip($argv[1], $argv[2], get_ip());
$result = explode(" ", $result);
// log it
$message = "Update IP to $result[1] was $result[0]";
ip_log($message);

echo $message;


?>
 

TomS

Guru
Joined
Oct 18, 2007
Messages
240
Reaction score
6
Great Idea!

Great Idea! :wink5:
I am a shell programmer - the perl looks great and very straight forward.
thanks
Tom
 

hjm

New Member
Joined
Dec 7, 2007
Messages
18
Reaction score
0
I tested your script and it works well - Thanks very much!

I've tested this script and for me it did not work. Not being a programmer, i've done some cutting and pasting and combined your script with that of Nerd Vittles. I have accomplished two things now. First my children surf safe because OpenDNS knows my IP and secondly externIP gets updated.
This is what i made of it:

#!/bin/bash
# dns-o-matic.sh DNS-O-MATIC by James at Rhino, TomS, Ward Mundy & hjm

# "crontab -e" and then insert the appropriate line.
# Insert in crontab to run every 5 minutes:
# [FONT=&quot]*/5 * * * * /usr/bin/dns-o-matic.sh >/dev/null 2>&1[/FONT]
# Remember to place the program in /usr/bin
# Make it executable "chmod 755 /usr/bin/dns-o-matic.sh"

USERNAME="dns_o-matic_username"
PASSWORD="dns_o-matic_password"
localnet="192.168.1.0"

# Bring up the webpage that shows your IP address
# Select out your IP Address
ip=$(curl -s http://www.whatismyip.com/automation/n09230945.asp)
# Or ip=$(curl -s http://myip.dnsomatic.com)

# Print your IP Address
echo $ip

# Update dns-o-matic
# Don't forget to create an account at dnsomatic.com and select the appropriate service e.g. dyndns
# Use the same line as in the original script above:

if [ -e /tmp/$ip ] ; then
echo No IP Update Required ;
# echo -e "`date`:$ip" No update >> /tmp/.DNSOMATIC-LOG
else
echo IP Update Required ;
# Log the IP Address (dot filename will not be deleted on reboot)

echo -e "`date`:$ip" >> /tmp/.DNSOMATIC-LOG
chmod 666 /tmp/.DNSOMATIC-LOG

# Write to DNS-O-Matic
result=$(curl -s https://${USERNAME}:${PASSWORD}@updates.dnsomatic.com/nic/update?hostname=all.dnsomatic.com&myip=$ip&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG)
touch /tmp/$ip ;
echo "nat=yes" > /etc/asterisk/sip_nat.conf
echo "externip=$ip" >> /etc/asterisk/sip_nat.conf
echo "localnet=$localnet/255.255.255.0" >> /etc/asterisk/sip_nat.conf
asterisk -rx "dialplan reload" ;
fi

Thanks all
 

james

Guru
Joined
Oct 18, 2007
Messages
374
Reaction score
38
I had thought about the second half of the script especially if i make this a freepbx module... I am thinking it may be more appropriate to add a sip_ext.conf that can be added as an include. If someone is use to using sip_custom for certain tasks it would be bad kerma to over write it
 

hjm

New Member
Joined
Dec 7, 2007
Messages
18
Reaction score
0
I had thought about the second half of the script especially if i make this a freepbx module... I am thinking it may be more appropriate to add a sip_ext.conf that can be added as an include. If someone is use to using sip_custom for certain tasks it would be bad kerma to over write it

You are right. Just after i posted this i made a little change and let it write to sip_nat.conf.
I will update my earlier post.
 

brunski

Member
Joined
Oct 18, 2007
Messages
38
Reaction score
1
just adding some email notifications

#!/bin/bash
# dns-o-matic.sh DNS-O-MATIC by James at Rhino, TomS, Ward Mundy & hjm, brunski

# "crontab -e" and then insert the appropriate line.
# Insert in crontab to run every 5 minutes:
# */5 * * * * /usr/bin/dns-o-matic.sh >/dev/null 2>&1
# Remember to place the program in /usr/bin
# Make it executable "chmod 755 /usr/bin/dns-o-matic.sh"

USERNAME="dns_o-matic_username"
PASSWORD="dns_o-matic_password"
localnet="192.168.1.0"
EMAIL=your-email-address


# Bring up the webpage that shows your IP address
# Select out your IP Address
ip=$(curl -s http://www.whatismyip.com/automation/n09230945.asp)
# Or ip=$(curl -s http://myip.dnsomatic.com)

# Print your IP Address
echo $ip

# Update dns-o-matic
# Don't forget to create an account at dnsomatic.com and select the appropriate service e.g. dyndns
# Use the same line as in the original script above:

if [ -e /tmp/$ip ] ; then
echo No IP Update Required ;
# echo -e "`date`:$ip" No update >> /tmp/.DNSOMATIC-LOG
else
echo IP Update Required ;
# Log the IP Address (dot filename will not be deleted on reboot)

echo -e "`date`:$ip" >> /tmp/.DNSOMATIC-LOG
chmod 666 /tmp/.DNSOMATIC-LOG

# Write to DNS-O-Matic and send an email

echo 'IP address for' `hostname -f` 'is:' $ip 'on ' `date` | mail -s 'New IP' $EMAIL

result=$(curl -s https://${USERNAME}:${PASSWORD}@updates.dnsomatic.com/nic/update?hostname=all.dnsomatic.com&myip=$ip&wildcar d=NOCHG&mx=NOCHG&backmx=NOCHG)
touch /tmp/$ip ;
echo "nat=yes" > /etc/asterisk/sip_nat.conf
echo "externip=$ip" >> /etc/asterisk/sip_nat.conf
echo "localnet=$localnet/255.255.255.0" >> /etc/asterisk/sip_nat.conf
asterisk -rx "dialplan reload" ;
fi

#send email notifying of job execution
echo 'Cron Job: dns-o-matic executed for' `hostname` 'on ' `date` 'ip: ' $ip | mail -s 'dns-o-matic cron job' $EMAIL
 

brunski

Member
Joined
Oct 18, 2007
Messages
38
Reaction score
1
obviously the email thing works nicely when you have your cron job running once a day: i.e,

59 23 * * * /usr/bin/dns-o-matic.sh >/dev/null 2>&1

otherwise it just gets too anonying :D
 

dstroot

Guru
Joined
Dec 20, 2007
Messages
31
Reaction score
0
Yeah - I used the original once a day at midnight cron schedule. Every 5 mins is excessive.
 

Members online

Forum statistics

Threads
25,821
Messages
167,814
Members
19,247
Latest member
mdauck
Get 3CX - Absolutely Free!

Link up your team and customers Phone System Live Chat Video Conferencing

Hosted or Self-managed. Up to 10 users free forever. No credit card. Try risk free.

3CX
A 3CX Account with that email already exists. You will be redirected to the Customer Portal to sign in or reset your password if you've forgotten it.
Top