AmperXavi7868rControl


#!/bin/bash
#
#ident "@(#)AmperXavi7868rControl 1.0 - 2007-09-16" /* S.Seidl */
#
###############################################################################
#
# The `AMPER XAVI 7868r' is an ADSL router which was and may be still
# provided in the form of a so-called `Kit ADSL Router Inalambrico'
# by Telefonica de Espana, S.A.
#
# The script which is given below allows one to enable or disable the
# WAN interface of that router, normally named ppp-0, using wget
# from the command line. It further allows one to query
# the current router WAN interface state, i.e., to find out whether
# the latter interface is enabled or disabled.
# And, finally, it allows one to synchronize the router time via NTP.
#
# To obtain usage information the present script can be invoked
# without arguments.
#
# It has been successfully tested with the following
# AMPER XAVI 7868r system
#
#         Vendor: Conexant
#            URL: http://www.conexant.com/
#   Hardware ver: Argon 4x1 Eval BSP v1.0 / Argon 4x1 CSP v1.0 (ISOS 9.0)
#   Software ver: 1.03APF2.7868A-D.79.52.25f 1/December/2005 23:20
#     Build type: RELEASE
#       Compiler: gcc 2.95.3 20010315 (release)
#
# where the above data can be printed with the help of the telnet command
# using `system info' as input.
#
# A special feature is that the script blocks until the router has reached
# the desired state, being useful for unattended operation in batch mode.
# When called with `-a', for allow, or, in case that the WAN interface
# is enabled also with `-q', for query, the current WAN interface IP address
# is returned.
#
# Finally, to get some knowledge what happens inside the script below,
# one has to glance over the code, i.e., there is no further documentation
# available at the moment.
#
# This script was written by Stephan Seidl in 2007, no copyright is claimed.
# It is offered as-is, without any warranty.
# This script is in the public domain; do with it what you wish.
#
###############################################################################
#
unset LANG LC_ALL
LC_ALL=C
export LC_ALL
#
set -e
#
###############################################################################
#
# Evaluate the script argument list.
arg=""
if [ ${#} -eq 1 ]; then
  arg="${1}"
  shift
  fi
if [ "${arg}" != "-a" -a \
     "${arg}" != "-p" -a \
     "${arg}" != "-q" -a \
     "${arg}" != "-t" ]; then
  arg=""
  fi
if [ -z "${arg}" ]; then
  pgm=`basename ${0}`
  txt=""
  txt="${txt}Usage: ${pgm} -a (to enable the WAN interface (allow));"
  txt="${txt}       ${pgm} -p (to disable the WAN interface (prevent);"
  txt="${txt}       ${pgm} -q (to query the WAN interface state);"
  txt="${txt}       ${pgm} -t (to synchronize the router time)"
  echo "${txt}" | tr ';' '\012' 1>&2
  exit 1
  fi
#
###############################################################################
#
# The AMPER XAVI 7868r factory defaults.
AdslRouterLanAddr="192.168.1.1"
AdslRouterUser="1234"
AdslRouterPasswd="1234"
#
# Now try to read the local values of `AdslRouterLanAddr',
# `AdslRouterUser', and `AdslRouterPasswd', i.e., the
# file `/etc/SmallSystemsAccessData' consists of appropriate
# assignments to those variables.
if [ -e /etc/SmallSystemsAccessData ]; then
  if [ -r /etc/SmallSystemsAccessData ]; then
    . /etc/SmallSystemsAccessData
    fi
  fi
#
###############################################################################
#
# Create a .wgetrc.
# Pay attention, a possibly existing old file gets destroyed here.
currpwd=`pwd`
cd
home=`pwd`
cd "${currpwd}"
rm -fr ${home}/.wgetrc
touch ${home}/.wgetrc
chmod 600 ${home}/.wgetrc
trap "rm -f ${home}/.wgetrc" 1 2 3 15
builtin echo     "user = ${AdslRouterUser}"   >> ${home}/.wgetrc
builtin echo "password = ${AdslRouterPasswd}" >> ${home}/.wgetrc
#
###############################################################################
#
# Create the Wget arguments.
post=""
url=""
if [ "${arg}" = "-a" ]; then
  # Allow WAN access.
  post="${post}--post-data="
  post="${post}EmWeb_ns:vim:3=/configuration/edit-form.html&"
  post="${post}EmWeb_ns:vim:11=ImRouter.ImIpInterfaces.ppp-0&"
  post="${post}EmWeb_ns:vim:6.ImRouter.ImIpInterfaces.ppp-0:enabled=true"
  url="${url}http://${AdslRouterLanAddr}/configuration/edit-form.html/edit"
  fi
if [ "${arg}" = "-p" ]; then
  # Prevent WAN access.
  post="${post}--post-data="
  post="${post}EmWeb_ns:vim:3=/configuration/edit-form.html&"
  post="${post}EmWeb_ns:vim:11=ImRouter.ImIpInterfaces.ppp-0&"
  post="${post}EmWeb_ns:vim:6.ImRouter.ImIpInterfaces.ppp-0:enabled=false"
  url="${url}http://${AdslRouterLanAddr}/configuration/edit-form.html/edit"
  fi
if [ "${arg}" = "-q" ]; then
  # Query WAN access state.
  url="${url}http://${AdslRouterLanAddr}/configuration/edit-form.html"
  url="${url}?ImRouter.ImIpInterfaces.ppp-0"
  fi
if [ "${arg}" = "-t" ]; then
  # Synchronize time.
  post="${post}--post-data="
  post="${post}EmWeb_ns:vim:3=/configuration/sntp_client.html&"
  post="${post}EmWeb_ns:vim:6.ImSntpClient:synchronize=true"
  url="${url}http://${AdslRouterLanAddr}/configuration"
  url="${url}/sntp_client.html/sntpsync"
  fi
#
###############################################################################
#
# Predefine the return code.
rc=0
#
###############################################################################
#
# Execute the WAN access stuff.
if [ "${arg}" = "-a" -o \
     "${arg}" = "-p" -o \
     "${arg}" = "-q" ]; then
  aux="name=\\\"EmWeb_ns:vim:6.ImRouter.ImIpInterfaces.ppp-0:ipaddr\\\""
  while :; do
    # One needs Wget 1.10 or higher here.
    wanaddr=`wget -o /dev/null -O - "${post}" "${url}" \
      | sed -e 's,<, < ,g'                             \
            -e 's,>, > ,g'                             \
      | tr ' \011' '\012\012'                          \
      | awk 'BEGIN{p=0}                                \
                  {if(NF==0)next;                      \
                   if($1=="'"${aux}"'"){p=1;next;}     \
                   if(p==0)next;p=0;                   \
                   n=length($1);if(n<15)next;          \
                   if(substr($1,1,7)!="VALUE=\"")next; \
                   if(substr($1,n,1)!="\"")next;       \
                   print substr($1,8,n-8);next;}'`
    tstaddr=`echo "${wanaddr}" \
      | sed -e 's,[1-9],0,g'   \
            -e 's,00*,0,g'`
    test                    "${tstaddr}" != "0.0.0.0" && break || :
    test "${arg}" = "-a" -a "${wanaddr}" != "0.0.0.0" && break || :
    test "${arg}" = "-p" -a "${wanaddr}"  = "0.0.0.0" && break || :
    test "${arg}" = "-q"                              && break || :
    sleep 1
    done
  if [ "${tstaddr}" = "0.0.0.0" ]
    then
      if [ "${wanaddr}" != "0.0.0.0" ]
        then echo "${wanaddr} (WAN interface enabled)"
        else echo "${wanaddr} (WAN interface disabled)"
        fi
    else
      rc=1
    fi
  # Done.
  arg=""
  fi
#
###############################################################################
#
# Execute the time synchronization stuff.
if [ "${arg}" = "-t" ]; then
  # One needs Wget 1.10 or higher here.
  ack=`wget -o /dev/null -O - "${post}" "${url}" \
    | sed -e 's,<, < ,g'                         \
          -e 's,>, > ,g'                         \
    | tr '\011' ' '                              \
    | sed -n 's,^.* Current , Current ,p'        \
    | awk '{s=" " $0 " ";                        \
            i=index(s,"<");                      \
            while(i!=0){                         \
              t=substr(s,i+1);                   \
              j=index(t,">");                    \
              if(j==0){t=t "> ";j=index(t,">");} \
              s=substr(s,1,i-1) substr(t,j+1);   \
              i=index(s,"<");}                   \
            print s;}'                           \
    | sed -e 's,   *, ,g'                        \
          -e 's,^ ,,'                            \
          -e 's, $,,'                            \
    | sort`
  if [ -n "${ack}" ]
    then
      echo "${ack}"
    else
      rc=1
    fi
  # Done.
  arg=""
  fi
#
###############################################################################
#
# Remove the .wgetrc.
rm ${home}/.wgetrc
#
###############################################################################
#
# Exit.
exit ${rc}
#
###############################################################################


Stephan K.H. Seidl