Update DDNS with RouterTech 2.94

All about firmwares for routers. Support for RouterTech firmwares is here too.
User avatar
TafnaSM
Regular
Regular
Posts: 40
Joined: Sat Apr 10, 2010 4:16 pm
Location: Cairns, QLD, Australia

Update DDNS with RouterTech 2.94

Post by TafnaSM » Thu Dec 02, 2010 12:38 am

Sometimes for unknown reasons my router will disconnect and reconnect, receiving a new WAN IP address. This behavior is OK, but: In some cases it doesn't update the DDNS entry at dyndns.
How can i initiate this by script. The idea is to put it into a cron job and fire it lets say every day.

MfG Stefan Froehlich
42 ;-)
User avatar
thechief
RouterTech Team
RouterTech Team
Posts: 12067
Joined: Wed Feb 01, 2006 10:22 pm
Location: England, the Centre of Africa
Contact:

Re: Update DDNS with RouterTech 2.94

Post by thechief » Thu Dec 02, 2010 11:25 am

Create a cron job that runs "killall pppd" at the required times.
The Chief: :afro: Be sure to read the Firmware FAQ and do a Forum Search before posting!
No support via PM. Ask all questions on the open forum.
User avatar
TafnaSM
Regular
Regular
Posts: 40
Joined: Sat Apr 10, 2010 4:16 pm
Location: Cairns, QLD, Australia

Re: Update DDNS with RouterTech 2.94

Post by TafnaSM » Thu Dec 02, 2010 2:10 pm

thechief wrote:Create a cron job that runs "killall pppd" at the required times.
Is there a way to detect when the router goes offline and online again?

MfG Stefan Froehlich
42 ;-)
User avatar
thechief
RouterTech Team
RouterTech Team
Posts: 12067
Joined: Wed Feb 01, 2006 10:22 pm
Location: England, the Centre of Africa
Contact:

Re: Update DDNS with RouterTech 2.94

Post by thechief » Thu Dec 02, 2010 6:37 pm

Code: Select all

dmesg | grep [t]n7atm_close
If you get something like "tn7atm_close: closing 0.0.38.5" (or even anything at all) then there has been a disconnection. Of course this will not be reliable unless you clear the ring buffer after each positive check ("dmesg -c").
The Chief: :afro: Be sure to read the Firmware FAQ and do a Forum Search before posting!
No support via PM. Ask all questions on the open forum.
User avatar
TafnaSM
Regular
Regular
Posts: 40
Joined: Sat Apr 10, 2010 4:16 pm
Location: Cairns, QLD, Australia

Re: Update DDNS with RouterTech 2.94

Post by TafnaSM » Sat Dec 04, 2010 3:04 am

thechief wrote:Create a cron job that runs "killall pppd" at the required times.
To whom it may concern:
I wrote a script that is retrieves the ip address from a given hostname and compares it to the ip address of the router. If they doesn't match, it resets the connection (it kills pppd as suggested by thechief). The purpose is to keep the router's ip address in sync with a dns entry. On my router sometimes the information on dyndns.org won't be updated for an unknown reason.
I've named it stf_check_myhostip.sh. For example if your hostname is "mydnsentry.homedns.org" call the script this way:
stf_check_myhostip.sh mydnsentry.homedns.org.

Code: Select all

#!/bin/sh
ULB=/usr/local/bin
. $ULB/rt_utils.sh
. $ULB/WANcheck.sh

# $1 the hostname to resolve
get_host_ip()
{
  PINGRESULT=$(ping -c 1 -s 1 $1 2> /dev/null | grep "PING $1" | sed s/'PING '$1' ('/''/ | sed s/'): 1 data bytes'/''/)
  echo $PINGRESULT
}

# $1 the hostname to resolve
# returns
#         "noownip" when we have no ip
#         "nohostip" when hostname has no ip (that is: can't be resolved)
#         "1" if the hostname's ip is the same as our ip
compare_host_myip()
{
  MYIP=$(get_WAN_info "ip")
  if [ "$MYIP" = "N/A" ]
  then
    echo "noownip"
    return
  fi
  HOIP=$(get_host_ip $1)
  if [ -z "$HOIP" ]
  then
    echo "nohostip"
    return
  fi
  if [ $MYIP = $HOIP ]
  then
    echo "1"
  else
    echo "0"
  fi
}

