Reply

SSH to a second device within Python script in NetMRI

Guru
Posts: 53
540     1

Hi, 

Not sure if this has been asked before but is there a way of connecting to a second device using SSH using Python within the same script. 

So we use "easy" to connect to the main device, but we need to potentially configure a neighbour device in the same job. For that we would need to identify the DeviceID, probably through a CDP neighbour call, but not sure how to connect to that device (in Python)

 

I did find an old Perl script from Sif, that might do the trick but we need to use Python at this stage.

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

 

Thanks

 

Russ

 

 

Re: SSH to a second device within Python script in NetMRI

Guru
Posts: 53
540     1

Is anyone able to provide a reply to this one?

Need to connect to a 2nd device within a Python script. 

I have done this in Perl but not Python.

 

 

Thanks

 

Re: SSH to a second device within Python script in NetMRI

Adviser
Posts: 463
540     1

Can you post the old Perl script Smiley Happy

I forgot all of these things and I can see what I can do via Python

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

https://sifbaksh.com

Re: SSH to a second device within Python script in NetMRI

Expert
Posts: 239
540     1

Maybe I don't understand your requirement, but can't you use Paramiko?  I did that some time last year, with the main script opening a SSH session to an external host.

Re: SSH to a second device within Python script in NetMRI

Expert
Posts: 239
540     1

I didn't fully read your original post.  You want NetMRI $easy to do it all?

Re: SSH to a second device within Python script in NetMRI

Guru
Posts: 53
540     1

Hi, yes, NetMRI internal scripting. I did it in Perl but need to move to Python - if possible.....

Re: SSH to a second device within Python script in NetMRI

Guru
Posts: 53
540     1

Thanks Sif, 

I cant seem to locate my original Perl script at the moment, but this is one from the Tutorials.

 

best regards

Russ

 

# BEGIN-INTERNAL-SCRIPT-BLOCK
#
# Script:
# API Example 8 - Connecting to Multiple Devices in a Script
#
# END-INTERNAL-SCRIPT-BLOCK

# BEGIN-SCRIPT-BLOCK
#
# Script-Filter:
# true
#
# Script-Variables:
# $command word "show version"
#
# END-SCRIPT-BLOCK

use strict;
use warnings;
use NetMRI_Easy;

# script variables are provided as a global
# variable from the NetMRI job engine.
our $command;

# Connect to the NetMRI
my $easy = new NetMRI_Easy;

$easy->log_message('info', '*** ' . $easy->device->DeviceName . " ***\n");

# First send the command to the device specified when running the
# script.
eval {
$easy->send_command($command);
};
my $error = $@;
if($error)
{
$easy->log_message('error', $error);
}

# Next find all the neighbors where this device is the source.
my @relations = $easy->broker->neighbor->find({
op_DeviceID => '=',
val_c_DeviceID => $easy->device_id,
op_NeighborDeviceID => 'is not null',
select => [qw( NeighborDeviceID )]
});

my @neighbors = ();
if(@relations) {
@neighbors = $easy->broker->device->find({
op_DeviceID => '=',
val_c_DeviceID => [map { $_->NeighborDeviceID } @relations],
select => [qw( DeviceName DeviceID )]
});
}

# Finally, run the same command on the neighbors.
foreach my $device (@neighbors)
{
eval {

# $easy2 is another instance of NetMRI_Easy, but instead
# of being associated with the primary device, it is assoicated
# with the secondary device that we are interested in.
my $easy2 = $easy->device_session( $device->DeviceID );

$easy2->log_message('info', '*** ' . $device->DeviceName . " ***\n");

$easy2->send_command($command);
};
my $error = $@;
if($error)
{
$easy->log_message('error', "ERROR: '$error'");
}
}

Re: SSH to a second device within Python script in NetMRI

Guru
Posts: 53
540     1

Hi Sif, were you able to get anywhere with this?

 

 

Thanks

Russ

 

Re: SSH to a second device within Python script in NetMRI

Adviser
Posts: 463
540     1

This is from Lon on the SlackChannel which we are moving to Dis-cord Smiley Happy

"Here is the script in the simplest form.  You must update the "host='netmri.mydomain.com'" and sub in proper names of devices for 'nameOfDevice1' and 'nameOfDevice2'.  This script does not use NetMRI.Easy."

 

Also, it will be posted on GitHub as well

https://github.com/infobloxopen/netmri-toolkit/tree/master/Python/NetMRI_GUI_Python

 

# BEGIN-SCRIPT-BLOCK
#
# Script-Filter:
#     true
#
# Script-Login:
#     false
#
# Script-Variables:
#
# Script-Timeout: 
#	3670
#
# END-SCRIPT-BLOCK
import infoblox_netmri
from infoblox_netmri.client import InfobloxNetMRI
client = infoblox_netmri.client.InfobloxNetMRI(
                host='netmri.mydomain.com',
                username=http_username,
                password=http_password,
                api_version='3.7',
                use_ssl='true',
                ssl_verify='true'
                )
# Get brokers for required objects
device_broker = client.get_broker('Device')	
# Search for our 2 devices to get the Id's	
devices = device_broker.search(DeviceName=[
            'nameOfDevice1','nameOfDevice2'
            ])
for i in devices:
    # Open Dis and Cli Session
    print('Opening DIS and Cli Session for device_id: ',i.DeviceID)
    cli_connection = client.api_request('dis_sessions/open_session_and_connection', params={'device_id': i.DeviceID})
    print("Cli Connection: ",cli_connection)
    # Send Command
    response = client.api_request('cli_connections/send_command', params={'SessionID': cli_connection['cli_connection']['SessionID'], 'device_id': i.DeviceID, 'command': 'sh ip int brief'})
    print("Send Response: ",response)
    # Close the Session
    response = client.api_request('dis_sessions/close', params={'SessionID': cli_connection['cli_connection']['SessionID']})
    print("Close response: ",response) 

 

 

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

https://sifbaksh.com

Re: SSH to a second device within Python script in NetMRI

Guru
Posts: 53
540     1

Thanks Sif. Sorry for the delay in replying. Exceedingly busy at the moment.....I have passed this on to our Python people and will no doubt hear back soon and will let you know how it went.

rgds

Russ

Re: SSH to a second device within Python script in NetMRI

Guru
Posts: 53
540     1

Hi Sif, 

I decided to test the script and it worked well. just need to adapt it to our use.

 

Many thanks to yourself and Lon.

Showing results for 
Search instead for 
Did you mean: 

Recommended for You