1

With this scripts i check if a host is response on a given port with the bash's built-in /dev/tcp.

I can use ip address or domains(hostnames).

Script 1

#!/bin/bash

HOST_NAME="127.1"
HOST_PORT="80"

if ( (exec 3<>/dev/tcp/${HOST_NAME}/${HOST_PORT}) 2> /dev/null); then
    echo -e "PORT: ${HOST_PORT} | ON"
    else
    echo -e "PORT: ${HOST_PORT} | OFF"
fi

exit;

Script 2

#!/bin/bash

HOST_NAME="127.1"

for HOST_PORT in {1..1000}
do

if ( (exec 3<>/dev/tcp/${HOST_NAME}/${HOST_PORT}) 2> /dev/null); then
    echo -e "PORT: ${HOST_PORT} | ON"
    else
    echo -e "PORT: ${HOST_PORT} | OFF"
fi
done

exit;

Script 3

#!/bin/bash

HOST_NAME="127.1"
declare -A PORT_ON

for HOST_PORT in {1..65535}
do
    if ( (exec 3<>/dev/tcp/${HOST_NAME}/${HOST_PORT}) 2> /dev/null); then
        PORT_ON[${HOST_PORT}]="ON"
    fi
done

for i in ${!PORT_ON[*]}
do
    echo -e "$i : ${PORT_ON[$i]}"
done

exit;

On some of our local and online domains Script 3 hangs on some ports and takes longer to jump to the next port scan, for example on ssh(because of firewalls or other services). How can i manage this that it continues straight away and is it possible to speed the script up, it takes a long time, when i scan all 65535 ports an.

My GNU/Linux Distro:

Distributor ID: Debian

Description:    Debian GNU/Linux 10 (buster)

Release:        10

Codename:       buster

4.19.0-16-amd64

My /etc/apt/sources.list

deb http://security.debian.org/debian-security buster/updates main contrib

deb-src http://security.debian.org/debian-security buster/updates main contrib

deb http://deb.debian.org/debian/ buster-updates main contrib

deb-src http://deb.debian.org/debian/ buster-updates main contrib

I can only install from this repos.

bash script, check port response with bash's built-in /dev/tcp, time delay | hangs | speed up

how can i speed up and terminate the time delay?

0

1 Answer 1

4

Run this with timeout.

I put a timer varible to check the running time.

It seems you can't speed up the scritp more than 0.01.

If this scirpt is executed with 0.001, you will only see the reslut of the timer varible but no other reslut.

Even with 1, 0.1 or 0.01, you will always get the same reslut.

I tested this scirpt with Ubuntu 22.04 and set the ports to 10000.

#!/bin/bash

starttime=$(date +%s)
HOST_NAME="127.0.0.1"
declare -A PORT_ON

for HOST_PORT in {1..10000}
do
 if((timeout 0.1 bash -c "</dev/tcp/${HOST_NAME}/${HOST_PORT}") 2> /dev/null); then
    PORT_ON[${HOST_PORT}]="ON";
  fi
done

for i in ${!PORT_ON[*]}
do
  echo -e "$i: ${PORT_ON[$i]}"
done

endtime=$(date +%s)

echo "runtime: $(($endtime - $starttime))"
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .