##### Tested with ArubaOS version 10.11 on a CX 6100 switch in 2023, by mw

#!/usr/bin/perl
use strict;
use warnings;
use LWP::UserAgent;
use HTTP::Request;
use URI::Escape;
use Data::Dumper;

# ArubaOS-CX switch details
my $switch_ip = '192.168.2.99';
my $username = 'admin';
my $password = 'bla#bla';

# URL-encode the password
my $encoded_password = uri_escape($password);

# API endpoints
my $login_url = "https://$switch_ip/rest/v10.11/login?username=$username&password=$encoded_password";
my $system_url = "https://$switch_ip/rest/v10.11/system?attributes=hostname";
my $logout_url = "https://$switch_ip/rest/v10.11/logout";

# Create a user agent
my $ua = LWP::UserAgent->new;
$ua->ssl_opts(verify_hostname => 0, SSL_verify_mode => 0); # Disable SSL certificate verification

# Set the headers
my $headers = [
'accept' => 'application/json',
'x-use-csrf-token' => 'true',
];

# Perform login
my $login_request = HTTP::Request->new('POST', $login_url, $headers);
my $login_response = $ua->request($login_request);

#print Dumper $login_response;

# Check the login response status
if ($login_response->is_success) {
my $session_cookie = $login_response->header('Set-Cookie');
my $x_csrf_token = $login_response->header('x-csrf-token');
print "session_cookie=$session_cookie\n";
print "x_csrf_token = $x_csrf_token\n";

# Retrieve system information
my $system_request = HTTP::Request->new('GET', $system_url, $headers);
$system_request->header('Cookie' => $session_cookie, 'x-csrf-token' => $x_csrf_token, );
#$system_request->header('x-csrf-token' => $x_csrf_token, );
my $system_response = $ua->request($system_request);


# Check the system response status
if ($system_response->is_success) {
my $system_data = $system_response->decoded_content;
# Process the system data as needed
print "System Information:\n";
print $system_data;
print "\n";
} else {
print "Error retrieving system information: " . $system_response->status_line . "\n";
}

# Perform logout
my $logout_request = HTTP::Request->new('POST', $logout_url, $headers);
#$logout_request->header('Cookie' => $session_cookie);
$logout_request->header('Cookie' => $session_cookie, 'x-csrf-token' => $x_csrf_token, );
my $logout_response = $ua->request($logout_request);

# Check the logout response status
if ($logout_response->is_success) {
print "Logged out.\n";
} else {
print "Error logging out: " . $logout_response->status_line . "\n";
}
} else {
print "Error logging in: " . $login_response->status_line . "\n";
}

computer2know :: thank you for your visit :: have a nice day :: © 2024