View on
ming zhang > Angel_101 > Expect::Angel::Cisco



Annotate this POD

View/Report Bugs
Module Version: 1.01   Source  


 Expect::Angel::Cisco - Build up a robust connection to Cisco or cisco-like device


 use Expect::Angel::Cisco;
 my $dut_ip = '';
 # connects the device and goes to its enable mode
 my $dut = new Expect::Angel::Cisco(connection => "ssh $dut_ip", goto => 'enable' )
             or die "Failed to access to device\n";
 # check device's clock
 my $clock = $dut->cmdexe("show clock");

 # configure its interface eth1/1
 my @int_cmds = ("int eth1/1",
                 "ip address",
                 "no shut"
 $dut->cmdexe(\@int_cmds, "config");

 # parse its parameters on eth1/1
 my @lines = $dut->cmdexe("show int eth1/1");
 for (@lines) {
     # parsing

 # executes a list of commands at "config" mode of Cisco device
 my @cmd = ("interface e0/0",
            "ip address 255.2555.255.0",
            "no shut",
            "interface e0/1",
            "ip address 255.2555.255.0",
            "no shut"
 $dut->cmdexe(\@cmd, "config");

 # always stay at config mode for each command execution, although
 # existing some command that may change mode inadvertently.
 my @cmd = ("interface e0/0",
            "ip address 255.2555.255.0",
            "end",  # NOTE, this cause the transition to "enable" mode
            "interface e0/1",
            "ip address 255.2555.255.0",
            "no shut"
 $dut->cmdexe(\@cmd, "config");

 # check if a command is rejected by device
 if ( defined (my $clock = $dut->cmdSendCheck("show clock")) ) {
     print "system current clock is \"$clock\"\n";
     # "show clock" is rejected 

Description ^

 This module is a derivation of Expect::Angel on Cisco-like devices. It's pretty simple 
because most of the function has been implemented in Expect::Angel. It serves more like
an example of how to use Expect::Angel to apply to Cisco device.

new() ^

  Create a new Cisco object 
  connection => "command to connect to Cisco device", for example telnet 3000
  hostname => hostname_of_Cisco, it should be switch's host name. It will be used in prompt 
              pattern match. If not specified, it's ignored at prompt match, but risky.

  # user/passwd/enablePass, if not provided, will be prompted to input from keyboard
  user     => username, used to access the device.
  passwd   => password, used to access the device.
  enablePass  => password, used to access the enable mode.
  goto => enable|config, optional. Will go to that state (mode) if specified.
  see Angel's build method for all other options.

  This is a good example to show how to build a module by means of Expect::Angel.
  Different type of devices may have slight difference in their state transition.
  You can modify it accordingly to fit your specific device. The major methods are
  inherited from Expect::Angel.

enableMode() ^

  transits to enable state. It will try 3 times (defined by errTries)
  input: none
  output: true - the state is transitted to enable state
          undef - can not transit to enable state

configMode() ^

  transits to config state. It will try 3 times (defined by errTries)
  input: none
  output: true - the state is transitted to config state
          undef - can not transit to config state

cmdexe(cmd,expect,state) ^

   Inherited from Expect::Angel, refer to it for detail

cmdSendCheck($cmd,$errorPattern) ^

 This sub will exectues $cmd at current state and detect if $errorPattern 
 matches its output in the each execution. 
 If the command is rejected, it will return immediatedly.
 $cmd: single or ref to a list of commands
 $errorPattern: optional, RE of error pattern, default is '^%|Invalid command'
 return: undef when $errorPattern matches, otherwise output of the last command

bye() ^

  Close the connection decently

pingable($ip_addr) ^

 Test if target $ip_addr is reachable from the DUT by ping it.
 Input: $ip_addr - ip address of target 
 Return: number of echoback packats, all parameters are device default.
        so it will be 5 if 100% success.


Ming Zhang <>


Copyright (c) 2010, Yahoo! Inc. All rights reserved.

Artistic License 1.0

syntax highlighting: