# Overview
## 佈置
- [連接 Github](https://hackmd.io/@5giotlead/tb-repo)
- 下載檔案
```
git clone git@github.com:5giotlead/apim.git
```
- 執行 `vapix.sh` 檔案
```
./vapix.sh
```
- 若無權限,給檔案加上權限,重新執行
```
chmod +x vapix.sh
```
## vapix .sh code
:::spoiler
```shell=
#!/bin/bash
DEFAULT_USER="swae"
DEFAULT_PASS="5Giotlead"
DEFAULT_IP="192.168.1.57"
DEFAULT_HTTP_METHOD="POST"
declare -A API_MAP
declare -A API_JSON
API_CATEGORIES=("Custom" "Applications" "AOA")
API_MAP["Custom,1"]="/axis-cgi/customhttpheader.cgi"
API_JSON["Custom,1"]="true"
API_MAP["Applications,1"]="/axis-cgi/applications/list.cgi"
API_JSON["Applications,1"]="false"
API_MAP["AOA,1"]="/local/objectanalytics/control.cgi"
API_JSON["AOA,1"]="true"
echo "Choose one auth type: "
echo "1) Basic"
echo "2) Digest"
read -p "(1/2) > " auth_choice
case "$auth_choice" in
1) auth_method="basic" ;;
2) auth_method="digest" ;;
*) echo "Invalid! Please enter 1 or 2"; exit 1 ;;
esac
echo "Choose API category:"
for idx in "${!API_CATEGORIES[@]}"; do
echo "$((idx+1))) ${API_CATEGORIES[$idx]}"
done
read -p "Category number > " cat_choice
cat_idx=$((cat_choice-1))
if [[ -z "${API_CATEGORIES[$cat_idx]}" ]]; then
echo "Invalid category!"
exit 1
fi
category="${API_CATEGORIES[$cat_idx]}"
echo "Choose API:"
api_count=0
for key in "${!API_MAP[@]}"; do
IFS=',' read -r cat api_idx <<< "$key"
if [[ "$cat" == "$category" ]]; then
api_count=$((api_count+1))
echo "$api_count) ${API_MAP[$key]}"
api_keys[$api_count]="$key"
fi
done
read -p "API number > " api_choice
api_key="${api_keys[$api_choice]}"
use_api="${API_MAP[$api_key]}"
use_json="${API_JSON[$api_key]}"
use_jq=""
if [[ "$use_json" == "true" ]]; then
use_jq="| jq ."
fi
read -p "Username: [$DEFAULT_USER]: " -r username
username="${username:-$DEFAULT_USER}"
read -s -p "Password: (Default is filled in)" -r password
password="${password:-$DEFAULT_PASS}"
echo
read -p "HTTP Method? [$DEFAULT_HTTP_METHOD]: " -r http_method
http_method="${http_method:-$DEFAULT_HTTP_METHOD}"
read -p "Target IP address? [$DEFAULT_IP]: " -r ip_addr
ip_addr="${ip_addr:-$DEFAULT_IP}"
if [[ $use_json == "true" ]]; then
read -e -p "A json file path for JSON input parameters: " json_path
curl_cmd="curl -u $username:$password --$auth_method -X $http_method -H \"Content-Type: application/json\" -d @$json_path http://$ip_addr$use_api $use_jq"
else
curl_cmd="curl -u $username:$password --$auth_method -X $http_method http://$ip_addr$use_api $use_jq"
fi
echo "Issue HTTP Request:"
echo "$curl_cmd"
eval $curl_cmd
```
:::
## 使用範例 (Custom HTTP header API)
```
Choose one auth type:
1) Basic
2) Digest
(1/2) > 2
Choose API category:
1) Custom
2) Applications
3) AOA
Category number > 1
Choose API:
1) /axis-cgi/customhttpheader.cgi
API number > 1
Username: [swae]:
Password: (Default is filled in)
HTTP Method? [POST]:
Target IP address? [192.168.1.57]:
A json file path for JSON input parameters: ListCustomHeaders.json
Issue HTTP Request:
curl -u swae:5Giotlead --digest -X POST -H "Content-Type: application/json" -d @ListCustomHeaders.json http://192.168.1.57/axis-cgi/customhttpheader.cgi | jq .
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 381 100 381 0 0 58778 0 --:--:-- --:--:-- --:--:-- 63500
100 1083 0 1004 100 79 28568 2247 --:--:-- --:--:-- --:--:-- 30815
{
"apiVersion": "1.0",
"context": "OptionalContext",
"method": "list",
"data": {
"X-Content-Type-Options": "nosniff",
"X-Frame-Options": "SAMEORIGIN",
"X-XSS-Protection": "1; mode=block",
"Content-Security-Policy": "default-src 'self'; frame-ancestors 'self'; connect-src 'self' https://*.google-analytics.com https://*.analytics.google.com https://*.googletagmanager.com https://*.axis.com mediastream: blob:; script-src 'self' https://*.googletagmanager.com https://www.google-analytics.com https://ssl.google-analytics.com https://*.axis.com; style-src 'self' 'unsafe-inline'; img-src 'self' https://*.google-analytics.com https://*.googletagmanager.com https://*.axis.com data: blob:; media-src 'self' mediastream: blob:; object-src 'none'",
"Referrer-Policy": "strict-origin-when-cross-origin",
"Access-Control-Allow-Headers": "authorization",
"Access-Control-Allow-Origin": "http://192.168.1.223:8080",
"Access-Control-Expose-Headers": "WWW-Authenticate",
"Access-Control-Allow-Credentials": "true"
}
}
```