# Make sure we have WAN connection before entering the loop
w=$(WaitForSync 10 24 1)

RESULT=$(compare_host_myip $1)
# echo "My IP:"$(get_WAN_info "ip")
# echo "Host IP:"$(get_host_ip $1)
# echo "Result was: $RESULT"
if [ "$RESULT" = "0" ]
then
  killall pppd
  echo "Connection reset"
fi
You can set up a cron job whitch calls this script let's say every hour.
If the script detects, that the given host name's ip doesn't match your WAN ip, it resets the connection. If it detects, that the given hostname is invalid (has no IP address), it does nothing. If it detects that the router has no connection yet, it does nothing.

MfG Stefan Froehlich
42 ;-)
mstombs
RouterTech Team
RouterTech Team
Posts: 3753
Joined: Wed Jan 10, 2007 11:54 pm

Re: Update DDNS with RouterTech 2.94

Post by mstombs » Sat Dec 04, 2010 11:29 am

Watch out "killall pppd" works fine for pppoA connections, but probably not for pppoE, but if you search the forums there are some cli commands that do the same as the disconnect/connect buttons in the web gui.
User avatar
thechief
RouterTech Team
RouterTech Team
Posts: 12067
Joined: Wed Feb 01, 2006 10:22 pm
Location: England, the Centre of Africa
Contact:

Re: Update DDNS with RouterTech 2.94

Post by thechief » Sat Dec 04, 2010 2:23 pm

mstombs wrote:Watch out "killall pppd" works fine for pppoA connections, but probably not for pppoE, but if you search the forums there are some cli commands that do the same as the disconnect/connect buttons in the web gui.
For pppoe you need to do something like this

Code: Select all

CON=connection0 # but it could be connection1, connection2, etc
echo "begin;$CON:pppoe:command/stop;end" | cm_cli
sleep 1
echo "begin;$CON:pppoe:command/start;end" | cm_cli
The problem with pppoe is that you have to get the connection number (i.e, connection0, or connection1, or connection2, or whatever). I haven't found a straightforward (or even completely reliable) way of doing that.
The Chief: :afro: Be sure to read the Firmware FAQ and do a Forum Search before posting!
No support via PM. Ask all questions on the open forum.
User avatar
thechief
RouterTech Team
RouterTech Team
Posts: 12067
Joined: Wed Feb 01, 2006 10:22 pm
Location: England, the Centre of Africa
Contact:

Re: Update DDNS with RouterTech 2.94

Post by thechief » Sat Dec 04, 2010 2:55 pm

