DX NetOps Manager

 View Only

Cisco WLC NCM Capture Script 

Aug 16, 2016 07:37 AM

Based on the Out of Box Cisco IOS SSH Capture Script.

 

 

#!/usr/Spectrum/bin/perl -w
# This script will capture the running configuration of a
# WLC device through an SSH session and print it to STDOUT.
#
# Error Codes:
# 0 = Success
# 255 = Usage error
# 254 = Invalid timeout value
# 252 = Login error
# 249 = Exec prompt not found error
# 244 = Error retrieving configuration
# 245 = Insufficient privileges
# 253 = Unexpected output
#
use strict;
use warnings;
use Net::SSH::Expect;
$ENV{'PATH'} = "/usr/bin:" . $ENV{'PATH'};
### Main ###
if ( $#ARGV != 4 && $#ARGV != 5 ) {
  print "Usage: wlc_capture.pl <device IP> <user> <pass> <enable_pass> <login_timeout_in_seconds> <capture_timeout_in_seconds>\n";
  print STDERR "Usage: wlc_capture.pl <deviceIP> <user> <pass> <enable_pass> <login_timeout_in_seconds> <capture_timeout_in_seconds>\n";
  exit 255;
} elsif ( $ARGV[4] < 1 || $ARGV[4] > 600 ) {
  print "$ARGV[4] is the login timeout and must be an int between 1 and 600 seconds\n";
  print STDERR "$ARGV[4] is the login timeout and must be an int between 1 and 600 seconds\n";
  exit 254;
} elsif ( $#ARGV == 5 && ( $ARGV[5] < 1 || $ARGV[5] > 600 ) ) {
  print "$ARGV[5] is the capture timeout and must be an int between 1 and 600 seconds\n";
  print STDERR "$ARGV[5] is the capture timeout and must be an int between 1and 600 seconds\n";
  exit 254;
} else {
  my $capture_timeout = $ARGV[4];
  if ( $ARGV[5] ) {
    $capture_timeout = $ARGV[5];
  }
  my $errorCode = 1;
  my @data;
  my $errorString = "\nHost $ARGV[0]: \n";
  ( $errorCode, @data ) = GetConfig( $ARGV[0], $ARGV[1], $ARGV[2], $ARGV[3], $ARGV[4], $capture_timeout );
  if ( $errorCode == 0 ) {

    # Success. The startup configuration
    # content is in the data variable
    foreach (@data) { print "$_\n" };    # print the configuration to STDOUT
    exit 0;
  } else {
    print STDERR $errorString;
    if ( $errorCode == 245 ) {
      print STDERR join " ", @data, "\nEnsure that the device user has sufficient privileges to disable paging and view the config\n";
    } else {
      print STDERR join " ", @data, "\n";
    }
    exit $errorCode;
  }
}
exit 0;

sub GetConfig {
  my $deviceIP        = shift;
  my $user            = shift;
  my $pass            = shift;
  my $epass           = shift;
  my $login_timeout   = shift;
  my $capture_timeout = shift;
  my @config;
  my $msg;
  my $ssh = Net::SSH::Expect->new(
    host        => $deviceIP,
    user        => $user,
    password    => $pass,
    raw_pty     => 1,
    no_terminal => 0,
    timeout     => $login_timeout
  );
  my $login_output;
  eval { $login_output = $ssh->login( 'ser:', 'assword:' ); };

  if ($@) {
    $msg = "Login has failed. Output: $login_output";
    return ( 252, $msg );
  }

  # check if the login and password is ok
  if ( $login_output =~ /Permission denied/ ) {
    $msg = "Login has failed. Permission with credentials supplied. Please check the device.\nThe username in use is $user";
    $ssh->close();
    return ( 252, $msg );
  }

  # login output should contain the right prompt characters
  if ( $login_output !~ /( >| #)$/ ) {
    $msg = "Login has failed. Didn't see device prompt as expected.";
    return ( 252, $msg );
    $ssh->close();
  }
  if ( $login_output !~ / >$/ ) {
    $msg = "Exec prompt not found.";
    $ssh->close();
    return ( 249, $msg );
  }
  my $paging = $ssh->exec( 'config paging disable', 3 );
  if ( $paging =~ /\s?%\s/ ) {
    $msg = "Unable to set terminal size to 0 - Insufficient privileges";
    $ssh->close();
    return ( 245, $msg );
  }
  $ssh->send( "show run-config commands", 10 );
  $ssh->timeout($capture_timeout);
  $ssh->peek(0);
  while ( my $line = $ssh->read_line() ) {

    # get configuration content
    push @config, $line;
  }

  #discard the first line, which is a command
  shift @config;
  if ( @config <= 0 ) {
    $msg = "No data retrieved, the capture timeout may be too low.";
    $ssh->close();
    return ( 244, $msg );
  }
  if ( scalar grep { $_ =~ /^%/ } @config ) {

    # Ensure show start actually returned the config and not an error
    # message containing '%'
    return ( 245, @config );
  }
  return ( 0, @config );    # everything was okay, return the captured data
}

Statistics
0 Favorited
25 Views
0 Files
0 Shares
0 Downloads

Tags and Keywords

Comments

Apr 09, 2021 04:37 AM

Hi, I implemented this script 2 different environments we manage. On both environments all WLC device config captures halts at a line with  "cts sxp disable".   This on both environments.  Anyone else that sees this behavior? Btw, It is not only with this scipt but with other alike scripts as well.

Sep 29, 2017 03:41 AM

You also have the option to export defined scripts in the system. Under Configuration Management, the section "Configuration Script Import/Export" allows you to select the scripts that you want to export from the system. 

Sep 28, 2017 03:38 PM

The out of the box scripts are kept within the database itself.

There are no flat files containing the scripts.

You can view the scripts we ship by "setting" them on a device or device family and copying from there.

 

Thanks,

Matt

Sep 28, 2017 02:50 PM

Any idea where the out of the box scripts are kept?

May 11, 2017 02:40 AM

Hi Renato,

That's perfect, thanks a lot !!!

May 10, 2017 01:31 PM

Odd. The code was ok last time I published.

Anyway, it is fixed now.

May 10, 2017 03:35 AM

Good morning,

This script seems very nice but it appears as a "single line". Could you please share it in raw text format.. or perhaps there is a tip to get it directly from this web page :-)

Thanks.

Related Entries and Links

No Related Resource entered.