Add get_port() function

Find a free port from an available range.
This commit is contained in:
Martin Wimpress 2020-03-19 17:28:13 +00:00
parent 4d7910b868
commit 292686c3ca
No known key found for this signature in database
GPG Key ID: 61DF940515E06DA3

View File

@ -29,6 +29,19 @@ function vm_snapshot() {
exit 0 exit 0
} }
function get_port() {
local PORT_START=22220
local PORT_RANGE=9
while true; do
local CANDIDATE=$[${PORT_START} + (${RANDOM} % ${PORT_RANGE})]
(echo "" >/dev/tcp/127.0.0.1/${CANDIDATE}) >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "${CANDIDATE}"
break
fi
done
}
function vm_boot() { function vm_boot() {
local VMNAME=$(basename ${VM} .conf) local VMNAME=$(basename ${VM} .conf)
local BIOS="" local BIOS=""