You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
3.0 KiB

  1. # /usr/lib/dynamic_dns/dynamic_dns_functions.sh
  2. #
  3. # Written by Eric Paul Bishop, Janary 2008
  4. # Distributed under the terms of the GNU General Public License (GPL) version 2.0
  5. #
  6. # This script is (loosely) based on the one posted by exobyte in the forums here:
  7. # http://forum.openwrt.org/viewtopic.php?id=14040
  8. . /lib/functions.sh
  9. . /lib/functions/network.sh
  10. #loads all options for a given package and section
  11. #also, sets all_option_variables to a list of the variable names
  12. load_all_config_options()
  13. {
  14. pkg_name="$1"
  15. section_id="$2"
  16. ALL_OPTION_VARIABLES=""
  17. # this callback loads all the variables
  18. # in the section_id section when we do
  19. # config_load. We need to redefine
  20. # the option_cb for different sections
  21. # so that the active one isn't still active
  22. # after we're done with it. For reference
  23. # the $1 variable is the name of the option
  24. # and $2 is the name of the section
  25. config_cb()
  26. {
  27. if [ ."$2" = ."$section_id" ]; then
  28. option_cb()
  29. {
  30. ALL_OPTION_VARIABLES="$ALL_OPTION_VARIABLES $1"
  31. }
  32. else
  33. option_cb() { return 0; }
  34. fi
  35. }
  36. config_load "$pkg_name"
  37. for var in $ALL_OPTION_VARIABLES
  38. do
  39. config_get "$var" "$section_id" "$var"
  40. done
  41. }
  42. get_current_ip()
  43. {
  44. #if ip source is not defined, assume we want to get ip from wan
  45. if [ "$ip_source" != "interface" ] && [ "$ip_source" != "web" ] && [ "$ip_source" != "script" ]
  46. then
  47. ip_source="network"
  48. fi
  49. if [ "$ip_source" = "network" ]
  50. then
  51. if [ -z "$ip_network" ]
  52. then
  53. ip_network="wan"
  54. fi
  55. fi
  56. current_ip='';
  57. if [ "$ip_source" = "network" ]
  58. then
  59. network_get_ipaddr current_ip "$ip_network" || return
  60. elif [ "$ip_source" = "interface" ]
  61. then
  62. current_ip=$(ifconfig $ip_interface | grep -o 'inet addr:[0-9.]*' | grep -o "$ip_regex")
  63. elif [ "$ip_source" = "script" ]
  64. then
  65. # get ip from script
  66. current_ip=$($ip_script)
  67. else
  68. # get ip from web
  69. # we check each url in order in ip_url variable, and if no ips are found we use dyndns ip checker
  70. # ip is set to FIRST expression in page that matches the ip_regex regular expression
  71. for addr in $ip_url
  72. do
  73. if [ -z "$current_ip" ]
  74. then
  75. current_ip=$(echo $( wget -O - $addr 2>/dev/null) | grep -o "$ip_regex")
  76. fi
  77. done
  78. #here we hard-code the dyndns checkip url in case no url was specified
  79. if [ -z "$current_ip" ]
  80. then
  81. current_ip=$(echo $( wget -O - http://checkip.dyndns.org 2>/dev/null) | grep -o "$ip_regex")
  82. fi
  83. fi
  84. echo "$current_ip"
  85. }
  86. verbose_echo()
  87. {
  88. if [ "$verbose_mode" = 1 ]
  89. then
  90. echo $1
  91. fi
  92. }
  93. syslog_echo()
  94. {
  95. if [ "$use_syslog" = 1 ]
  96. then
  97. echo $1|logger -t ddns-scripts-$service_id
  98. fi
  99. }
  100. start_daemon_for_all_ddns_sections()
  101. {
  102. local event_interface="$1"
  103. SECTIONS=""
  104. config_cb()
  105. {
  106. SECTIONS="$SECTIONS $2"
  107. }
  108. config_load "ddns"
  109. for section in $SECTIONS
  110. do
  111. local iface
  112. config_get iface "$section" interface "wan"
  113. [ -z "$event_interface" -o "$iface" = "$event_interface" ] || continue
  114. /usr/lib/ddns/dynamic_dns_updater.sh $section 0 > /dev/null 2>&1 &
  115. done
  116. }
  117. monotonic_time()
  118. {
  119. local uptime
  120. read uptime < /proc/uptime
  121. echo "${uptime%%.*}"
  122. }