#!/bin/bash

# version 0.0.4

if [ "$#" -ne 1 ]; then
  >&2 echo 'usage: tinc-graph $net_name'
  exit 1
fi

. '/etc/tinc-extras.conf'

subnet="${subnets["$1"]:?}"
url="${urls["$1"]:?}"
interface="$1"

hosts=$(
  nmap -oG - -sn "${subnet}" \
  | awk '$1 == "Host:" && $4 == "Status:" && $5 == "Up" {print $2}' \
  | sed '
    s@.*@"\0"@
    $! s@$@,@
  ' \
  | tr -d '\n'
)
curl -Ssk -4 -X POST --data "hosts=[${hosts}]" --interface "${interface}" "${url}"
