Device-Cisco-NXAPI

 view release on metacpan or  search on metacpan

lib/Device/Cisco/NXAPI.pm  view on Meta::CPAN



=head1 NAME

Device::Cisco::NXAPI - Interact with the NX-API (Nexus 9K Switches)

=head1 VERSION

Version 0.03

=cut

our $VERSION = '0.03';


=head1 SYNOPSIS

This module provides methods to make API calls and extract information from devices that support the NX-API.
This is predominantly the Nexus 9K range of switches in NXOS mode (not in ACI mode).

    use Device::Cisco::NXAPI;

    my $switch_api = Device::Cisco::NXAPI->new(uri => "https://192.168.1.1:8080", username => "admin", password => "admin");

    my @route_info = $switch_api->routes(vrf => "CustVRF");
    my %version_info = $switch_api->version();

=cut

has 'user_agent'    => ( is => 'rw', isa => 'LWP::UserAgent', default => sub { LWP::UserAgent->new });
has 'http_request'  => ( is => 'rw', isa => 'HTTP::Request');
has 'uri'          => ( is => 'ro', isa => 'Str', required => 1);
has 'username'      => ( is => 'ro', isa => 'Str', required => 1);
has 'password'      => ( is => 'ro', isa => 'Str', required => 1);
has 'debug'         => ( is => 'ro', isa => 'Bool', default => 0);

=head1 CONSTRUCTOR
 
This method constructs a new C<Device::Cisco::NXAPI> object.
 
    my $switch_api = Device::Cisco::NXAPI->new(
                                    # Mandatory parameters:
                                    uri => '',                  # URI of the switch to connect to.
                                    username => '',             # Username to logon to the switch
                                    password => '',             # Password to logon to the switch

                                    # Optional Parameters
                                    debug => (0 | 1),           # Output debugging information to stderr
                                );

=cut

sub BUILD {
    my $self = shift;
    
    my $uri = URI->new($self->uri);
    croak "Only http:// or https:// supported." if !($uri->scheme eq 'http' or $uri->scheme eq 'https');

    $self->http_request(HTTP::Request->new(POST => $uri->scheme()."://".$uri->host_port()."/ins"));
    $self->http_request()->content_type("application/json-rpc");
    $self->user_agent()->credentials($uri->host_port(), 'Secure Zone', $self->username, $self->password);
}

=head1 METHODS
 
=head2 tester()

Returns a B<Device::Cisco::NXAPI::Test> object for the switch. This object can be used to run test
cases against the switch.

=cut

sub tester {
    my $self = shift;
    return Device::Cisco::NXAPI::Test->new(switch => $self);
}

=head2 version()

    my %version_info = $switch->version()

Returns a hash consisting of system information. There are no arguments to this method.

The structure returned is as follows:

    (
      'kern_uptm_secs' => 17,
      'kickstart_ver_str' => '7.0(3)I2(2b)',
      'kick_file_name' => 'bootflash:///nxos.7.0.3.I2.2b.bin',
      'rr_ctime' => ' Mon Dec 19 04:57:51 2016',
      'kern_uptm_days' => 0,
      'kick_tmstmp' => '02/29/2016 05:21:45',
      'host_name' => 'switch',
      'cpu_name' => 'Intel(R) Core(TM) i3- CPU @ 2.50GHz',
      'kern_uptm_hrs' => 0,
      'manufacturer' => 'Cisco Systems, Inc.',
      'rr_sys_ver' => '11.3(2h)',
      'mem_type' => 'kB',
      'bootflash_size' => 7906304,
      'kern_uptm_mins' => 5,
      'bios_cmpl_time' => '10/12/2015',
      'bios_ver_str' => '07.41',
      'proc_board_id' => 'SAL1911BCSU',
      'kick_cmpl_time' => ' 2/28/2016 21:00:00',
      'header_str' => 'Cisco Nexus Operating System (NX-OS) Software',
      'rr_reason' => 'Reset Requested by CLI command reload',
      'memory' => 16401952,
      'chassis_id' => 'Nexus9000 C9372PX chassis',
      'rr_usecs' => 832622,
      'rr_service' => 'PolicyElem Ch reload'
    );

=cut

sub version {
    my $self = shift;

    my $ret = $self->_send_cmd("show version");
    _fixup_returned_structure($ret);
    return %{ $ret };
}



( run in 0.882 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )