1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Custom update DNS

Discussion in 'Tomato Firmware' started by kameleon, Aug 7, 2007.

  1. kameleon

    kameleon LI Guru Member

    I have a domain name that I want to tie to certain machines in my LAN. My main router is a wrt54gs v2 running tomato. I have looked high and low for a script or a howto to write a script that I can run on the router to update the dns for the domain.

    I work at an ISP and the DNS is being hosted there. What I need is a script that will output this into a file.

    Code:
    server dns.mywork.com
    zone mydomainname.com
    update delete machine1.mydomainname.com
    update add machine1.mydomainname.com in a whatever.public.ip.address
    send
    So that I can run nsupdate -y 'my-key:really-long-key-here' fileabove.txt

    Also it needs to have redundancy checks to make sure the current IP hasn't changed and only update as needed. (the admin has threatened bodily harm if it updates too much ;) )

    On a side note the "something" in something.mydomainname.com is the machine name. And I will have at least 10 different machines that will need names.

    Also... I know I can pay $XX a year and have all this done via some service like dyndns but that does not interest me nor my bank account. I am looking to do it like this so that maybe I can learn a little from it.

    Thanks in advance for the help.
     
  2. yaqui

    yaqui LI Guru Member

    I am not sure about fileabove.txt... are they just config settings? but here is how you would put in a 'here document'. Not sure if it has to be executable or even a script... if not just take out #!/bin/sh and don't chmod +x it.
    Code:
    if [ ! -x /tmp/fileabove.txt ] ; then
    cat > /tmp/fileabove.txt <<EOF
    #!/bin/sh
    server dns.mywork.com
    zone mydomainname.com
    update delete machine1.mydomainname.com
    update add machine1.mydomainname.com in a whatever.public.ip.address
    send
    EOF
    nsupdate -y 'my-key:really-long-key-here' /tmp/fileabove.txt
    fi
     
  3. kameleon

    kameleon LI Guru Member

    Right.... but I need to be able to obtain the 'whatever.public.ip.address' which will be my current public ip address and input it in that part of the script. And it only needs to update when my ip actually changes.
     
  4. yaqui

    yaqui LI Guru Member

    Use read and awk from ifconfig, and for updating set up a while loop to check for your ip change at a certain interval.

    Edit: actually pipe output of ifconfig through grep first to get the line with the ip address into a temporary file then using awk on temp file and checking on the temp file might be easier
     
  5. yaqui

    yaqui LI Guru Member

    Maybe something similar to this?
    Code:
    #!/bin/sh
    
    old_ip="0.0.0.0"
    current_ip="0.0.0.0"
    
    sleep_time=30 # time in seconds to wait between loops
    user="user"
    pass="pass"
    
    max_days=10 # max number of days to go without forcing an update
    last_update=`date +%s`
    
    max_seconds=`expr $max_days*24*60*60`
    
    while : ; do
    
            current_ip=$(ifconfig $(nvram get wan_ifname) | grep inet | cut -d B -f1 | sed s/[^0-9.]//g)
    
            if [ $(($last_update+$max_seconds)) -lt `date +%s` ]
            then
                    # set old_ip to be different, forcing an update
                    old_ip="0.0.0.0"
            fi
    
            if [ $old_ip != $current_ip ]
            then
                    # current ip different from old ip
                    old_ip=$current_ip
                     
                    wget -q -O /dev/null "http://dns.mywork.com/dnsup?id=xxx&user=$user&pass=$pass&ip=${current_ip}"
                    last_update=$(date +%s)
            fi
    
            sleep $sleep_time
    done &
    Taken from here: http://forum.openwrt.org/viewtopic.php?pid=51764
     
  6. yaqui

    yaqui LI Guru Member

    This looks interesting also:
    Code:
    #!/bin/sh                                                                                                                           
                                                                                                                                        
    if [ "$(nvram get ddns_enable)" -eq "1" ]                                                                                           
    then                                                                                                                                
            ## get variables from nvram and command line                                                                                
            HOSTNAME=$(nvram get ddns_hostname)                                                                                         
            USERNAME=$(nvram get ddns_username)                                                                                         
            PASSWORD=$(nvram get ddns_password)                                                                                         
            INTERVAL=$(nvram get ddns_interval)                                                                                         
            LASTUPDATE=$(nvram get ddns_cache | awk 'BEGIN { FS=","; }{print $1}')                                                      
            LASTIP=$(nvram get ddns_cache | awk 'BEGIN { FS=","; }{print $2}')                                                          
            NEWIP="$1"                                                                                                                  
            CURRENTDATE=$(date +%s)                                                                                                     
                                                                                                                                        
            ## get the IP address from wan_ifname if not supplied                                                                       
            if [ -z "$NEWIP" ]                                                                                                          
            then                                                                                                                        
                    NEWIP=$(ifconfig $(nvram get wan_ifname) | grep inet | awk '{print $2}' | cut -c 6-)                                
            fi                                                                                                                          
                                                                                                                                        
            ## set default values for ddns_cache if not set                                                                             
            if [  -z "$LASTUPDATE"  -o  -z "$LASTIP"  ]                                                                                 
            then                                                                                                                        
                    LASTUPDATE=0                                                                                                        
                    LASTIP=0.0.0.0                                                                                                      
            fi                                                                                                                          
                                                                                                                                        
            ## set default values for ddns_interval if not set                                                                          
            if [ -z "$INTERVAL" ]                                                                                                       
            then                                                                                                                        
                    INTERVAL=600                                                                                                        
            fi                                                                                                                          
                                                                                                                                        
            ## do the update if the ip changed and the interval is exceeded                                                             
            if [ -n "$CURRENTDATE" -a $(($CURRENTDATE-$LASTUPDATE)) -gt $INTERVAL -a  "$LASTIP" != "$NEWIP" ]                           
            then                                                                                                                        
                   echo "Updating $HOSTNAME to $NEWIP"                                                                                 
                   wget -qO /dev/null http://$USERNAME:$PASSWORD@members.dyndns.org/nic/update?hostname=$HOSTNAME                      
                   nvram set ddns_cache="$CURRENTDATE,$NEWIP"                                                                          
                   nvram commit                                                                                                        
                                                                                                                                        
            else                                                                                                                        
                    echo "No update needed for $HOSTNAME"                                                                               
            fi                                                                                                                          
                                                                                                                                        
    fi
     
  7. kameleon

    kameleon LI Guru Member

    Thanks for that input. Maybe I can clarify myself a little. Here is what I need to do.

    I need to first check and see if my ip has changed from what the current DNS says it is. If it has not then do nothing. If it has then I need to obtain the current IP address and put it into a file that also contains the other information I need to update the DNS remotely. Basically look something like this:

    1. Check current IP--> If same as current DNS exit, is different go to step 2.
    2. Obtain current IP and input in a file (named update-ip.txt for instance) with this info and in this format:
    Code:
    server dns.mywork.com
    zone mydomainname.com
    update delete machine1.mydomainname.com
    update add machine1.mydomainname.com in a current.public.ip.address
    send 
    3. run nsupdate -y 'my-key:really-long-key-here' update-ip.txt

    As simple as that. There is no http address to go to on the dns server. And how would I set it to check my ip every so often.
     
  8. yaqui

    yaqui LI Guru Member

    So you want telnet to the router to run nslookup or you want the script to run it? If you study the above scripts, it looks like alot of this work has been done already.

    btw.. nslookup is under /usr/bin/, I previously thought it was not included in busybox shell.

    Additionally these are apparently your only options, I don't think you can use a text file with nslookup in busybox's implementation:
    Code:
    BusyBox v1.2.2 (2007.05.06-15:48+0000) multi-call binary
    
    Usage: nslookup [HOST] [SERVER]
    
    Queries the nameserver for the IP address of the given HOST
    optionally using a specified DNS server
    
    #
     

Share This Page