### Local Variables: ***
### mode:perl ***
### comment-column:0 ***
### comment-start: "### " ***
### comment-end: "***" ***
### End: ***
#
# ****************DO NOT MOVE OR CHANGE LINES ABOVE THIS*********************
#
# The first set of lines runs perl from any shell. The second set of lines
# identifies the rest of the file as PERL for EMACS autoformatting.
# See end of copyright for more information.
#
# 
# -------------------------------------------------------------------
#                                   X-BONE
#
#                          http://www.isi.edu/xbone
#                USC Information Sciences Institute (USC/ISI)
#                   Marina del Rey, California 90292, USA
#                          Copyright (c) 1998-2005
# 
# -------------------------------------------------------------------
#
# Copyright (c) 1998-2005 by the University of Southern California.
# All rights reserved.
#
# Permission to use, copy, modify, and distribute this software and
# its documentation in source and binary forms for non-commercial
# purposes and without fee is hereby granted, provided that the above
# copyright notice appear in all copies and that both the copyright
# notice and this permission notice appear in supporting
# documentation, and that any documentation, advertising materials,
# and other materials related to such distribution and use acknowledge
# that the software was developed by the University of Southern
# California, Information Sciences Institute.  The name of the
# University may not be used to endorse or promote products derived
# from this software without specific prior written permission.
# 
# THE UNIVERSITY OF SOUTHERN CALIFORNIA MAKES NO REPRESENTATIONS ABOUT
# THE SUITABILITY OF THIS SOFTWARE FOR ANY PURPOSE.  THIS SOFTWARE IS
# PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
# INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#
# Other copyrights might apply to parts of this software and are so
# noted when applicable.
#
# -------------------------------------------------------------------
#
# Effort partly sponsored by the Defense Advanced Research Projects
# Agency (DARPA) and Air Force Research Laboratory, Air Force Materiel
# Command, USAF, under agreement numbers F30602-98-1-0200 (X-Bone) and
# F30602-01-2-0529 (DynaBone). The views and conclusions contained
# herein are those of the authors and should not be interpreted as
# necessarily representing the official policies or endorsements,
# either expressed or implied, of the Defense Advanced Research
# Projects Agency (DARPA), the Air Force Research Laboratory, or the
# U.S. Government.
#
# This work was partly supported by the NSF STI-XTEND (ANI-0230789)
# and NETFS (ANI-0129689) projects. Any opinions, findings, and
# conclusions or recommendations expressed in this material are those
# of the authors and do not necessarily reflect the views of the
# National Science Foundation.
#
# ----------------------------------------------------------------------------
#  $RCSfile: XB_CiscoSSH.pm,v $
#
# $Revision: 1.9 $
#   $Author: pingali $
#     $Date: 2005/03/31 07:03:54 $
#    $State: Exp $
# ----------------------------------------------------------------------------
#
# Primary Author: Mohit Pande

package XB_CiscoSSH;

require Exporter;
@ISA = qw(Exporter);
@Export = qw();
@EXPORT_OK = qw(cmd show_cmd);

use strict;
use sigtrap;
 
use XB_Cisco;
use XB_Params;
use XB_Log;

#######################################################################
# EXPORTED API
#######################################################################


# Description: 
#	Issues the configuration command(s) to the router after logging
#       to the "enable" (exec) mode followed by "config terminal" mode
# Arguments:
#       @cmd  the configuration command(s) to be issued to the router 
# Returns:
#       the output of the cmd returned by the router
# Exceptions:
#       "XB_CiscoSSH::cmd" on error, nothing to clean up by caller
#
sub cmd {
  my $router = $XB_Params::node_opts{app_addr};
  my $username = $XB_Params::node_opts{cisco_buddy_username};
  my $password = $XB_Params::node_opts{cisco_buddy_password};
  my $enable_password = $XB_Params::node_opts{cisco_buddy_enable_password};
  my @cmd = @_;
  my @output;
  # print trace line
  XB_Log::log "info", "-> XB_CiscoSSH::cmd @cmd";
  eval {
    @cmd = 'config t' . "\n" . $cmd[0];
    my $SSH = Cisco->new(host => $router, username => $username, 
     password => $password, enable_password => $enable_password, timeout => 20);
    @output = $SSH->new_cmd(@cmd);
  };
  # print trace line
  XB_Log::log "info", "<- XB_CiscoSSH::cmd @cmd";
  return @output unless $@;  #success if no exception
   
  #exception handling
  if ($@) {
    # unknown exception caught, log and pass up a defined one   
    XB_Log::log "warning", "XB_CiscoSSH::cmd: caught unexpected exception $@";
  }
  # pass defined exceptions up to caller
  die "XB_CiscoSSH::cmd";
}

# Description: 
#	Issues the show command to the router after logging
#       to the "enable" (exec) mode 
# Arguments:
#    	@cmd  the show command to be issued to the router 
# Returns:
#    	the output of the cmd returned by the router
# Exceptions:
#       "XB_CiscoSSH::show_cmd" on error, nothing to clean up by caller
sub show_cmd {
  my $router = $XB_Params::node_opts{app_addr};
  my $username = $XB_Params::node_opts{cisco_buddy_username};
  my $password = $XB_Params::node_opts{cisco_buddy_password};
  my $enable_password = $XB_Params::node_opts{cisco_buddy_enable_password};
  my @output;
  my @cmd = @_;
  # print trace line
  XB_Log::log "info", "-> XB_CiscoSSH::show_cmd @cmd";
  XB_Log::log "debug", "cisco router = $router, username = $username, 
    password= $password, enable_password = $enable_password";
  eval {
    my $SSH = Cisco->new(host => $router, username => $username, 
      password => $password, enable_password  => $enable_password, 
      timeout => 20);
    @output = $SSH->new_cmd (@cmd);
  };
  #print trace line
  XB_Log::log "info", "<- XB_CiscoSSH::show_cmd @cmd";
  return @output unless $@;  #success if no exception
  
  #exception handling
  if ($@) {  
    XB_Log::log "warning", "XB_CiscoSSH::show_cmd: caught unexpected".
      " exception $@";
  }
  # pass defined exceptions up to caller
  die "XB_CiscoSSH::show_cmd";
}           

#########################################################################
# UTILITY FUNCTIONS
#########################################################################
sub ping {
  my @cmd = @_;
  my @ping = XB_CiscoSSH::show_cmd "ping @cmd";
  my @line = split " ", $ping[$#ping];
  if ($line[3] > 0 ) {
      return 1;
  }
  else
  { return 0; }
}

1;



syntax highlighted by Code2HTML, v. 0.9.1