Product
Search Results

How can I automatically restart my router each time I run a test?

Overview

Starting with CDRouter 13.21, the easiest way to do this is to use the cdrouter-power command which supports controlling the Synaccess NP-02B or Synaccess NP-05B power strips via a USB cable plugged directly into the CDRouter system. See below for examples of how to use the cdrouter-power command.

Other power strips are available which can be remotely controlled via telnet or HTTP. There are several commercially available products that provide this functionality. We recommend the NP-0801DT or NP-1601DT models, also available from Synaccess Networks. These Synaccess units can be controlled via telnet or through a URL. Note that these models are not supported by the cdrouter-power command. Examples of both are shown below.

The RestartDut and RestartDutDelay testvars in the CDRouter configuration file control the automatic reboot functionality of CDRouter. The RestartDut testvar allows you to enter a command or script that can be executed to power cycle the router under test. If this testvar is commented out, CDRouter 12.12 and earlier releases will prompt the user to restart the router under test manually. CDRouter 12.13 and newer releases no longer prompt the user restart the router. Once power cycled, the router under test will also require a certain amount of time to reboot before it is fully operational. The RestartDutDelay testvar is designed to pause CDRouter for a number of seconds until the router under test is fully booted and operational. The appropriate value of RestartDutDelay is device dependent and must be determined for each individual router. The value of RestartDutDelay should be greater than (power cycle time + reboot time).

Note that multiple scripts or commands can be executed with the RestartDut testvar by separating each command with a semi-colon. Please see this Knowledge Base article for more information.

Controlling a Synaccess NP-02B or NP-05B unit via the cdrouter-power command

Starting with CDRouter 13.21, CDRouter includes a cdrouter-power command which supports controlling the Synaccess NP-02B or Synaccess NP-05B power strips via a USB cable plugged directly into the CDRouter system and which can be used in conjunction with the RestartDut and RestartDutDelay testvars to reboot the router under test.

Before configuring the RestartDut and RestartDutDelay testvars, first ensure the power strip itself is powered on. Next, connect a USB cable from the port on the power strip labeled “USB” to a free USB port on the CDRouter system. Finally, connect the power cable for the router under test to a free numbered outlet on the power strip and and note the number of the chosen outlet. Outlets are numbered starting from 1, with outlets on the NP-02B unit being numbered from 1 to 2 and the outlets on the NP-05B unit being numbered from 1 to 5.

Once this physical setup is finished, use the following values for the RestartDut and RestartDutDelay testvars to reboot the router under test, replacing <outlet> with the outlet number noted above:

testvar RestartDut "/usr/cdrouter/bin/cdrouter-power -reboot <outlet>"
testvar RestartDutDelay 20

For example, if the router under test is connected to outlet 2 on the power strip, configure RestartDut and RestartDutDelay as follows:

testvar RestartDut "/usr/cdrouter/bin/cdrouter-power -reboot 2"
testvar RestartDutDelay 20

The cdrouter-power command supports other operations besides rebooting an outlet. See below for the full list of options available with the cdrouter-power command:

# /usr/cdrouter/bin/cdrouter-power -h
Usage of /usr/cdrouter/bin/cdrouter-power:
  -debug
    	enable debug output on stderr
  -device DEVICE
    	control outlets of power booter DEVICE, i.e. /dev/ttyACM1
  -off OUTLET
    	turn off outlet OUTLET and exit.  If OUTLET is 'all', turn off all outlets
  -on OUTLET
    	turn on outlet OUTLET and exit.  If OUTLET is 'all', turn on all outlets
  -reboot OUTLET
    	reboot outlet OUTLET and exit.  If OUTLET is 'all', reboot all outlets
  -timeout duration
    	timeout period to wait for other cdrouter-power processes to finish before aborting (default 1m0s)
  -toggle OUTLET
    	toggle outlet OUTLET and exit.  If OUTLET is 'all', toggle all outlets

Controlling the Synaccess unit via telnet expect script

First, make sure that telnet is enabled in System page of the Synaccess web configuration tool.

There are multiple ways of automating the telnet session. One way is to use the expect utility. Here you will find a sample expect TCL script that will power cycle an outlet on a Synaccess NT-0801DT unit. The script would be the same for an NT-1601DT but would allow up to 16 outlets.

