Disaster Recovery for Cisco NAC L3 OOB with ACLs using NCM

NetCraftsmen®

#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
use Opsware::NAS::Connect;

my($host,$port,$user,$pass) = (‘localhost’,’$tc_proxy_telnet_port$’,’$tc_user_username$’,’$tc_user_password$’);
my $device = ‘#$tc_device_id$’;
my @output;
my @data;
my @auth_ports = ();
my $foundauth_flag = 0;
my $foundaccess_flag = 0;
my $done_flag = 0;
my $access_vlan;
my $auth_vlan;

my $con = Opsware::NAS::Connect->new(-user => $user, -pass => $pass, -host => $host, -port => $port);

$con->login();
$con->connect( $device ) or die “Failed to connect.”;

$con->cmd(“terminal length 0”);

# Looking for the authentication VLAN ID associated with # the VLAN name and the ports assigned to the authentication # VLAN
@output = $con->cmd(“show vlan name authentication”);

# Running through the output for each line of the results
foreach (@output) {
  # Printing the line of the command for display
  print “$_ “;
  # Splitting the line into an array based on white space   # as delimiters
  @data = split(/s+/);
  # Making sure there is data on the line, the line starts
  # with a number and has “authentication” as the second
  # field.  First field is the VLAN ID   if ($#data > 1 && $data[0] =~ /d+/ && $data[1] =~ /authentication/) {
    $auth_vlan = $data[0];
    $foundauth_flag = 1;
  }
  # Gathering switch ports in the VLAN
  if ($foundauth_flag == 1) {
    foreach (@data) {
      if ($data[0] =~ /VLAN/) {
        $done_flag = 1;
        last;
      }
      elsif (/Fa/ or /Gi/) {
        if (/,/) {
          chop;
        }
        $auth_ports[++$#auth_ports] = $_;
      }
    }    }
  if ($done_flag == 1) {
    last;
  }
}
# Gathering the VLAN ID for the access VLAN
@output = $con->cmd(“show vlan name staff1a”);
foreach (@output) {
  @data = split (/s+/);
  if ($#data > 2) {
    if ($data[0] =~ /d+/ && $data[1] =~ /staff1a/) {
      $access_vlan = $data[0];
      $foundaccess_flag = 1;
      last;
    }
  }
}

# Error Checking before assigning switch ports
if ($foundaccess_flag != 1) {
  print “Error:  Access VLAN not found “;
  exit 1;
}
if ($foundauth_flag != 1) {
  print “Error:  Authentication VLAN not found “;
  exit 1;
}

# Entering “configuration terminal” mode
@output = $con->cmd(“config t”);

# Displaying message saying that switchports are being # changed
print ” ################################################# “;
print “All ports in the authentication VLAN are being moved “;
print “to the access VLAN “;
print ” ################################################# “;
# Assigning the authentication VLAN ports to the access
# VLAN foreach (@auth_ports) {
  @output = $con->cmd(“int $_”);
  print join(” “, @output,” “);
  @output = $con->cmd(“switchport access vlan $access_vlan”);
  print join(” “, @output,” “);  }

@output = $con->cmd(“end”);
print join(” “, @output);

 
@output = $con->disconnect();

$con->logout();
undef $con;
exit(0);
 
 

Leave a Reply