Last active
December 9, 2019 14:45
-
-
Save jamesallman/4c089dc63187dfb937b2bd6c070469e3 to your computer and use it in GitHub Desktop.
Example FortiManager API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
user=username | |
pass=password | |
url=https://172.31.255.100/jsonrpc | |
id=0 | |
# $1 method | |
# $2 url | |
# $3 data | |
fm() { | |
local json="{'method': '$1', 'id': $id, 'params': [{'url': '$2' ${3:+,'data': $3}}] ${session:+,'session': '$session'}}" | |
response=$(curl -k -s "$url" -d "$json") | |
id=$(($id + 1)) | |
} | |
# $1 user | |
# $2 pass | |
fmlogin() { | |
local data="{'user': '$user', 'passwd': '$pass'}" | |
fm exec /sys/login/user "$data" | |
session=$(echo $response | jq .session) | |
} | |
fmlogout() { | |
fm exec /sys/logout | |
unset session | |
} | |
# Proxy a request to FortiGate | |
# $1 adom | |
# $2 device name | |
# $3 action | |
# $4 resource | |
# $5 payload | |
fmproxy() { | |
local target="['adom/$1/device/$2']" | |
local data="{'action': '$3', 'resource': '$4', ${5:+'payload': '$5',} 'target': $target}" | |
fm exec /sys/proxy/json "$data" | |
} | |
fmlogin $user $pass | |
# Exit if no session | |
[ -z "$session" ] && exit | |
# Example 1: Get the hostname and version from the remote FortiGate via FortiManager proxy | |
fmproxy root FGT60D4613016433 get /api/v2/cmdb/system/global?format=hostname\|version | |
hostname=$(echo $response | jq .result[0].data[0].response.results.hostname) | |
# hostname=FGT60D4613016433 | |
version=$(echo $response | jq .result[0].data[0].response.version) | |
# version=v6.0.4 | |
# Example 2: Get hostname, platform and version from the FortiManager device database | |
fm get /dvmdb/adom/root/device/FGT60D4613016433 | |
hostname=$(echo $response | jq .result[0].data.hostname) | |
# hostname=FGT60D4613016433 | |
platform=$(echo $response | jq .result[0].data.platform_str) | |
# platform=FortiGate-60D | |
os_ver=$(echo $response | jq .result[0].data.os_ver) | |
# os_ver=6 | |
mr=$(echo $response | jq .result[0].data.mr) | |
# mr=0 | |
patch=$(echo $response | jq .result[0].data.patch) | |
# patch=4 | |
fmlogout |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment