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 <navycat@ultrasparc>
This commit is contained in:
navycatt 2021-10-10 00:27:47 +03:00 committed by GitHub
parent d1afc10857
commit 018fb1c454
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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