Cisco NAC Device Filter API Example

NetCraftsmen®

The Cisco NAC device filter is used to allow computers to bypass authentication and optionally perform posture assessment.  The optional posture assessment is based on the role that is assigned to the device filter.  The MAC addresses, used in the device filter list, can be added through the NAC Manager Web GUI or through the NAC API.

The NAC API provides a way of performing NAC functions through a script.  A device filter script could be useful when adding or removing a large number of subnets.  Cisco provides details on the API functionality, but does not provide a lot of examples for configuring scripts, so I thought I’d throw out a Perl script I wrote as an example.

The Perl script I wrote provides a means of adding or removing MAC addresses.  The usage screen is shown below.

#######################################################################
Usage: mac.pl [remove | add [access type]]

- access type = allow, deny, ignore, check, or
- For example: mac.pl add allow maclist.txt
- For example: mac.pl remove maclist.txt
- File should be a CSV file containing one MAC and description per line
For example: xx:xx:xx:xx:xx:xx, description
#######################################################################

An example of running the script ot add MAC addresses to the device filter list is shown below

./mac.pl add allow maclist.txt
Enter the NAC Manager IP: 10.1.1.233
Enter the NAC Manager User ID: admin
Enter the NAC Manager password: netcraftsmen

#################################################################
0 successes
7 failures
Log files have been created with the information below.
Note: If the file existed in the current directory, it was deleted
success.log = Successful additions
failure.log = Failed additions
info.log = Informative messages
#################################################################

The Perl script is shown below

#! /usr/bin/perl -w

############################################################
#
# Modified: Aug 7, 2010
#
# This Perl script changes the MAC filter list of existing
# filter list entries to the "deny" access type. Because the NAC API
# only support add and remove, we have to remove the MAC
# and then add it back in with "deny" as the access type
#
# The script takes one parameter. This parameter is a file
# that contains a list of MAC addresses in xx:xx:xx:xx:xx:xx
# format that must exactly match the existing MAC entries in
# the device filter list. There should be only one MAC per
# line. The syntax for the script is
# perl mac.pl <file> (where <file> is the filename)
#
# There are three log files that are created. Success.log
# contains the MAC addresses of all MAC addresses that were
# successfully changed in the NAC device filter list.
# Failure.log contains the MAC addresses of all MAC addresses
# that failed to be added. Info.log contains information
# about the success and failure of each MAC address. The files
# are overwritten on each script execution, so the files should be
# saved to a different directory if they need to be kept
#
############################################################

# Define variables
$camip = "10.1.1.233";
$camuser = "admin";
$campassword = "netcraftsmen";
$successlog = "success.log";
$failurelog = "failure.log";
$infolog = "info.log";

# Use HTTP Module
use HTTP::Request::Common qw(POST GET);
use HTTP::Headers;
use LWP::UserAgent;
use MIME::Base64;

sub usage()
{
print "nn#######################################################################n";
print "Usage: mac.pl [remove | add [access type]] <filename>nn";
print " - access type = allow, deny, ignore, check, or <role>n";
print " - For example: mac.pl add allow maclist.txtn";
print " - For example: mac.pl remove maclist.txtn";
print " - File should be a CSV file containing one MAC and description per linen";
print " For example: xx:xx:xx:xx:xx:xx, descriptionn";
print "#######################################################################nn";
}

# Exit if an incorrect number of arguments are used
if ($#ARGV < 1 or $#ARGV > 2) {
usage();
exit 1;
}

$action = $ARGV[0];
if ($action =~ /add/)
{
if ($#ARGV != 2)
{
usage();
exit 1;
}
$type = $ARGV[1];
$file = $ARGV[2];
}
elsif ($action =~ /remove/)
{
if ($#ARGV != 1)
{
usage();
exit 1;
}
$type = "";
$file = $ARGV[1];
}
else
{
usage();
exit 1;
}

# Open file for reading
open(FILE, "<$file") || die "nnCan't open file $file for readingn";
# Open LogFile
open SLOG, "> $successlog";
open FLOG, "> $failurelog";
open ILOG, "> $infolog";

# Gathering information about access to the NAC Manager
print "Enter the NAC Manager IP: ";
$camip = <STDIN>;
chomp($camip);
print "Enter the NAC Manager User ID: ";
$camuser = <STDIN>;
chomp($camuser);
print "Enter the NAC Manager password: ";
$campassword = <STDIN>;
chomp($campassword);


# Setup the request parameters
$ua = LWP::UserAgent->new();
$ua->agent("CCAAgent/v4.7.2 CleanAccessManager API");
$encoded = encode_base64("$camuser/:$campassword");
$url = "https://$camip/admin/cisco_api.jsp";

# Reading each line of the file
while (<FILE>)
{
($line) = $_;
chomp($line);
($mac,$description) = split(/,/,$line,2);
#Error checking for valid MAC
if ($mac !~ /[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]/)
{
print ILOG "Invalid MAC Address $macn";
print FLOG "$macn";
next;
}

# Creating API request
if ($action =~ /remove/)
{
# Creating requests
$req = POST $url,
'Authorization' => "Basic ".$encoded,
Content => [
op => "removemac",
admin => "$camuser",
passwd => "$campassword",
mac => "$mac"
];
}

if ($action =~ /add/)
{
if ($type =~ /allow/ || $type =~ /deny/ || $type =~ /check/ || $type =~ /ignore/)
{
$req = POST $url,
'Authorization' => "Basic ".$encoded,
Content => [
op => "addmac",
admin => "$camuser",
passwd => "$campassword",
mac => "$mac",
desc => $description,
type => "$type"
];
}
else
{
$req = POST $url,
'Authorization' => "Basic ".$encoded,
Content => [
op => "addmac",
admin => "$camuser",
passwd => "$campassword",
mac => "$mac",
type => "userole",
desc => $description,
role => "$type"
];
}
}

# Executing API request
$res = $ua->request($req);
$err = $res->status_line;
# Put the results into $results
$results = $res->content;

#############
# Reporting
#############
print ILOG "Result for $mac ";
print ILOG "Webserver response=$err ";
print ILOG "API response=$results";

# CAM is reporting bad HTTP request
if ($err !~ "200 OK")
{
print FLOG "$macn";
next;
}

# CAM response from API Request
if ($results =~ /error=0/)
{
print SLOG "$macn";
}
# Looking for a non-blank line without error=0
elsif ($results =~ /[0-9a-zA-Z]+/)
{
print FLOG "$macn";
}
}
# Close logfile
close SLOG;
close FLOG;
close ILOG;

open SLOG, "< $successlog";
open FLOG, "< $failurelog";
$scount = 0;
$fcount = 0;

$scount++ while <SLOG>;
$fcount++ while <FLOG>;

# Close logfile
close SLOG;
close FLOG;


print "n#################################################################n";
print "$scount successesn";
print "$fcount failuresn";
print "Log files have been created with the information below.n";
print "Note: If the file existed in the current directory, it was deletedn";
print " success.log = Successful additionsn";
print " failure.log = Failed additionsn";
print " info.log = Informative messagesn";
print "#################################################################n";

exit 0;

Leave a Reply