From 018fb1c454545795b271206a11ef6445178f52b0 Mon Sep 17 00:00:00 2001 From: navycatt <92228311+navycatt@users.noreply.github.com> Date: Sun, 10 Oct 2021 00:27:47 +0300 Subject: [PATCH] Improvments of port scanning. (#79) * Fix port scan. Don't send a new line character when scanning. * Fix port scanning. Possible infinite loop when all range of ports is busy. Changed port scanning scheme from random to linear because this is the same faster but more predictable. * Fix port scan. Don't send a new line character when scanning. echo -n "" for clarity instead of cat < /dev/null Co-authored-by: navycat --- quickemu | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/quickemu b/quickemu index cddc36f..d8fec09 100755 --- a/quickemu +++ b/quickemu @@ -84,12 +84,12 @@ function snapshot_info() { function get_port() { local PORT_START=$1 - local PORT_RANGE=$2 - while true; do - local CANDIDATE=$((PORT_START + (RANDOM % PORT_RANGE))) - (echo "" >/dev/tcp/127.0.0.1/${CANDIDATE}) >/dev/null 2>&1 + local PORT_RANGE=$((PORT_START+$2)) + local PORT + for ((PORT = PORT_START; PORT <= PORT_RANGE; PORT++)); do + (echo -n "" >/dev/tcp/127.0.0.1/${CANDIDATE}) >/dev/null 2>&1 if [ ${?} -ne 0 ]; then - echo "${CANDIDATE}" + echo "${PORT}" break fi done