virtual-dsm/src/print.sh

67 lines
1.4 KiB
Bash
Raw Normal View History

2023-11-08 03:31:19 +01:00
#!/usr/bin/env bash
set -Eeuo pipefail
2023-11-08 12:07:38 +01:00
info () { echo -e >&2 "\E[1;34m\E[1;36m $1\E[0m" ; }
2023-11-08 03:44:12 +01:00
error () { echo -e >&2 "\E[1;31m ERROR: $1\E[0m" ; }
2023-11-08 03:31:19 +01:00
file="/run/dsm.url"
2023-11-08 03:31:19 +01:00
while [ ! -f "$file" ]
2023-11-08 04:12:09 +01:00
do
2023-11-08 03:44:12 +01:00
2023-11-11 17:06:56 +01:00
sleep 3
[ -f "$file" ] && continue
2023-11-08 12:07:38 +01:00
2023-11-08 04:12:09 +01:00
# Retrieve IP from guest VM
2023-11-08 12:07:38 +01:00
set +e
RESPONSE=$(curl -s -m 30 -S http://127.0.0.1:2210/read?command=10 2>&1)
2023-11-08 12:07:38 +01:00
set -e
2023-11-08 03:44:12 +01:00
2023-11-08 04:12:09 +01:00
if [[ ! "${RESPONSE}" =~ "\"success\"" ]] ; then
2023-11-08 04:21:07 +01:00
error "Failed to connect to guest: $RESPONSE" && continue
2023-11-08 04:12:09 +01:00
fi
2023-11-08 03:44:12 +01:00
2023-11-08 04:12:09 +01:00
# Retrieve the HTTP port number
if [[ ! "${RESPONSE}" =~ "\"http_port\"" ]] ; then
2023-11-08 04:21:07 +01:00
error "Failed to parse response from guest: $RESPONSE" && continue
2023-11-08 04:12:09 +01:00
fi
2023-11-08 03:44:12 +01:00
2023-11-08 04:12:09 +01:00
rest=${RESPONSE#*http_port}
rest=${rest#*:}
rest=${rest%%,*}
PORT=${rest%%\"*}
2023-11-08 03:44:12 +01:00
2023-11-08 04:21:07 +01:00
[ -z "${PORT}" ] && continue
2023-11-08 04:12:09 +01:00
# Retrieve the IP address
if [[ ! "${RESPONSE}" =~ "eth0" ]] ; then
2023-11-08 04:21:07 +01:00
error "Failed to parse response from guest: $RESPONSE" && continue
2023-11-08 04:12:09 +01:00
fi
rest=${RESPONSE#*eth0}
rest=${rest#*ip}
rest=${rest#*:}
rest=${rest#*\"}
IP=${rest%%\"*}
2023-11-08 04:21:07 +01:00
[ -z "${IP}" ] && continue
2023-11-08 03:44:12 +01:00
echo "${IP}:${PORT}" > $file
2023-11-08 03:44:12 +01:00
2023-11-08 04:12:09 +01:00
done
2023-11-08 03:31:19 +01:00
LOCATION=$(cat "$file")
if [[ "$LOCATION" == "20.20"* ]]; then
MSG="port ${LOCATION##*:}"
2023-11-08 03:31:19 +01:00
else
MSG="http://${LOCATION}"
2023-11-08 03:31:19 +01:00
fi
2023-11-08 12:07:38 +01:00
echo "" >&2
2023-11-08 03:31:19 +01:00
info "--------------------------------------------------------"
info " You can now login to DSM at ${MSG}"
info "--------------------------------------------------------"
2023-11-08 12:07:38 +01:00
echo "" >&2