Sample powerCycle.tcl script for controlling a Synaccess unit.

# powerCycle.tcl script for Synaccess NP-08
set address [ lindex $argv 0 ]
set outlet [ lindex $argv 1 ]

if { $outlet <= 0 || $outlet > 8 } {
   puts "powerCycle.tcl failure: selected power outlet index was $outlet; must be between 1 and 8."
   exit
}

spawn -console telnet $address
expect {
   "Make sure to set Telnet mode to Local Echo Off" {
      send "rb $outlet\r"
      expect "Outlet-$outlet rebooted."
      send "logout\r"
      send "\r"
      puts "powerCycle.tcl success: outlet $outlet on host $address successfully power cycled."
      exit
   }
   "Unable to connect to remote host" {
      puts "powerCycle.tcl failure: unable to connect to the remote host."            exit
   }
   timeout {
      puts "powerCycle.tcl failure: connection timed out."  
      exit 
   }
}

This script can also be downloaded here. The powerCycle.tcl script has two arguments - the IP address of the Synaccess unit and the number of the outlet to power cycle.

Note: Expect must be installed prior to using this script.

Using the powerCycle.tcl script provided above, CDRouter can be easily configured to control the Synaccess unit and automatically reboot the router under test prior to each test run. To do this, first create or copy powerCycle.tcl to a working directory on the CDRouter host machine. Be sure that the powerCycle.tcl is executable:

chmod 755 powerCycle.tcl

Assuming the Synaccess unit is configured with an IP address of 10.0.0.200, the router under test is plugged into outlet 1, and the path of the powerCycle.tcl script is /usr/cdrouter-data/custom/qateam, the CDRouter configuration file would be modified as follows:

testvar RestartDut "expect /usr/cdrouter-data/custom/qateam/powerCycle.tcl 10.0.0.200 1"
testvar RestartDutDelay 20

Note: An appropriate value for RestartDutDelay testvar must be determined for each router (this value is device dependent). A value of 20 seconds should be appropriate for most routers.

Controlling the Synaccess unit via telnet Python script

As mentioned above, make sure that telnet is enabled in System page of the Synaccess web configuration tool.

Any script that can fake a telnet session could be used to control the ports of multiple Synaccess units. An Python example can be found here. This script should be executable and located on the CDRouter system. Once set up, you can define config files as follows:

testvar RestartDut                    "/path/to/power.py 10.0.0.22 3 reboot"
testvar RestartDutDelay               30
testvar ShutdownDut                   "/path/to/power.py 10.0.0.22 3 off"

In this example, 10.0.0.22 is the IP address of the Synaccess unit. The “3” refers to the port on the Synaccess you want to control. Finally, the last part is the command for that port. Options are “on”, “off”, “reboot”, and “state”. Of course, you can modify the power.py script to behave however you want.

Controlling the Synaccess unit via wget.

Crafting a URL to send to the Synaccess units will allow you to toggle the state of an outlet. The format of the URL differs slightly depending on whether you have an older or newer unit.

Older style Synaccess units.

Assuming the Synaccess is configured with an IP address of 192.168.1.16 and the router under test is plugged into outlet 12 on the NP-16, the CDRouter configuration file would be modified as follows:

testvar RestartDut "wget --http-user=admin --http-password=admin http://192.168.1.16/pwrSw12.cgi"
testvar RestartDutDelay 20

Newer style Synaccess units.

Newer versions of the Synaccess have a slightly different URL for modifying the state of an outlet. To toggle outlet 1 on or off use this command:

testvar RestartDut "wget --http-user=admin --http-password=admin http://192.168.1.16/cmd.cgi?rly=0"
testvar RestartDutDelay 20

To reboot outlet 1, use this command:

testvar RestartDut "wget --http-user=admin --http-password=admin http://192.168.1.16/cmd.cgi?rb=0"
testvar RestartDutDelay 20

Note that for newer style NP-16 devices the outlet numbers are indexed from 0 when controlling them via URL. Outlet 1 is controlled using the rly=0 or rb=0 arguments in the commands above, respectively.

Note: For all examples above, an appropriate value for the RestartDutDelay testvar must be determined for each router (this value is device dependent). A value of 20 seconds should be appropriate for most routers.