Alpha-gray Super Contributor.
Super Contributor.
839 views

Using AAA crededentials for Change Plan with Python advanced script

Jump to solution

I am creating a Change Plan with an Advanced Python script becaue I need to build checks and balances in the script. I can initiate the SSH session using Python, but do I program the script to use the users AAA credentials set in their profile?  I found variables for $tc_user_username$ and $tc_user_password$, but this seems to be for thier login to NA credentials, not their AAA credentials. Need help on how to use user AAA creds in advanced scripts.

0 Likes
1 Solution

Accepted Solutions
Highlighted
Micro Focus Expert
Micro Focus Expert

Re: Using AAA crededentials for Change Plan with Python advanced script

Jump to solution

unfortunately not for python, only in perl.

****NOTE:

I do not own this script is just an example that is in our Databases, as a recomendation test it first on a test device in non-production environments.

 

use Opsware::NAS::Client;

use strict;

use warnings;

#===============================================================================

#

# Global variables    (Customize as needed)

#

#===============================================================================

my $host  = 'localhost';

                  #=========================================================#

                  # When running the script outside NA, put your NA         #

                  # credentials here. Replace "admin" with your username    #

                  # and "opsware" with your password.                       #

                  # When you run this script from inside NA (as diagnostic) #

                  # you don't need to touch this.                           #

                  #=========================================================#

                  # HashKey          => [ ArrElem[0] , ArrElem[1]         ] #

                  # ----------------      ----------   -----------          #

                  # Name of Variable => [ Value when , NA Variable        ] #

                  # in NA                 outside NA   replaced by NA       #

                  #=========================================================#

my %insideVar = ( 'tc_user_username' => [ 'admin',   '$tc_user_username$' ],

                  'tc_user_password' => [ 'opsware', '$tc_user_password$' ],

                  'tc_device_id'     => [ $ARGV[0],  '$tc_device_id$' ] );

#===============================================================================

#

# Local variables  (nothing customizable beyond this point)

#

#===============================================================================

#===============================================================================

#

# Functions and Subroutines

#

#===============================================================================

#===============================================================================

#

# insideNA

#

#===============================================================================

sub insideNA($)

{

my $p = shift;

my %hash = %$p;

my %result;

   foreach my $k ( keys %hash )

   {

      if( $hash{$k}[1] eq '$' . $k . '$' )

      {

         $result{$k} = $hash{$k}[0];

      }

      else

      {

         $result{$k} = $hash{$k}[1];

      }

   }

   return %result;

}

#===============================================================================

#

# getConfig( nas, deviceID )

#

#===============================================================================

sub getConfig($;$)

{

my ( $nas, $deviceID ) = @_;

my $res;

my $cfg;

my $data;

   $res = $nas->show_device( id => $deviceID );

   unless( $res->ok())

   {

      printf STDERR ("ERROR: show_device :%s\n", $res->error_message());

      $nas->logout();

      exit( 1 );

   }

   ($data) = $res->results();  

   $res = $nas->show_config( id => $data->mostRecentConfigID );

   unless( $res->ok())

   {

      printf STDERR ("ERROR: show_config :%s\n", $res->error_message());

      $nas->logout();

      exit( 1 );

   }

   ($cfg) =  $res->results();

   return $cfg;

}

#===============================================================================

#

# nasLogin( User, Password, Host )

#

#===============================================================================

sub nasLogin($;$;$)

{

my ( $user, $password, $host ) = @_;

my $res;

my $n;

   $n = Opsware::NAS::Client->new();

   $res = $n->login( -username => $user,

                     -password => $password,

                     -host => $host );

   unless ($res->ok())

   {

      printf STDERR ("ERROR: Login :%s\n", $res->error_message());

      exit( 1 );

   }

   return $n

}

#===============================================================================

#

# Main

#

#===============================================================================

my $nas;

my %h;

my $cfg;

my $res;

#=============================================

# Populate variables from values passed by NA,

# or default values if running outside NA

#=============================================

%h = insideNA( \%insideVar );

if( not defined( $h{'tc_device_id'} ) )

{

   printf STDERR ("usage: ShowNTPServerIPs.pl [deviceID]\n" );

   exit( 1 );

}

$nas = nasLogin( $h{'tc_user_username'}, $h{'tc_user_password'}, $host );

$cfg = getConfig( $nas,  $h{'tc_device_id'} );

foreach my $i ( $cfg =~ m/ntp server (.*)/g )

{

   printf "Device (%d) has NTP Server %s\n", $h{'tc_device_id'}, $i

}

#===============================================================================

# Logout and exit

#===============================================================================

$nas->logout();

exit( 0 );

Pedro A. Batista
Customer Support Engineer

If you find that this or any other post resolves your issue, please be sure to mark it as an accepted solution.
If you are satisfied with anyone’s response please remember to give them a KUDOS by clicking on the THUMB at the bottom left of the post and show your appreciation.
Tags (3)
0 Likes
6 Replies
Micro Focus Expert
Micro Focus Expert

Re: Using AAA crededentials for Change Plan with Python advanced script

Jump to solution

Hi @Alpha-gray,

I recommend you to access the proxy and use the connect option, in that way NA will use the AAA credentials tied to the accounts.

Pedro A. Batista
Customer Support Engineer

If you find that this or any other post resolves your issue, please be sure to mark it as an accepted solution.
If you are satisfied with anyone’s response please remember to give them a KUDOS by clicking on the THUMB at the bottom left of the post and show your appreciation.
0 Likes
Alpha-gray Super Contributor.
Super Contributor.