TafnaSM wrote:To whom it may concern:
I wrote a script that is retrieves the ip address from a given hostname and compares it to the ip address of the router. If they doesn't match, it resets the connection (it kills pppd as suggested by thechief). The purpose is to keep the router's ip address in sync with a dns entry. On my router sometimes the information on dyndns.org won't be updated for an unknown reason.
I've named it stf_check_myhostip.sh. For example if your hostname is "mydnsentry.homedns.org" call the script this way:
stf_check_myhostip.sh mydnsentry.homedns.org.
Looks good (bearing in mind the issue raised by mstombs above - but I don't think you use pppoe). Just one little point: you don't check for an empty parameter before proceeding. It can be done very simply by adding something like this at beginning (after the includes):

Code: Select all

[ $HELPVAR -eq 1 ] && {
  echo "Syntax = $ARGV0 <your dynamic DNS hostname>"
  echo "Example: $ARGV0 mydnsentry.homedns.org"
  exit 0
}
HELPVAR will be equal to 1 if no parameter is passed to the script, or if "--help" is passed.
The Chief: :afro: Be sure to read the Firmware FAQ and do a Forum Search before posting!
No support via PM. Ask all questions on the open forum.
User avatar
thechief
RouterTech Team
RouterTech Team
Posts: 12067
Joined: Wed Feb 01, 2006 10:22 pm
Location: England, the Centre of Africa
Contact:

Re: Update DDNS with RouterTech 2.94

Post by thechief » Sat Dec 04, 2010 5:27 pm

thechief wrote:The problem with pppoe is that you have to get the connection number (i.e, connection0, or connection1, or connection2, or whatever). I haven't found a straightforward (or even completely reliable) way of doing that.
Actually, it seems that I had already solved this a while back ("get_connection_type" in rt_utils.sh), and simply forgot. It can be used in this way, to kill and restart the ppp connection:

Code: Select all

CT=$(get_connection_type)
[ -z "$CT" ] && {
 LOG_MSG "No valid connection. Quitting."
 exit 
}
LOG_MSG "The current connection is \"$CT\" - resetting the connection."
echo "begin;$CT:command/stop;end" | cm_cli
sleep 1
echo "begin;$CT:command/start;end" | cm_cli
You can simply call "ppp_restore.sh" instead of "killall pppd". It is a wrapper that will work with pppoa connections, but is rather dubious for pppoe. It will be updated in v2.95 to take account of this fix, instead of the pppoe kludge that is currently in it.

If you don't object, then your script can be added to firmware v2.95, in this format (perhaps with a few tweaks for increased flexibility)

Code: Select all

#!/bin/sh
# ------------------------------------------------------------
# RouterTech firmware script to check whether dynamic DNS is up-to-date; 
# if not, reset the ppp connection
# (c)2010, TafnaSM, RouterTech
# Author: TafnaSM
# Last amended: 4 December 2010
# ------------------------------------------------------------
ULB=/usr/local/bin
. $ULB/rt_utils.sh
. $ULB/WANcheck.sh

# empty parameter or "--help"
[ $HELPVAR -eq 1 ] && {
  echo "Syntax = $ARGV0 <your dynamic DNS hostname>"
  echo "Example: $ARGV0 mydnsentry.homedns.org"
  exit 0
}

# $1 the hostname to resolve
get_host_ip()
{
  PINGRESULT=$(ping -c 1 -s 1 $1 2> /dev/null | grep "PING $1" | sed s/'PING '$1' ('/''/ | sed s/'): 1 data bytes'/''/)
  echo $PINGRESULT
}

# $1 the hostname to resolve
# returns
#         "noownip" when we have no ip
#         "nohostip" when hostname has no ip (that is: can't be resolved)
#         "1" if the hostname's ip is the same as our ip
compare_host_myip()
{
  MYIP=$(get_WAN_info "ip")
  [ "$MYIP" = "N/A" ] && {
    echo "noownip"
    return
  }
  HOIP=$(get_host_ip $1)
  [ -z "$HOIP" ] && {
    echo "nohostip"
    return
  }  
  [ $MYIP = $HOIP ] && echo "1" || echo "0"
}

# Make sure we have WAN connection before entering the loop
w=$(WaitForSync 10 24 1)
RESULT=$(compare_host_myip $1)
[ "$RESULT" = "0" ] && $ULB/ppp_restore.sh
The Chief: :afro: Be sure to read the Firmware FAQ and do a Forum Search before posting!
No support via PM. Ask all questions on the open forum.
User avatar
TafnaSM
Regular
Regular
Posts: 40
Joined: Sat Apr 10, 2010 4:16 pm
Location: Cairns, QLD, Australia

Re: Update DDNS with RouterTech 2.94

Post by TafnaSM » Sun Dec 05, 2010 12:01 pm

thechief wrote:
TafnaSM wrote:To whom it may concern:
I wrote a script that is retrieves the ip address from a given hostname and compares it to the ip address of the router. If they doesn't match, it resets the connection (it kills pppd as suggested by thechief). The purpose is to keep the router's ip address in sync with a dns entry. On my router sometimes the information on dyndns.org won't be updated for an unknown reason.
I've named it stf_check_myhostip.sh. For example if your hostname is "mydnsentry.homedns.org" call the script this way:
stf_check_myhostip.sh mydnsentry.homedns.org.
Looks good (bearing in mind the issue raised by mstombs above - but I don't think you use pppoe). Just one little point: you don't check for an empty parameter before proceeding. It can be done very simply by adding something like this at beginning (after the includes):

Code: Select all

[ $HELPVAR -eq 1 ] && {
  echo "Syntax = $ARGV0 <your dynamic DNS hostname>"
  echo "Example: $ARGV0 mydnsentry.homedns.org"
  exit 0
}
HELPVAR will be equal to 1 if no parameter is passed to the script, or if "--help" is passed.
Yes, i know that. But if it is empty, the ping will fail and so compare_host_myip will return nohostip.

MfG Stefan Fröhlich
42 ;-)
User avatar
TafnaSM
Regular
Regular
Posts: 40
Joined: Sat Apr 10, 2010 4:16 pm
Location: Cairns, QLD, Australia

Re: Update DDNS with RouterTech 2.94

Post by TafnaSM » Sun Dec 05, 2010 12:09 pm

thechief wrote:If you don't object, then your script can be added to firmware v2.95, in this format (perhaps with a few tweaks for increased flexibility)
That's the reason why i've posted it here. But don't you think at least the function get_host_ip is usefull for other things and you should move it into rt_utils.sh?
However, if not, I've made three little changes to the script:

Code: Select all

#!/bin/sh
# ------------------------------------------------------------
# RouterTech firmware script to check whether dynamic DNS is up-to-date;
# if not, reset the ppp connection
# (c)2010, Stefan Fröhlich, RouterTech
# Author: Stefan Fröhlich, thechief
# Last amended: 4 December 2010
# ------------------------------------------------------------
ULB=/usr/local/bin
. $ULB/rt_utils.sh
. $ULB/WANcheck.sh

# empty parameter or "--help"
[ $HELPVAR -eq 1 ] && {
  echo "Syntax = $ARGV0 <your dynamic DNS hostname>"
  echo "Example: $ARGV0 mydnsentry.homedns.org"
  exit 0
}

# $1 the hostname to resolve
get_host_ip()
{
  PINGRESULT=$(ping -c 1 -s 1 $1 2> /dev/null | grep "PING $1" | sed s/'PING '$1' ('/''/ | sed s/'): 1 data bytes'/''/)
  echo $PINGRESULT
}

# $1 the hostname to resolve
# returns
#         "noownip" when we have no ip
#         "nohostip" when hostname has no ip (that is: can't be resolved)
#         "1" if the hostname's ip is the same as our ip
#         "0" if the hostnames's ip is different from your ip
compare_host_myip()
{
  MYIP=$(get_WAN_info "ip")
  [ "$MYIP" = "N/A" ] && {
    echo "noownip"
    return
  }
  HOIP=$(get_host_ip $1)
  [ -z "$HOIP" ] && {
    echo "nohostip"
    return
  } 
  [ $MYIP = $HOIP ] && echo "1" || echo "0"
}

# Make sure we have WAN connection before testing
w=$(WaitForSync 10 24 1)
RESULT=$(compare_host_myip $1)
[ "$RESULT" = "0" ] && $ULB/ppp_restore.sh
Changes:
1) Author
2) Completed comment for function compare_host_myip
3) Changed comment at the end, because it wasn't a loop (In the first incarnation there was a loop when the script was called from init. But a cron job is definitively a better way to do that).

