1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Please read our transition notice before posting support questions. Thanks.

DNS-O-Matic

Discussion in 'Add-On Install Instructions' started by james, Apr 24, 2008.

  1. james Guru

    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
    
    
  2. TomS Guru

    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
  3. james Guru

    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($handle256);
    fclose($handle);
    ip_log("Received $ip");
    return 
    $ip;
    }
    //updates ip
    function update_ip($user$pass$ip){
    $handle fopen("https://$user:$pass@updates.dnsomatic.com/nic/update?hostname=all.dnsomatic.com&myip=$ip&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG",r);
    $result fread($handle256);
    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;


    ?>


  4. TomS Guru

    Great Idea!

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

    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
  6. james Guru

    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
  7. hjm New Member

    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.
  8. brunski Member

    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
  9. dstroot Guru

    Brilliant!
  10. brunski Member

    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
  11. dstroot Guru

    Yeah - I used the original once a day at midnight cron schedule. Every 5 mins is excessive.

Share This Page