Peplink Router API question (using Python)

Hello all,

I am trying to use the API to modify the WAN configuration on my MAX Transit Duo (8.1.3 firmware) via the REST API (from Python, but I’ve also tried CURL). I’m finding that no changes ever take effect.

For context, the ultimate goal is to be able to disable/enable WANs selectively to allow for automated speed testing. I’d really like to be able to change the Outbound Policy for that purpose, but the API does not seem to support that.

I have sample code below that does the following steps:
• Log in (user/pass) and preserve the pauth cookie
• Query the current ‘name’ and ‘enable’ state for a specific WAN (status.wan.connection)
• Attempt to change those values (config.wan.connection & cmd.config.apply)
• Re-query the current ‘name’ and ‘enable’ state for that WAN

Observe that the values do not change.

Questions:
• Am I missing something in the API invocation? The documentation discusses creating client and auth tokens, but that seems like an alternate authentication path.
They do not seem to be needed here, and I’ve tried that with no different results.
• Has anyone else been successful with these API calls?
• Is there a better approach than the API? (The CLI does not appear to support making these sorts of changes).

Code and the output are posted in a reply beacuse the forum software takes forever to render it as a code snippet. Wait for 2-4 minutes for it to render.

Thanks for any assistance!
–Roger

Code and output referenced in the first post:


Code:
===================================================
import sys
import requests
import json


## Define target username and password
baseUrl = 'http://10.11.0.1'
uName='admin'
pWord='MyPassWord'


## Use a Session object to preserve the pauth cookie after authentication
sesh=requests.Session()


###############################
print("Connecting...")   

payload = {
    "username" : uName,
    "password" : pWord
    }

# using 'json-' param sets the Content type
response=sesh.post(baseUrl + '/api/' + 'login', json=payload )

if response.json()['stat'] == "ok":
    print("Login succeeded: pauth=" + response.cookies['pauth'])
else:
    sys.exit("Login failed")
    
# Note: pauth is now captured in the Session object
# can use sesh.cookies to view


############ Test API calls

# Get WAN status of specific id
wanId="2"

print("\nBEFORE Wan Info:")

payload={"id":wanId }
response=sesh.get(baseUrl + '/api/' + 'status.wan.connection', params=payload )
if response.json()['stat'] != "ok":
    sys.exit("Status failed")

## Full response
#print(json.dumps(response.json(), indent=2))

print("Name:   " + str(response.json()['response'][wanId]['name']))
print("Enable: " + str(response.json()['response'][wanId]['enable']))


print("\nAttempting to change name and enable flag")

payload={
    "action": "update",
        "list" : {
            "id": wanId,
            "name" :  "New Name",
            "enable": False
            }
    }

response=sesh.post(baseUrl + '/api/' + 'config.wan.connection', json=payload )

if response.json()['stat'] == "ok":
    print("   Config returned 'ok'")
else:
    sys.exit("config failed: \n"+str(json.dumps(response.json(), indent=2)))
    
response=sesh.post(baseUrl + '/api/' + 'cmd.config.apply' )
if response.json()['stat'] == "ok":
    print("   Apply returned 'ok'")
else:
    sys.exit("Apply failed: \n"+str(json.dumps(response.json(), indent=2)))

print("\nAFTER Wan Info:")

payload={"id":wanId }
response=sesh.get(baseUrl + '/api/' + 'status.wan.connection', params=payload )
if response.json()['stat'] != "ok":
    sys.exit("Status failed")

## Full response
#print(json.dumps(response.json(), indent=2))

print("Name:   " + str(response.json()['response'][wanId]['name']))
print("Enable: " + str(response.json()['response'][wanId]['enable']))

===================================================

Output

===================================================
Connecting...
Login succeeded: pauth=DoPwxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxvct8B

BEFORE Wan Info:
Name:   Cell1-A Spr/Tmo
Enable: True

Attempting to change name and enable flag
   Config returned 'ok'
   Apply returned 'ok'

AFTER Wan Info:
Name:   Cell1-A Spr/Tmo
Enable: True
===================================================

My released v1 of scripts were written in windows batch script.
https://forum.peplink.com/t/automated-deployment-for-batch-of-100-br1-minis/6121e3d36b1bec0690002d8a/
You should be able to look at the curls I used and modify it.
I’m specifically changing the orders of the wan at one point.
V2 scripts was a complete re-write to drive 24 peplinks from a raspberry pi with a management display, but it was heavily customized for our needs, along with required hardware like a peplink switch so I never released it. Hopefully this helps. I will also add that the device api calls are heavily dependent upon the firmware version that the device is running.
For the documented device api calls on 8.1.3 please refer to this document: