|
|
@ -79,15 +79,16 @@ exports.tcping = function (hostname, port) {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Ping the specified machine
|
|
|
|
* Ping the specified machine
|
|
|
|
* @param {string} hostname Hostname / address of machine
|
|
|
|
* @param {string} hostname Hostname / address of machine
|
|
|
|
|
|
|
|
* @param {number} [size=56] Size of packet to send
|
|
|
|
* @returns {Promise<number>} Time for ping in ms rounded to nearest integer
|
|
|
|
* @returns {Promise<number>} Time for ping in ms rounded to nearest integer
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
exports.ping = async (hostname) => {
|
|
|
|
exports.ping = async (hostname, size = 56) => {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
return await exports.pingAsync(hostname);
|
|
|
|
return await exports.pingAsync(hostname, false, size);
|
|
|
|
} catch (e) {
|
|
|
|
} catch (e) {
|
|
|
|
// If the host cannot be resolved, try again with ipv6
|
|
|
|
// If the host cannot be resolved, try again with ipv6
|
|
|
|
if (e.message.includes("service not known")) {
|
|
|
|
if (e.message.includes("service not known")) {
|
|
|
|
return await exports.pingAsync(hostname, true);
|
|
|
|
return await exports.pingAsync(hostname, true, size);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
throw e;
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -98,14 +99,16 @@ exports.ping = async (hostname) => {
|
|
|
|
* Ping the specified machine
|
|
|
|
* Ping the specified machine
|
|
|
|
* @param {string} hostname Hostname / address of machine to ping
|
|
|
|
* @param {string} hostname Hostname / address of machine to ping
|
|
|
|
* @param {boolean} ipv6 Should IPv6 be used?
|
|
|
|
* @param {boolean} ipv6 Should IPv6 be used?
|
|
|
|
|
|
|
|
* @param {number} [size = 56] Size of ping packet to send
|
|
|
|
* @returns {Promise<number>} Time for ping in ms rounded to nearest integer
|
|
|
|
* @returns {Promise<number>} Time for ping in ms rounded to nearest integer
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
exports.pingAsync = function (hostname, ipv6 = false) {
|
|
|
|
exports.pingAsync = function (hostname, ipv6 = false, size = 56) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
ping.promise.probe(hostname, {
|
|
|
|
ping.promise.probe(hostname, {
|
|
|
|
v6: ipv6,
|
|
|
|
v6: ipv6,
|
|
|
|
min_reply: 1,
|
|
|
|
min_reply: 1,
|
|
|
|
deadline: 10,
|
|
|
|
deadline: 10,
|
|
|
|
|
|
|
|
packetSize: size,
|
|
|
|
}).then((res) => {
|
|
|
|
}).then((res) => {
|
|
|
|
// If ping failed, it will set field to unknown
|
|
|
|
// If ping failed, it will set field to unknown
|
|
|
|
if (res.alive) {
|
|
|
|
if (res.alive) {
|
|
|
|