MfG Stefan Fröhlich
42 ;-)
User avatar
thechief
RouterTech Team
RouterTech Team
Posts: 12067
Joined: Wed Feb 01, 2006 10:22 pm
Location: England, the Centre of Africa
Contact:

Re: Update DDNS with RouterTech 2.94

Post by thechief » Sun Dec 05, 2010 4:41 pm

TafnaSM wrote:
thechief wrote:If you don't object, then your script can be added to firmware v2.95, in this format (perhaps with a few tweaks for increased flexibility)
That's the reason why i've posted it here.
I know. But it is still polite to ask. ;) And it prevents future misunderstandings.
TafnaSM wrote:But don't you think at least the function get_host_ip is usefull for other things and you should move it into rt_utils.sh?
Yes, it should. I will move it there. There is just one little problem - in many cases, if the host is invalid, you will get a response containing the IP address of your DNS server. In cases where you are only checking for hosts that you know to exist (e.g., your ddns host), this is no problem. But for general use, where the host may be valid or invalid, perhaps some further tests might need to be added.
TafnaSM wrote:However, if not, I've made three little changes to the script:
Many thanks.
One change that I have made is this

Code: Select all

MYHOST="$1"
# empty parameter or "--help"
[ $HELPVAR -eq 1 ] && {
N=0
  # if no parameter, then check for "myddns_host" in the env
  [ -z "$MYHOST" ] && {
    MYHOST=$(ramenv "myddns_host") 
    [ -n "$MYHOST" ] && N=1
  }
  [ $N -eq 0 ] && {  
    echo "Syntax = $ARGV0 <your dynamic DNS hostname>"
    echo "Example: $ARGV0 mydnsentry.homedns.org"
    echo ""
    echo 'The host name can be stored in the environment variable: "myddns_host".'
    echo "If this is done, then no argument is needed at the command line."
    exit
  }
}
This allows you to specify the host in an environment variable ("myddns_host"), dispensing with the need to provide a command-line argument.
The Chief: :afro: Be sure to read the Firmware FAQ and do a Forum Search before posting!
No support via PM. Ask all questions on the open forum.
User avatar
TafnaSM
Regular
Regular
Posts: 40
Joined: Sat Apr 10, 2010 4:16 pm
Location: Cairns, QLD, Australia

