tcp-ping-port
A simple TCP ping utility to ping a port of an IP or domain.
Table of contents
Benefits
Following are the pros and cons of this package compared to the regular ping
Pros
- Can be used with Amazon Lambda functions and Aazure functions.
Regular ping
uses ICMP protocol which is not permitted by AWS/Azure functions - TCP packets have higher priorty than ICMP packets
- It can ping a particular port
- It can work even if ICMP is disabled
Cons
back to top
Installation
$ npm install tcp-ping-port --save
back to top
How to use
Simple examples
const { tcpPingPort } = require("tcp-ping-port")
tcpPingPort("google.com").then(online => {
})
With additional options
const { tcpPingPort } = require("tcp-ping-port")
const dns = require('dns')
const options = {
socketTimeout: 11000,
dnsTimeout: 10000,
dnsServers: '8.8.8.8',
}
tcpPingPort("google.com", 80, options).then(online => {
})
back to top
Dependencies
dns-fast-resolver
- to resolve multiple domains at the same time and to handle any errors while resolving the domain name. Also, to skip the whole dns resolution if host name is an IP address.lodash
maketype
back to top
API
tcpPingPort(hostname[, port, options, resolver])
Attemps to open and close TCP connection
- hostname
<string>
Host name or an IP address to try to connect to - port
<number>
Port number - options
<Object>
Socket and Resolver options
- socketTimeout
<integer>
Number of miliseconds to wait before the resolving request is canceled. Default value 4000 (4s). - dnsTimeout
<integer>
Number of miliseconds to wait before the resolving request is canceled. Default value 4000 (4s).
Usefull for rare cases when hostnames may need more than default 4 seconds to be resolved. - dnsServers
<string[]>
Array of RFC 5952 formatted addresses. (Example: ['4.4.4.4', '[2001:4860:4860::8888]', '4.4.4.4:1053', '[2001:4860:4860::8888]:1053']
)
- returns
<Promise>
Promise that will return online status of host
True
- when host is online and TCP connection is successfully opened and closedFalse
- when host name is not resolved or it failed to open a TCP connection
back to top
Testing
Run all tests
$ npm test
Run ping "google.com:80"
test
$ npm run unit-test -- --grep "google.com:80"
back to top