Reply

SSH to a second device within Python script in NetMRI

Guru
Posts: 23
6744     0

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: 23
6744     0

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

Superuser
Posts: 115
6744     0

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: 70
6744     0

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: 70
6744     0

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: 23
6744     0

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: 23
6744     0

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: 23
6744     0

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

 

 

Thanks

Russ

 

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

Superuser
Posts: 115
6744     0

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: 23
6744     0

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: 23
6744     0

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.

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

[ Edited ]
Expert
Posts: 18
6744     0

Curious @RParker_1 - What I am seeing now with this script is that it is not using the credentials that I enter in the job.  Instead it is using the 'guess' credentials (I think), but definitely not anything I entered in the job.  Did you have that issue?  Is it still working?

 

Thanks,

Lon.

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

Guru
Posts: 23
6744     0

Hi Lon. 

Just looking now. Ive been tied up so not been able to check.

At the moment, my simple script is still working. To be honest, this hasn't progressed further than getting CDP from 2 devices as this was passed to another team to implement.

The only comment i would make that may differentiate us is that I am using the system credentials (a generic account for NetMRI), and not my user credentials (if that was what you were referring to). I confirmed that was the case in our logs.

 

But, guessing that is what you are asking, I have just run the script entering my credentials (not the system) and found that indeed it still uses the generic credentials for NetMRI. For me that isn't an issue as we audit through the TACACS server but may point to a bug. 

 

I have run a script that just pulls Show Ver from a switch using my credentials and TACACS logs show that my own credentials ARE being used. 

 

Processing Device: Dev8545test-a4
Opening DIS and Cli Session for device_id: 3528428477999463517
Cli Connection: {'cli_connection': {'CommandDetail': None, 'CommandType': 'open_connection', 'DeviceID': '3528428477999463517', 'SessionID': '2-8988190722050008', 'Timestamp': '2022-03-09 14:20:21', '_class': 'CliConnection'}}
Send Response: {'command_response': 'Load for five secs: 13%/0%; one minute: 6%; five minutes: 6%\nTime source is NTP, 14:20:22.708 GMT Wed Mar 9 2022\n\nCapability Codes: R - Router, T - Trans Bridge, B - Source Route Bridge\n S - Switch, H - Host, I - IGMP, r - Repeater, P - Phone, \n D - Remote, C - CVTA, M - Two-port Mac Relay \n\nDevice ID Local Intrfce Holdtme Capability Platform Port ID\nDev8545test-D1 Gig 0/1 134 R S I WS-C6509- Gig 2/4/22'}
Close response: {'command_response': '""'}
Processing DeviceSmiley Very Happyev8545test-a2
Opening DIS and Cli Session for device_id: 8648519001430410424
Cli Connection: {'cli_connection': {'CommandDetail': None, 'CommandType': 'open_connection', 'DeviceID': '8648519001430410424', 'SessionID': '2-9188876712882471', 'Timestamp': '2022-03-09 14:20:30', '_class': 'CliConnection'}}
Send Response: {'command_response': 'Capability Codes: R - Router, T - Trans Bridge, B - Source Route Bridge\n S - Switch, H - Host, I - IGMP, r - Repeater, P - Phone, \n D - Remote, C - CVTA, M - Two-port Mac Relay \n\nDevice ID Local Intrfce Holdtme Capability Platform Port ID\nDev8545test-D1 Gig 1/1/1 131 R S I WS-C6509- Gig 2/3/21\n\nTotal cdp entries displayed : 1'}
Close response: {'command_response': '""'}

 

Showing results for 
Search instead for 
Did you mean: 

Recommended for You