Re: Using AAA crededentials for Change Plan with Python advanced script

Jump to solution

Thank Pedro, I did not know that was an option, do you know any sample code that I can use as an example as Python?

0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

Re: Using AAA crededentials for Change Plan with Python advanced script

Jump to solution

unfortunately not for python, only in perl.

****NOTE:

I do not own this script is just an example that is in our Databases, as a recomendation test it first on a test device in non-production environments.

 

use Opsware::NAS::Client;

use strict;

use warnings;

#===============================================================================

#

# Global variables    (Customize as needed)

#

#===============================================================================

my $host  = 'localhost';

                  #=========================================================#

                  # When running the script outside NA, put your NA         #

                  # credentials here. Replace "admin" with your username    #

                  # and "opsware" with your password.                       #

                  # When you run this script from inside NA (as diagnostic) #

                  # you don't need to touch this.                           #

                  #=========================================================#

                  # HashKey          => [ ArrElem[0] , ArrElem[1]         ] #

                  # ----------------      ----------   -----------          #

                  # Name of Variable => [ Value when , NA Variable        ] #

                  # in NA                 outside NA   replaced by NA       #

                  #=========================================================#

my %insideVar = ( 'tc_user_username' => [ 'admin',   '$tc_user_username$' ],

                  'tc_user_password' => [ 'opsware', '$tc_user_password$' ],

                  'tc_device_id'     => [ $ARGV[0],  '$tc_device_id$' ] );

#===============================================================================

#

# Local variables  (nothing customizable beyond this point)

#

#===============================================================================

#===============================================================================

#

# Functions and Subroutines

#

#===============================================================================

#===============================================================================

#

# insideNA

#

#===============================================================================

sub insideNA($)

{

my $p = shift;

my %hash = %$p;

my %result;

   foreach my $k ( keys %hash )

   {

      if( $hash{$k}[1] eq '$' . $k . '$' )

      {

         $result{$k} = $hash{$k}[0];

      }

      else

      {

         $result{$k} = $hash{$k}[1];

      }

   }

   return %result;

}

#===============================================================================

#

# getConfig( nas, deviceID )

#

#===============================================================================

sub getConfig($;$)

{

my ( $nas, $deviceID ) = @_;

my $res;

my $cfg;

my $data;

   $res = $nas->show_device( id => $deviceID );

   unless( $res->ok())

   {

      printf STDERR ("ERROR: show_device :%s\n", $res->error_message());

      $nas->logout();

      exit( 1 );

   }

   ($data) = $res->results();  

   $res = $nas->show_config( id => $data->mostRecentConfigID );

   unless( $res->ok())

   {

      printf STDERR ("ERROR: show_config :%s\n", $res->error_message());

      $nas->logout();

      exit( 1 );

   }

   ($cfg) =  $res->results();

   return $cfg;

}

#===============================================================================

#

# nasLogin( User, Password, Host )

#

#===============================================================================

sub nasLogin($;$;$)

{

my ( $user, $password, $host ) = @_;

my $res;

my $n;

   $n = Opsware::NAS::Client->new();

   $res = $n->login( -username => $user,

                     -password => $password,

                     -host => $host );

   unless ($res->ok())

   {

      printf STDERR ("ERROR: Login :%s\n", $res->error_message());

      exit( 1 );

   }

   return $n

}

#===============================================================================

#

# Main

#

#===============================================================================

my $nas;

my %h;

my $cfg;

my $res;

#=============================================

# Populate variables from values passed by NA,

# or default values if running outside NA

#=============================================

%h = insideNA( \%insideVar );

if( not defined( $h{'tc_device_id'} ) )

{

   printf STDERR ("usage: ShowNTPServerIPs.pl [deviceID]\n" );

   exit( 1 );

}

$nas = nasLogin( $h{'tc_user_username'}, $h{'tc_user_password'}, $host );

$cfg = getConfig( $nas,  $h{'tc_device_id'} );

foreach my $i ( $cfg =~ m/ntp server (.*)/g )

{

   printf "Device (%d) has NTP Server %s\n", $h{'tc_device_id'}, $i

}

#===============================================================================

# Logout and exit

#===============================================================================

$nas->logout();

exit( 0 );

Pedro A. Batista
Customer Support Engineer

If you find that this or any other post resolves your issue, please be sure to mark it as an accepted solution.
If you are satisfied with anyone’s response please remember to give them a KUDOS by clicking on the THUMB at the bottom left of the post and show your appreciation.
Tags (3)
0 Likes
Alpha-gray Super Contributor.
Super Contributor.

Re: Using AAA crededentials for Change Plan with Python advanced script

Jump to solution

Ok, thank, I will research a pyton way, but a Perl base gives me a good starting point, accepting this solution.

0 Likes
Chris_Powers Acclaimed Contributor.
Acclaimed Contributor.

Re: Using AAA crededentials for Change Plan with Python advanced script

Jump to solution

Alpha-Gray - I've attached a Python script however, not for a Change Script but between what Pedro provided and this, might be able to come up with something good.  Thias uses Client, as Pedro suggested, you'd want Connect...

And don't forget to add Python as a avaialble scripting language in NA (Admin / Admin Tools / Server / Advanced Scripting), though you may have already done this step...

Good luck,

Chris

0 Likes
Alpha-gray Super Contributor.
Super Contributor.

Re: Using AAA crededentials for Change Plan with Python advanced script

Jump to solution

Thanks Chris for another example, helpful. Yes, already added Python as Advanced Scripting option, I see this in my drop-down for Change Plan Advanced Script.

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.