Re: Update DDNS with RouterTech 2.94

Post by TafnaSM » Mon Dec 06, 2010 3:34 am

thechief wrote:
TafnaSM wrote:But don't you think at least the function get_host_ip is usefull for other things and you should move it into rt_utils.sh?
Yes, it should. I will move it there. There is just one little problem - in many cases, if the host is invalid, you will get a response containing the IP address of your DNS server. In cases where you are only checking for hosts that you know to exist (e.g., your ddns host), this is no problem. But for general use, where the host may be valid or invalid, perhaps some further tests might need to be added.
I've never seen this behavior in my router. I tried invalid (unresolvable) and unreachable hosts. If it is invalid/unresolvable ping shows "bad address" and returns with an error, if it is unreachable, it shows the right IP address and returns with an error. Both is expected behavior.
thechief wrote:This allows you to specify the host in an environment variable ("myddns_host"), dispensing with the need to provide a command-line argument.
That is good. But isn't it possible to retrieve it from the router's configuration area instead of defining an other environment variable? It must be already there.

MfG Stefan Fröhlich
42 ;-)
mstombs
RouterTech Team
RouterTech Team
Posts: 3753
Joined: Wed Jan 10, 2007 11:54 pm

Re: Update DDNS with RouterTech 2.94

Post by mstombs » Mon Dec 06, 2010 9:21 am

TafnaSM wrote:I've never seen this behavior in my router. I tried invalid (unresolvable) and unreachable hosts. If it is invalid/unresolvable ping shows "bad address" and returns with an error, if it is unreachable, it shows the right IP address and returns with an error. Both is expected behavior.
This may be a 'feature' of OpenDNS.
User avatar
thechief
RouterTech Team
RouterTech Team
Posts: 12067
Joined: Wed Feb 01, 2006 10:22 pm
Location: England, the Centre of Africa
Contact:

Re: Update DDNS with RouterTech 2.94

Post by thechief » Mon Dec 06, 2010 9:43 am

mstombs wrote:
TafnaSM wrote:I've never seen this behavior in my router. I tried invalid (unresolvable) and unreachable hosts. If it is invalid/unresolvable ping shows "bad address" and returns with an error, if it is unreachable, it shows the right IP address and returns with an error. Both is expected behavior.
This may be a 'feature' of OpenDNS.
It probably is. I use OpenDNS, and I get the IP of an OpenDNS server (but not any of the publicised ones)
/var # ping bloggs
PING bloggs (67.215.65.132): 56 data bytes
64 bytes from 67.215.65.132: seq=0 ttl=56 time=50.000 ms
64 bytes from 67.215.65.132: seq=1 ttl=56 time=40.000 ms
64 bytes from 67.215.65.132: seq=2 ttl=56 time=40.000 ms
64 bytes from 67.215.65.132: seq=3 ttl=56 time=40.000 ms
The Chief: :afro: Be sure to read the Firmware FAQ and do a Forum Search before posting!
No support via PM. Ask all questions on the open forum.
Post Reply