Description

Here is related HTTP API available to use on Milesight router(OpenCPU system version), user can follow below steps to invoke them to control and manage devices directly.

 

 

Requirement

 ● Milesight Router: UR75v3, UF51v2

 ● Linux system



Configuration

Step 1: Install Curl software to Linux System

1. 

sudo apt install curl

2. Check install result:

curl -V

If curl software has been installed normally, you can get software version later.

 

Step 2: Access to Milesight Router web GUI, go to Network > Firewall > Security, enable SSH access service, and Save it.

Step 3: Use the router's login username and password to obtain the API token key.

Use the curl command below, remember to change IP '192.168.45.187' to your router IP address and it is neccessry to use the HTTPS request.

 

Note: the password is AES encrypted value. AES encryption settings:

 Parameter 

Option

Encryption cipher mode

CBC

Encryption Key Size

128 bits

IV(initial Vector)

 2222222222222222 

 Secret key 

 1111111111111111 

 Output format 

 Base64 

 Example: Online AES Encryption / Decryption | Anycript 


curl -k -c ./cookie.txt -X POST "https://192.168.45.187/cgi-bin/luci" -d "luci_username=admin&luci_password=sI/7ewBCeWunDs6JXXtSHg==" -v


If the command execute successful, you can use the command below to check the token in the cookie.txt file.

cat cookie.txt

 

 

Step 4: Obtain the required UCI commands 

Open the web GUI of router via browser, and then open the Developer tools via click F12 button on the keyboard or through the browser settings.

 

Then you can find the UCI commands of the current web page.


 

Step 5: Calling the API for the Router

When calling the API for the UR75v3, please note that the request URL needs to include the current timestamp. In the following example, we provide a script for your reference to include the current timestamp when making an HTTPS request.


Using the command below to create UR75v3_HTTP_API.sh file:

vi UR75v3_HTTP_API.sh

Paste the sample script below and save the .sh file.

#!/bin/bash

# Step 1: Send login request and save cookie
login_url="https://192.168.45.187/cgi-bin/luci?luci_username=admin&luci_password=sI/7ewBCeWunDs6JXXtSHg=="
cookie_file="cookies.txt"
curl -k -c $cookie_file "$login_url"

# Step 2: Extracting sysauth values from cookie files
sysauth=$(grep 'sysauth' $cookie_file | cut -f 7)
if [ -z "$sysauth" ]; then
  echo "Failed to retrieve sysauth value from cookies."
  exit 1
fi

# Step 3: Replace the params in the JSON body with the extracted sysauth value
json_body=$(cat <<EOF
[
    {"jsonrpc":"2.0","id":27,"method":"call","params":["$sysauth","luci","getSystemBoard",{}]},
    {"jsonrpc":"2.0","id":27,"method":"call","params":["$sysauth","system","info",{}]},
    {"jsonrpc":"2.0","id":28,"method":"call","params":["$sysauth","luci","getSystemBoard",{}]},
    {"jsonrpc":"2.0","id":29,"method":"call","params":["$sysauth","system","info",{}]},
    {"jsonrpc":"2.0","id":30,"method":"call","params":["$sysauth","luci","getCurrentNetwork",{}]},
    {"jsonrpc":"2.0","id":31,"method":"call","params":["$sysauth","luci-rpc","getDHCPLeases",{}]},
    {"jsonrpc":"2.0","id":32,"method":"call","params":["$sysauth","luci-rpc","getHostHints",{}]},
    {"jsonrpc":"2.0","id":33,"method":"call","params":["$sysauth","luci-rpc","getDSLStatus",{}]},
    {"jsonrpc":"2.0","id":34,"method":"call","params":["$sysauth","ups","get_info",{}]}
]
EOF
)

# Step 4: Get the current timestamp and build the URL
currentTimestamp=$(date +%s)
post_url="https://192.168.45.187/cgi-bin/luci/admin/ubus?$currentTimestamp"

# Step 5: Sending a POST request with updated params
curl -k -b $cookie_file -X POST "$post_url" \
     -H "Content-Type: application/json" \
     -d "$json_body"

Execute the script file, you can see the result below.



---END---