Nixos-Configuration/packages/ip-gather/default.nix
Jermeiah S 2802db394f
All checks were successful
/ check (push) Successful in 1m3s
/ deploy (push) Has been skipped
feature: added ip-gather command for improved workflow
2025-06-22 17:15:45 -04:00

36 lines
941 B
Nix

# incus-ip-collector.nix
{
writeShellScriptBin,
}:
writeShellScriptBin "ip-gather" ''
set -euo pipefail
remote_host="''${1:-"administrator@olympus"}"
output_file="''${2:-container_ips.json}"
generate_json() {
echo "{"
first=true
for c in $(incus list --format csv -c n | awk '!/NAME/ {print $1}'); do
ip=$(incus exec "$c" -- ip -6 addr show dev ygg0 2>/dev/null | awk '/inet6/ {print $2}' | cut -d/ -f1 | grep -v '^fe80' | head -n1)
if [ -n "$ip" ]; then
[ "$first" = false ] && echo ","
echo -n " \"$c\": \"$ip\""
first=false
fi
done
echo ""
echo "}"
}
if [ -n "''${remote_host}" ]; then
# Execute remotely and save to local file
ssh "$remote_host" "$(declare -f generate_json); generate_json" > "$output_file"
else
# Execute locally and save to file
generate_json > "$output_file"
fi
echo "Container IPs saved to: $output_file"
''