From d1bcc3955ab31da24291f026bd7668dabdb2890d Mon Sep 17 00:00:00 2001 From: Kroese Date: Mon, 8 May 2023 19:32:46 +0200 Subject: [PATCH] Retrieve IP from VM --- run/server.sh | 48 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 8 deletions(-) diff --git a/run/server.sh b/run/server.sh index ae3c6ae..34fb06b 100644 --- a/run/server.sh +++ b/run/server.sh @@ -1,17 +1,49 @@ #!/usr/bin/env bash set -eu -trap 'kill 0' EXIT +trap 'pkill -f nc || true' EXIT trap exit SIGINT SIGTERM -# Serve the page -HTML="VirtualDSM

$2

" +if [ "$2" != "ipinfo" ]; then -LENGTH="${#HTML}" + # Serve the page + HTML="VirtualDSM

$2

" -RESPONSE="HTTP/1.1 200 OK\nContent-Length: ${LENGTH}\nConnection: close\n\n$HTML\n\n" + LENGTH="${#HTML}" + RESPONSE="HTTP/1.1 200 OK\nContent-Length: ${LENGTH}\nConnection: close\n\n$HTML\n\n" -while true; do - echo -en "$RESPONSE" | nc -lp "${1:-5000}" & wait $! + while true; do + echo -en "$RESPONSE" | nc -lp "${1:-5000}" & wait $! + done + + exit 0 + +fi + +SH_SCRIPT="/run/ipinfo.sh" + +{ echo "INFO=\$(curl -s -m 5 -S http://127.0.0.1:2210/read?command=10 2>&1)" + echo "rest=\${INFO#*http_port}" + echo "rest=\${rest#*:}" + echo "rest=\${rest%%,*}" + echo 'PORT=\${rest%%\"*}' + echo "rest=\${INFO#*eth0}" + echo "rest=\${rest#*ip}" + echo "rest=\${rest#*:}" + echo 'rest=\${rest#*\"}' + echo 'IP=\${rest%%\"*}' + echo "BODY=\"The location of DSM is http://\${IP}:\${PORT}\"" + echo "HTML=\"VirtualDSM

\$BODY

\"" + echo 'LENGTH="\${#HTML}"' + echo 'RESPONSE="HTTP/1.1 200 OK\nContent-Length: \${LENGTH}\nConnection: close\n\n\$HTML\n\n"' + echo 'echo \$RESPONSE' +} > "$SH_SCRIPT" + +chmod +x "$SH_SCRIPT" + +while true ; do + nc -lp "${1:-5000}" -e "$SH_SCRIPT" & wait $! done