Check if some tcp endpoint (or many) exists. Can be used as a port scanner
- Zero-dependency
- Small — just 3 functions
- Fast — scans
65536
endpoints in~9sec
(via tcpExistsMany) - ESM and CJS
npm i -g tcp-exists
tcp-exists --help # print full cli docs
tcp-exists example.com # scan 20 most popular ports for given host
tcp-exists example.com:22,80,443,8000-10000,27017 # example how to provide list/ranges of ports
tcp-exists example.com:22 another.org:1-65535 # example how to scan several endpoints
npm i tcp-exists --save
Arguments:
host
<string>
port
<string> | <number>
timeout
<number>
- optional number ofms
. Default:DEFAULT_TIMEOUT
signal
<AbortSignal>
- optional. An AbortSignal that may be used to close a socket and return result ASAP.
Returns:
<Promise<boolean>>
import { tcpExistsOne } from 'tcp-exists'
const exist = await tcpExistsOne('8.8.8.8', 53, 25)
// check existance of endpoint 8.8.8.8:53 with timeout in 25ms
console.log(exist) // true
It is an async function to check multiple endpoints. If size of endpoints you want to check more than 4096
then recommended to use generator function tcpExistsMany
or increase timeout
.
endpoints
<[string, string|number][]>
- array of[host, port]
options
<object>
- optionaltimeout
<number>
- optional number ofms
to execute on chunk. How to pick the best timeout Default:DEFAULT_TIMEOUT
returnOnlyExisted
<boolean>
- optional flag to exclude all non-existed results. Default:true
signal
<AbortSignal>
- optional. An AbortSignal that may be used to close a sockets and return result ASAP.
<Promise<[string, string|number, boolean][]>>
- will returnarray
of[host, port, existed]
import { tcpExistsChunk } from 'tcp-exists'
const endpoints = [
['8.8.8.8', 53],
['8.8.8.8', 80],
['8.8.8.8', 443],
['8.8.8.8', 8080]
]
const result = await tcpExistsChunk(endpoints)
console.log(result)
// all existed endpoints in format [host, port, existed][]
It is an async generator. So you can use it with for await (... of ...)
or as a stream (check nodejs documentation).
Useful to use with large amount of endpoints.
endpoints
<[string, string|number][]|string>
- array of[host, port]
or string in formathost:port,port2; host2; host3:port0-port9
options
<object>
- optionalchunkSize
<number>
- optional chunk size of endpoints to process at once. Default:DEFAULT_CHUNK_SIZE
timeout
<number>
- optional number ofms
to execute on chunk. How to pick the best timeout Default:DEFAULT_TIMEOUT
returnOnlyExisted
<boolean>
- optional flag to exclude all non-existed results. Default:true
signal
<AbortSignal>
- optional. An AbortSignal that may be used to close a sockets, stop iteration and return last chunk result ASAP.
<AsyncIterable<[host:string, port:string|number, exist:boolean][]>>
- generator will yieldarray
of[host, port, existed]
import { tcpExistsMany } from 'tcp-exists'
const result = []
for await (const existedEndpoints of tcpExistsMany('localhost:1-65535')) {
result.push(...existedEndpoints)
}
console.log(result)
// all existed endpoints in format [host, port, existed][]
It is a generator. So you can use it with for (... of ...)
or destruct into array [...getEndpoints('example.com:1-65535')]
argument
<string|string[]>
- string in formathost:port,port2; host2; host3:port0-port9
or array like this['host1', 'host2:port1,port2', 'host3:port0-port9']
defaultPorts
<string>
- optional. Comma separated string of ports. Default:DEFAULT_PORTS
<Generator<[string, string|number]>>
- generator will yieldarray
of[host, port]
import { getEndpoints, tcpExistsOne } from 'tcp-exists'
for (const [host, port] of getEndpoints('localhost:1-65535')) {
if (await tcpExistsOne(host, port)) console.log(host, port, 'exists')
}
<number>
:2300
<number>
:250
ms
<string>
:'21,22,23,25,53,80,110,111,135,139,143,443,445,993,995,1723,3306,3389,5900,8080'
The best timeout usually is the ninth of the endpoint's size, but at least 100ms
.
For example, better to pick timeout as 220ms
for a chunk of 2000
endpoints.
Also, you can take into account latency to the endpoint.
If your endpoint has latency 300ms
then better to pick timeout as 350ms
and chunkSize as 3100
.
There is better alternative of port scanner to use in shell written in rust RustScan (scans 65536 ports in 3s)
License (MIT)