Skip to content

Instantly share code, notes, and snippets.

@szampardi
Last active September 24, 2024 12:18
Show Gist options
  • Save szampardi/d162ef51c49a28337f527ec0b18873c3 to your computer and use it in GitHub Desktop.
Save szampardi/d162ef51c49a28337f527ec0b18873c3 to your computer and use it in GitHub Desktop.
openvpn status.log routing table to json
#!/usr/bin/env bash
ovpn_statuslog_json() {
local _status_log=${1:-"/var/log/openvpn/status.log"}
local _grep_routing_table='^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)'
local _sed_csv_lines_to_array='s/^/["/g;s/,/","/g;s/$/"]/g'
local _jq_template='{
"virtual_address": .[0],
"common_name": .[1],
"real_address": (.[2]|split(":")[0]),
"last_ref": (.[3]|strptime("%Y-%m-%d %H:%M:%S")|todate)
}'
grep -E "${_grep_routing_table}" "${_status_log}" | sed "${_sed_csv_lines_to_array}" | jq "${_jq_template}" | jq --slurp
}
if ! (return 0 2>/dev/null); then
ovpn_statuslog_json "${1}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment