Automation Scripts

Reply
Highlighted

Python - External Script to Run the "Ad Hoc Command Batch" on NetMRI

Adviser
Posts: 373
4646     1

 

Here is a Python code to run our “Ad Hoc Command Batch” and executing a “show version” on the device.

 

The other key factor is the device ID, I would cheat right now to find the device ID so the can test it, buy using this method.

Network Explorer -> Discovery -> Find the  device you are looking for click on the IP address to bring up the “Device Viewer” and highlighted in Pink below is the device ID for 10.66.21.81

branch7_10_66_21_81.png

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Python Code:

 

__author__ = 'sbaksh'
#!/usr/bin/python
# -*- coding: utf-8 -*-
import requests
import json

# Declare the IP Address NetMRI is configured on
netmri = "https://netmriIP/api/2.9/"
# The SysAdmin user account we are using to login
username = "sbaksh"
password = "mypassword"
requests.packages.urllib3.disable_warnings()

def run_adhoc():
    object_type = "scripts/run?name=Ad Hoc Command Batch&$commands_to_be_executed=sh version&device_ids=24607"
    data = requests.get(netmri + object_type, verify=False, auth=(username, password))
    json_input = data.text
    print json_input

run_adhoc()

print "Completed"

 

 

 

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

Re: Python - External Script to Run the "Ad Hoc Command Batch" on NetMRI

Expert
Posts: 139
4647     1

Hi Sif - 

 

I was trying out your script and wanted to add the 'wait' parameter in, but the call keeps giving me an error.  I've tried it both as an integer and a string for the seconds to wait.  I wanted to see what the payload result would look like.  The job executes and completes just fine and returns the job id if I exclude the 'wait'.

 

Could you give it a try, please?

 

def run_adhoc():
        data = client.api_request(
                'scripts/run', params={
                        'name': "NW - Ad Hoc Command Batch",
                        '$commands_to_be_executed': "sh version",
                        'device_ids': "290",
                        'credential_mode': "requestor",
                        'wait': 30,
                }
        )
        json_input = data.text
        print json_input

python ad_hoc_command.py
Traceback (most recent call last):
  File "ad_hoc_command.py", line 38, in <module>
    run_adhoc()
  File "ad_hoc_command.py", line 32, in run_adhoc
    'wait': 30,
  File "/home/netmrift/lib/python2.7/site-packages/infoblox_netmri-3.2.0.0-py2.7.egg/infoblox_netmri/client.py", line 227, in api_request
    return self._make_request(url=url, method="post", data=data)
  File "/home/netmrift/lib/python2.7/site-packages/infoblox_netmri-3.2.0.0-py2.7.egg/infoblox_netmri/client.py", line 103, in _make_request
    return self._send_request(url, method, data, extra_headers)
  File "/home/netmrift/lib/python2.7/site-packages/infoblox_netmri-3.2.0.0-py2.7.egg/infoblox_netmri/client.py", line 129, in _send_request
    raise HTTPError(res.json(), response=res)
requests.exceptions.HTTPError: {u'message': u'SQL::Error', u'error': u'general/unexpected-error'}

Thanks,

Lon.

Re: Python - External Script to Run the "Ad Hoc Command Batch" on NetMRI

Adviser
Posts: 373
4647     1

 

This is what I did:

broker_script = myeasy.broker('Script')

save_new = {
"name" : "sif_adhoc",
"device_ids" : device_id,
"job_name" : "Save New Config",
"username" : "seuser",
"password" : "serouter"
}
time.sleep(20)

broker_script.run (**save_new)

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

Re: Python - External Script to Run the "Ad Hoc Command Batch" on NetMRI

Expert
Posts: 139
4647     1

So are you infering that the 'wait' didn't work for you either?  I can open a new case if it doesn't work.

 

Thanks,

Lon.

Re: Python - External Script to Run the "Ad Hoc Command Batch" on NetMRI

Adviser
Posts: 357
4647     1
IIRC, ”wait” is a client-side function, not a server-side one. Is it implemented in the Python libraries or only the Perl ones?

Re: Python - External Script to Run the "Ad Hoc Command Batch" on NetMRI

Expert
Posts: 139
4647     1

I'm running it from outside of NetMRI as a client.  The 'wait' is documented in the NetMRI API, Perl and Python libraries for scripts/run.

Re: Python - External Script to Run the "Ad Hoc Command Batch" on NetMRI

Adviser
Posts: 373
4647     1

Since you are getting the error on the "wait", I would grab the user JobID once it runs and then gets the status via another call Smiley Happy

Follow me on LinkedIn: https://www.linkedin.com/in/sifbaksh
Twitter: https://twitter.com/sifbaksh
Showing results for 
Search instead for 
Do you mean 

Recommended for You