Network Management

last person joined: yesterday 

Keep an informative eye on your network with IMC and Airwave network management solutions.
Expand all | Collapse all

Activate API management

  • 1.  Activate API management

    Posted Jul 24, 2017 07:19 AM

     

    Hello all,

     

    I'm doing a very simple python 3 Script to query the activate inventory.

    The problem is that the server is ignoring the body json filters, although URL filters are working.

    According to the doc, I wrote the code below.

    Json body filter is completely ignored, and if I omit the URL filter the response is all devices in the directory

    Everything is working but the Json body Mac device filter.

     

    Could you give a hand?

    Thank you very much

     

     

    import os, sys, re, argparse
    import urllib.parse
    import getpass
    import urllib.request
    import urllib
    import json
    import traceback
    import pprint



    line="https://activate.arubanetworks.com"
    string_oauth={"credential_0" : "XXXXX","credential_1": "XXXXXX"}


    jsonbody_url=urllib.parse.urlencode(string_oauth)
    jsonbody_bytes = jsonbody_url.encode("utf-8")

    print('Autenticando en : ',line)
    req=urllib.request.Request(url=line+"/LOGIN",data=jsonbody_bytes,method='POST')
    #construimos y enviamos consulta http
    try:
    f=urllib.request.urlopen(req)
    except urllib.error.HTTPError as e:
    if hasattr(e, 'code'):
    print('ERROR HTTP ', e.code)

    if hasattr(e, 'detail'):
    print('Reason: ', e.detail)
    print("autenticación fallida:Cerrando Aplicacion")
    sys.exit(e.code)
    else:
    print("solicitud aprobada")
    cookie=f.getheader('Set-Cookie')

    f.read()

    string_query={"devices" : "34:FC:B9:CE:12:E4"}
    json_alta_guest=json.dumps(string_query)
    json_alta_guest_bytes=json_alta_guest.encode('utf-8')
    req=urllib.request.Request(line+"/api/ext/inventory.json?action=query&deviceFilter=basicData&addDate=06-15-2017",data=json_alta_guest_bytes,headers={"Cookie" : cookie,"Content-Type": "Application/json", "charset":"UTF-8",
    "Connection": "Keep-Alive"},method='POST')
    try:
    f=urllib.request.urlopen(req)
    except urllib.error.HTTPError as e:
    var=e.read().decode("utf-8")

    print("HTTP "+str(e.code)+" "+"
    ")

    else:
    print(f.headers)
    data=json.loads(f.read().decode('utf-8'))
    print(data)
    print("succesfull")



  • 2.  RE: Activate API management

    Posted Jul 24, 2017 08:07 AM

    Hi,

     

    Try this Curl command from any ofyour linux server it will pull all the whitelist devices from activate.

     

    #curl -3 -b Activate-cookie.txt 'https://activate.arubanetworks.com/api/ext/inventory.json?action=whitelist'

     

    Regards,

    Pavan



  • 3.  RE: Activate API management

    Posted Jul 24, 2017 08:24 AM

    Hello Pavan,

    Curl is not an option in my enviroment, but I tried the url on my script and worked. The problem is that we have also controllers and switches we want to query about.

    I'm interested in getting all device of data matching a certain mac_address, or, if possible, a serial number, do you know any url request which allows to do that?

     

    Would like to solve the json body filter anyway, anybody has more ideas?



  • 4.  RE: Activate API management

    Posted Jul 24, 2017 09:15 AM

    Hi,

     

    from where you want to pull the controller and switch inventory details Activate or Airwave?

     

    Regards,

    Pavan



  • 5.  RE: Activate API management

    Posted Jul 24, 2017 09:22 AM

    <Hello Pavan

     

    Activate



  • 6.  RE: Activate API management

    Posted Jul 24, 2017 09:48 AM

    Pass the MAC as form-urlencoded:

    { "devices" : [ "AA:BB:CC:11:22:33" ] } 

     



  • 7.  RE: Activate API management

    Posted Jul 24, 2017 11:20 AM

    Hello TIm,

     

    I already tried:

     

    string_query={"devices" : [ "34:FC:B9:CE:14:7E"]}
    jsonbody_url=urllib.parse.urlencode(string_query)
    json_alta_guest_bytes=jsonbody_url.encode("utf-8")
    req=urllib.request.Request(line+"/api/ext/inventory.json?action=query&deviceFilter=basicData&addDate=06-16-2017",data=json_alta_guest_bytes,headers={"Cookie" : cookie,
    "Content-Type": "application/x-www-form-urlencoded", "charset":"UTF-8","Content-length":len(json_alta_guest_bytes),
    "Connection": "Keep-Alive"},method='POST')

     

    and no effect. I left the other version because is the same method I use to manage ClearPass Json API, so that's the reason I think the code is correct.

     

    I tried with an APi that only accepts jsond body filters(folder update API):


    string_query={"devices" : [{"mac" : "20:4C:03:04:31:80",
    "deviceName" : "nombre de prueba",
    "folderName": "XXXXX"
    }]}
    #activate.arubanetworks.com/api/ext/inventory.json?action=update.

    #string_query={"devices" : [ "34:FC:B9:CE:14:7E"]}
    #json_alta_guest=json.dumps(string_query)
    json_alta_guest=urllib.parse.urlencode(string_query)
    datos=json_alta_guest.encode('utf-8')

    req=urllib.request.Request(line+"/api/ext/inventory.json?action=update",data=datos,headers={"Cookie" : cookie,
    "charset":"UTF-8","Content-length":len(datos),"Host": "activate.arubanetworks.com",
    "Connection": "keep-alive"},method='POST')

    try:
    f=urllib.request.urlopen(req)

     

     

    and this is the server reponse:

    errors': [{'key': 'json', 'errMessage': 'json POST parameter must be supplied for update action}]

     

    Do you have any way to check what's the error detected in the server to return such error message?

    As I said, the way I build the request is the same I use for Clearpass guest API, so I think the problem is not the request I'm sending...maybe a header missing?

     



  • 8.  RE: Activate API management

    Posted Jul 24, 2017 09:50 AM
      |   view attached

    Hi,

     

    I have attached Aruba Activate guide , Check Inventory APIs from page 36 for more details.

     

    Regards,

    Pavan

     

    If my post addresses your query give kudos:)

    Attachment(s)



  • 9.  RE: Activate API management

    Posted Jul 24, 2017 11:27 AM

    Hello Pavan, 

    I already read the doc before writing the code, that's the reason I posted the case.

     

    thank you all for the support



  • 10.  RE: Activate API management

    Posted Jul 24, 2017 11:46 AM

    Hello TIm,

     

    I also tried Urlenconding the data before sending with no luck. The code I posted is pretty much the same I use to manage the Clearpass API, so I think the code is ok. I could have overlook something though.

     

    string_query={"devices" : [ "34:FC:B9:CE:14:7E"]}

    jsonbody_url=urllib.parse.urlencode(string_query)
    json_alta_guest_bytes=jsonbody_url.encode("utf-8")
    req=urllib.request.Request(line+"/api/ext/inventory.json?action=query&deviceFilter=basicData&addDate=06-16-2017",data=json_alta_guest_bytes,headers={"Cookie" : cookie,
    "Content-Type": "application/x-www-form-urlencoded", "charset":"UTF-8","Content-length":len(json_alta_guest_bytes),
    "Connection": "Keep-Alive"},method='POST')

     

    In case it helps, I tried with an APi that only accepts json body filters (folder update API)

     

    string_query={"devices" : [{"mac" : "20:4C:03:04:31:80",
    "deviceName" : "nombre de prueba",
    "folderName": "XXXX"}]}

    json_alta_guest=urllib.parse.urlencode(string_query)
    datos=json_alta_guest.encode('utf-8')

    req=urllib.request.Request(line+"/api/ext/inventory.json?action=update",data=datos,headers={"Cookie" : cookie,
    "charset":"UTF-8","Content-length":len(datos),"Host": "activate.arubanetworks.com",
    "Connection": "keep-alive","Accept-Encoding":"deflate, br","Origin":"https://activate.arubanetworks.com",
    "Referer":"https://activate.arubanetworks.com/registration/","Accept-Language":"es-ES,es;q=0.8"},method='POST')

    try:
    f=urllib.request.urlopen(req)

     

     

    and this is the server response:

     

    -----errors': [{'key': 'json', 'errMessage': 'json POST parameter must be supplied for update action.'}]}

     

    Do you have any way to check what's the reason the server returns this error?

     

    I tried with the other version too:

    string_query={"devices" : [{"mac" : "20:4C:03:04:31:80",
    "deviceName" : "nombre de prueba",
    "folderName": "XXXX"}]}
    json_alta_guest=json.dumps(string_query)
    datos=json_alta_guest.encode('utf-8')

    req=urllib.request.Request(line+"/api/ext/inventory.json?action=update",data=datos,headers={"Cookie" : cookie,
    "charset":"UTF-8","Content-length":len(datos),"Host": "activate.arubanetworks.com",
    "Connection": "keep-alive","Accept-Encoding":"deflate, br","Origin":"https://activate.arubanetworks.com",
    "Referer":"https://activate.arubanetworks.com/registration/","Accept-Language":"es-ES,es;q=0.8","Accept":"*/*"},method='POST')

     

    and same server response

     

    Thank you very much



  • 11.  RE: Activate API management

    Posted Jul 24, 2017 11:54 AM

    You need to use form data, not JSON payload.



  • 12.  RE: Activate API management

    Posted Jul 24, 2017 04:38 PM

    I used form data with request library and not working either

     

    string_query={"devices" : [{"mac" : "20:4C:03:04:31:80",
    "deviceName" : "nombre de prueba",
    "folderName": "XXXXXX" }]}


    try:
    f = requests.post(line+"/api/ext/inventory.json?action=update", files={key: str(value) for key, value in string_query.items()},headers=......

     

    json POST parameter must be supplied for update action."}]}



  • 13.  RE: Activate API management

    Posted Jul 25, 2017 03:36 PM

    Hello all,

     

    This is working;

     

    curl -d 'json={"devices" : ["00:1A:1E:29:73:B2","00:1A:1E:29:73:B2"]}' -b Activate-cookie.txt -v https://activate.arubanetworks.com/api/ext/inventory.json?action=query --trace /dev/stdout

     

    but this python 3 script is not:

     

    string_query={"devices" : [ "00:1A:1E:29:73:B2","00:1A:1E:29:73:B2" ]}
    headers={
    "Cookie" : cookie,
    "Content-Type": "application/x-www-form-urlencoded",
    "User-Agent":"curl/7.47.0",
    "charset":"UTF-8",
    "Connection": "Keep-Alive",
    "Accept": "*/*"
    }

    response = requests.post(line+"/api/ext/inventory.json?action=query&deviceFilter=basicData&addDate=06-16-2017",data=jsonbody_url,headers=headers)
    print(response.text)

     

    Also I am taking a look to the Java sample code in the doc and I found this:

    if (status != 200) System.out.println("login failed code[" + status + "]"); else {

             request.sendRequest("/api/ext/folder.json?action=query", null);            

              System.out.println(request.getResponseBody());

             String post = "json={\"folders\":[\"72ffcf7c-6242-415a-84a4-        f64606af3c9c\"]}";

             request.sendRequest("/api/ext/inventory.json?action=query",      post);

            System.out.println(request.getResponseBody());

     

          public void sendRequest(String api, String post)

     

           .....

     

    Looks like you are doing 2 request to the server, first one with a Get and a second one with a POST

     

    Can you clear this up?