Network Change & Configuration Management

Reply
Highlighted

Perl API - Passing script variables from one script to another

AndrewSJ
Techie
Posts: 14
3804     0

I'm writing a Perl script that will run a second script. Both scripts appear on the Scripts tab in the NetMRI GUI. I'm having trouble passing script variables to the second script. The second script runs and it runs against the proper devices but the script variables I try to pass it are not populated.

 

Here is the API call I'm using to run the second script.

 

# Run the script and wait for it's exit status.
$response = $script_broker->run({ name => $script_name,
                                  device_group_ids => @device_group_list,
                                  device_ids => @device_list,
                                  '$passwd' => $password,
                                  '$enable_changes' => $enable_changes });

 

Like I said, the second script specified by $script_name does run and it does run against the devices listed in @device_group_list and @device_list. The second script has script variables called $passwd and $enable_changes which I'm trying to pass through using the above API function. I'm hoping I just don't have the syntax right. The documentation isn't especially clear on how passing script variables works.

Re: Perl API - Passing script variables from one script to another

Adviser
Posts: 410
3805     0

Andrew try this, This script establishes multiple sessions with the given devices and executes the commands.

 

# BEGIN-SCRIPT-BLOCK
#
# Script-Filter:
# $Vendor eq "Cisco" and $sysDescr like /IOS/
#
# Script-Variables:
#
##
# END-SCRIPT-BLOCK

use NetMRI::API::Client;

our $_client = new NetMRI::API::Client(
UserName => "$http_username", # Well known variable
Password => "$http_password", # Well known variable
URL => "$api_url" # Well known variable
);

our $_dis = $_client->get_broker("DisSession");
our $_cli = $_client->get_broker("CliConnection");
our $_session_id = 0;

# Map Common device_id variable to another variable
my $first_device = $device_id;
my $cli_command = "sh run | inc hostname";


open_session();
open_connection($first_device);

# Send a show ver command to the job selected device
#my $output1 = send_command($first_device, $cli_command);

# Find the device ID for the second device
my $device_broker = $_client->get_broker("Device");
my @devices = $device_broker->find(DeviceIPDotted => '10.66.22.251');
my $device = shift @devices;
unless ($device) {
print "Unable to find device ID\n";
}


my $second_device = $device->{DeviceID};
print "Second device is = $second_device\n";
foreach my $output1 (split /\r?\n/,send_command($first_device, $cli_command))
{
print "First device ($first_device) is output: \n$output1\n";

#$easy->log_message('info', $output1);
if ($output1 =~ m/hostname (.*)/) {
#$easy->log_message('info', $output1);
$int = $1;
print "Hostname : $int\n";
my $cli_command1 = "config t\rsnmp-server contact $int";
#$easy->log_message('info', , "Hostname : $int");
# Open a new connection to device 2 and send a command
open_connection($second_device);
my $output2 = send_command($second_device, $cli_command1);

}
};

 


print "Second device ($second_device) output is:\n $output2\n";



### SUBROUTINES ###
sub open_session {
our $_dis_response = $_dis->open(
job_id => $job_id # Well known variable
);

$_session_id = $_dis_response->{dis_session}->{SessionID};

END {
close_session();
}
}

sub close_session {
our $_dis_response = $_dis->close(
id => $_session_id,
);
}

sub open_connection {
my $devID = shift;
print "++++ Opening session to device $devID\n";
our $_cli_response = $_cli->open(
id => $_session_id,
device_id => $devID
);
print "DEBUG: _cli_reponse: $_cli_response\n";
END {
close_connection($devID);
}
}

sub close_connection {
my $devID = shift;
our $_cli_response = $_cli->close(
id => $_session_id,
device_id => $devID # Well known variable
);
}

sub send_command {
my $devID = shift;
my ($command, $debug) = @_;

if($debug eq "") {
$debug = 0;
}
print "DEBUG: Device ID is: $devID\n";
$_cli_response = $_cli->send_command(
id => $_session_id,
device_id => $devID, # Well known variable
command => $command,
debug => $debug
);

return($_cli_response->{command_response});
}

Follow me on LinkedIn: https://www.linkedin.com/in/sifbaksh
Twitter: https://twitter.com/sifbaksh

Re: Perl API - Passing script variables from one script to another

Adviser
Posts: 357
3805     0

Either way here should work. The run call looks correct to me. Not sure why it's not working here. I have done this without an issue.

Showing results for 
Search instead for 
Do you mean 

Recommended for You