Operational Scripts Using the API#
For preparation before running the samples (preparing a token, using jq, and so on) and the token type and API permission required for each sample, see "API Usage Examples".
Rescan All Servers#
The following examples combine the APIs for retrieving the server list and scanning a server to rescan all servers one at a time for each group token specified in the script. A rescan matches the previous configuration information registered in FutureVuls against the latest vulnerability information again (for servers using the Amazon Inspector, container registry, or GitHub integration, it also retrieves the latest information from the integrated service. For details, see "Periodic Scans, Manual Scans, and Automatic Re-matching"). Use group tokens with the API permission "Read, Update" or higher (see Creating a FutureVuls API Token). For instructions on saving and running the scripts, see the comments at the beginning of each script.
The following table shows how the scripts process each response and what the operator should check. For the rate limits and how to handle 429, see "API Rate Limits". For where to check when no response is received, see "Queuing for Scans and SBOM Registration".
| Operation | Response | Script behavior | Items to check |
|---|---|---|---|
| Retrieving the server list | HTTP 429 | Stops processing for that token | Run the script again after some time has passed. Check the parallelism and request intervals of processes that use the same token or source IP address (see What to Do After Being Rate-Limited) |
| Retrieving the server list | Other errors (such as authentication or permission errors) | Stops processing for that token | The token value and API permission |
| Requesting a server scan | HTTP 200 | Counts it as an accepted request | Check the completion of detection processing with the server's "Last Detection Time" in the FutureVuls UI |
| Requesting a server scan | HTTP 429 | Waits 60 seconds and retries up to 3 times. If 429 is still returned after the retries, counts it as a failure | If 429 is still returned after the retries, check the parallelism and request intervals of processes that use the same token or source IP address (see What to Do After Being Rate-Limited) |
| Requesting a server scan | 000 (a value the script sets when no response is received), HTTP 504 (timed out at a relay server) |
Counts it as "result unknown" | Do not rescan immediately; after some time has passed, check whether "Last Detection Time" has been updated |
| Requesting a server scan | Other errors | Counts it as a failure | Rescan from the FutureVuls UI or through the API. For servers with no scan results registered yet, register scan results first, for example by running the scanner |
Using bash#
#!/usr/bin/env bash
# Rescan all servers in a group one at a time using the FutureVuls API.
#
# [Before you start]
# - This script requires jq. If it is not installed, install it first.
# - Prepare group tokens with the API permission "Read, Update" or higher.
#
# [Steps]
# 1. Create a scan-all-servers.sh file in a location of your choice, paste this script into it, and save it.
# 2. Enter the group tokens in TOKENS below.
# 3. Open a terminal and navigate to the folder where you saved this file.
# 4. Grant execute permission and run it as follows.
# chmod 700 scan-all-servers.sh
# ./scan-all-servers.sh
# With many servers, results may scroll off the screen. To also save them to a file, run the following command.
# ./scan-all-servers.sh 2>&1 | tee -a scan-all-servers.log
# If a file with the same name exists, results are appended to it.
#
# [If an error occurs]
# - HTTP 000 (no response was received) and HTTP 504 (a relay server timed out) are counted as "Unknown". Processing may still be continuing on the FutureVuls side, so do not rescan immediately; after a while, check whether the server's "Last Detection Time" in the FutureVuls UI has been updated.
# - For how to handle other responses, see the table in "Operational Scripts Using the API" in the help.
#
# [Notes]
# - Running this script scans the target servers sequentially.
# - The script waits between servers to avoid hitting API rate limits.
# - Do not run multiple instances of this script at the same time.
# - Do not share this file containing tokens with others or upload it to Git or similar services.
# Enter one token per line, enclosed in " " (no commas are needed for multiple tokens).
TOKENS=(
"xxxxxxxxxxxxxxxx" # Token for the production web server group
"yyyyyyyyyyyyyyyy" # Token for the internal systems group
)
BASE=https://rest.vuls.biz
# Stop early because the server list cannot be read without jq.
if ! command -v jq >/dev/null 2>&1; then
echo "jq was not found. Install jq before running this script."
exit 1
fi
# Call the FutureVuls API, storing the response body in BODY and the HTTP status in CODE.
# Pass the token through standard input to keep it out of the process list.
# If the transfer fails partway (curl exits with an error), treat it as HTTP 000.
api() {
local out
out=$(printf 'header = "Authorization: %s"\n' "$TOKEN" |
curl -q -s -K - --max-time 300 -X "$1" -w '\n%{http_code}' "$BASE$2") || out=$'\n000'
CODE=${out##*$'\n'}
BODY=${out%$'\n'*}
}
# Extract the error message returned by the API. If the body is not JSON, output it as is.
message() {
printf '%s' "$BODY" | jq -r '.message // .error // empty' 2>/dev/null || printf '%s' "$BODY"
}
ok=0
ng=0
unknown=0
listng=0
# For each token, retrieve the server list and scan the servers.
for TOKEN in "${TOKENS[@]}"; do
# Retrieve all pages of the server list. On failure, output the API message and move to the next token.
ids=()
names=()
page=1
while :; do
api GET "/v1/servers?limit=300&page=$page"
if [ "$CODE" != 200 ]; then
echo "Failed to retrieve the list of servers to scan: HTTP $CODE $(message)"
listng=$((listng+1))
continue 2
fi
while IFS=$'\t' read -r id name; do
ids+=("$id")
names+=("$name")
done < <(printf '%s' "$BODY" | jq -r '.servers[]? | "\(.id)\t\(.serverName | gsub("[\\n\\r\\t]"; " "))"' 2>/dev/null)
total=$(printf '%s' "$BODY" | jq -r '.paging.totalPage' 2>/dev/null)
case "$total" in
''|*[!0-9]*)
echo "Failed to retrieve the list of servers to scan: could not read the response"
listng=$((listng+1))
continue 2
;;
esac
[ "$page" -ge "$total" ] && break
page=$((page+1))
sleep 5
done
echo "Number of servers to scan: ${#ids[@]} servers"
# Scan the servers, waiting between servers to avoid hitting API rate limits.
for i in "${!ids[@]}"; do
sleep 5
api POST "/v1/server/scan/${ids[i]}"
# If an API rate limit (HTTP 429) is hit, wait 60 seconds and retry up to 3 times.
retry=0
while [ "$CODE" = 429 ] && [ "$retry" -lt 3 ]; do
retry=$((retry+1))
sleep 60
api POST "/v1/server/scan/${ids[i]}"
done
echo "Server name: ${names[i]} / Server ID: ${ids[i]} / HTTP $CODE $(message)"
case "$CODE" in
200) ok=$((ok+1)) ;;
000|504) unknown=$((unknown+1)) ;;
*) ng=$((ng+1)) ;;
esac
done
done
echo "Request accepted: $ok servers / Request failed: $ng servers / Unknown: $unknown servers / Tokens whose server list could not be retrieved: $listng"
Using PowerShell#
# Rescan all servers in a group one at a time using the FutureVuls API.
#
# [Before you start]
# - Prepare group tokens with the API permission "Read, Update" or higher.
#
# [Steps]
# 1. Create a scan-all-servers.ps1 file in a location of your choice, paste this script into it, and save it as UTF-8 with BOM.
# 2. Enter the group tokens in TOKENS below.
# 3. Open PowerShell and navigate to the folder where you saved this file.
# 4. Run it as follows.
# powershell -ExecutionPolicy Bypass -File .\scan-all-servers.ps1
# With many servers, results may scroll off the screen. To also save them to a file, run the following command.
# powershell -ExecutionPolicy Bypass -Command ".\scan-all-servers.ps1 | Tee-Object -FilePath scan-all-servers.log -Append"
# If a file with the same name exists, results are appended to it.
#
# [If an error occurs]
# - HTTP 000 (no response was received) and HTTP 504 (a relay server timed out) are counted as "Unknown". Processing may still be continuing on the FutureVuls side, so do not rescan immediately; after a while, check whether the server's "Last Detection Time" in the FutureVuls UI has been updated.
# - For how to handle other responses, see the table in "Operational Scripts Using the API" in the help.
#
# [Notes]
# - Running this script scans the target servers sequentially.
# - The script waits between servers to avoid hitting API rate limits.
# - Do not run multiple instances of this script at the same time.
# - Do not share this file containing tokens with others or upload it to Git or similar services.
# Enter one token per line, enclosed in ' ' (no commas are needed for multiple tokens).
$TOKENS = @(
'xxxxxxxxxxxxxxxx' # Token for the production web server group
'yyyyyyyyyyyyyyyy' # Token for the internal systems group
)
$BASE = 'https://rest.vuls.biz'
[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12
Add-Type -AssemblyName System.Net.Http
$handler = [Net.Http.HttpClientHandler]::new()
$handler.AllowAutoRedirect = $false
$handler.UseCookies = $false
$client = [Net.Http.HttpClient]::new($handler)
$client.Timeout = [TimeSpan]::FromSeconds(300)
# Call the FutureVuls API, storing the response body in BODY and the HTTP status in CODE.
function api($method, $path) {
$request = [Net.Http.HttpRequestMessage]::new([Net.Http.HttpMethod]::new($method), "$BASE$path")
$response = $null
try {
[void]$request.Headers.TryAddWithoutValidation('Authorization', $TOKEN)
$response = $client.SendAsync($request).GetAwaiter().GetResult()
$script:BODY = [Text.Encoding]::UTF8.GetString($response.Content.ReadAsByteArrayAsync().GetAwaiter().GetResult())
$script:CODE = [string][int]$response.StatusCode
} catch {
$script:CODE = '000'
$script:BODY = $_.Exception.Message
} finally {
if ($null -ne $response) { $response.Dispose() }
$request.Dispose()
}
}
# Extract the error message returned by the API. If the body is not JSON, output it as is.
function message {
try {
$json = ConvertFrom-Json -InputObject $BODY -ErrorAction Stop
if ($null -ne $json.message) { return $json.message }
return $json.error
} catch { return $BODY }
}
$ok = 0
$ng = 0
$unknown = 0
$listng = 0
# For each token, retrieve the server list and scan the servers.
:tokens foreach ($TOKEN in $TOKENS) {
# Retrieve all pages of the server list. On failure, output the API message and move to the next token.
$servers = @()
$page = 1
while ($true) {
api GET "/v1/servers?limit=300&page=$page"
if ($CODE -ne '200') {
Write-Output "Failed to retrieve the list of servers to scan: HTTP $CODE $(message)"
$listng++
continue tokens
}
try {
$json = ConvertFrom-Json -InputObject $BODY -ErrorAction Stop
} catch {
Write-Output "Failed to retrieve the list of servers to scan: could not read the response"
$listng++
continue tokens
}
foreach ($server in $json.servers) { $servers += $server }
if ($page -ge $json.paging.totalPage) { break }
$page++
Start-Sleep -Seconds 5
}
Write-Output "Number of servers to scan: $($servers.Count) servers"
# Scan the servers, waiting between servers to avoid hitting API rate limits.
foreach ($server in $servers) {
Start-Sleep -Seconds 5
api POST "/v1/server/scan/$($server.id)"
# If an API rate limit (HTTP 429) is hit, wait 60 seconds and retry up to 3 times.
$retry = 0
while ($CODE -eq '429' -and $retry -lt 3) {
$retry++
Start-Sleep -Seconds 60
api POST "/v1/server/scan/$($server.id)"
}
Write-Output "Server name: $($server.serverName) / Server ID: $($server.id) / HTTP $CODE $(message)"
if ($CODE -eq '200') { $ok++ }
elseif ($CODE -eq '000' -or $CODE -eq '504') { $unknown++ }
else { $ng++ }
}
}
Write-Output "Request accepted: $ok servers / Request failed: $ng servers / Unknown: $unknown servers / Tokens whose server list could not be retrieved: $listng"
$client.Dispose()