一个简单的 ping 工具。支持TCP/UDP/ICMP协议。
平格斯可以...
发送 ICMP 回显请求和 ttl 跟踪路由。
发送 ping、扫描 TCP/UDP 端口以及抓取横幅。
使用 UDP ping 发送魔术包(LAN 唤醒)。
pingus.Ping'ready''result''error'ping.send()pingus.PingTCP扩展: pingus.Pingnew PingTCP(options)pingtcp.send()pingtcp.scan()pingus.PingUDP扩展: pingus.Pingnew PingUDP(options)pingudp.send()pingudp.scan()pingus.PingICMP扩展: pingus.Pingnew PingICMP(options)pingicmp.send()pingicmp.traceroute()Callback或Promise ( async/await )pingus.tcp(options[, callback])pingus.tcpscan(options[, callback])pingus.udp(options[, callback])pingus.udpscan(options[, callback])pingus.wol(mac, options[, callback])pingus.icmp(options[, callback])pingus.traceroute(options[, callback]) npm i pingus
如果安装过程中出现错误,可能是由于以下软件包安装失败造成的。
可以通过安装GCC编译器来修复。
在Ubuntu中:
sudo apt-get install -y build-essential在 Windows 中:
安装Visual Studio包括Desktop development with C++ 。
// TCP Ping to localhost:22
import pingus from 'pingus' ; // ESM, Typescript
const pingus = require ( 'pingus' ) ; // CJS
pingus . tcp ( { host : 'localhost' , port : 22 } ) . then ( console . log ) ; // Result
{
type : 'ping/tcp' ,
status : 'open' ,
host : 'localhost' ,
ip : IP { label : '127.0.0.1' } ,
ips : [ IP { label : '127.0.0.1' } ] ,
time : 2 ,
port : 22 ,
name : 'ssh' ,
banner : 'SSH-2.0-OpenSSH_8.9p1 Ubuntu-3'
toPrimitiveJSON : [ Function ( anonymous ) ]
}pingus.Ping pingus.Ping是具有以下事件的EventEmitter :
'ready'result <Object>当准备好(解析 DNS、过滤 Bogon IP)在调用ping.send()后发送 ping 时发出。
import pingus from 'pingus' ;
const ping = new pingus . PingTCP ( {
host : 'example.com' ,
} ) ;
ping . on ( 'ready' , ( result ) => {
const data = result . toPrimitiveJSON ( ) ;
console . log ( 'pingttarget:t' , data . host ) ;
console . log ( 'tips:t' , data . ips ) ;
} ) ;
ping . send ( ) ; ping target: example.com
ips: [ '93.184.215.14', '2606:2800:021f:cb07:6820:80da:af6b:8b2c' ]
'result'result <Object>ping 数据的结果。
import pingus from 'pingus' ;
const ping = new pingus . PingTCP ( {
host : 'example.com' ,
} ) ;
ping . on ( 'result' , ( result ) => {
const data = result . toPrimitiveJSON ( ) ;
console . log ( result ) ;
} ) ;
ping . send ( ) ; {
type : 'ping/tcp' ,
status : 'open' ,
host : 'example.com' ,
ip : '93.184.215.14' ,
ips : [ '93.184.215.14' , '2606:2800:021f:cb07:6820:80da:af6b:8b2c' ] ,
time : 134 ,
port : 80 ,
name : 'http' ,
banner : ''
}'error'<Error>result <Object>发生错误时发出。 result有错误发生前的最后一条语句和错误代码。
ping.send()发送 ping。请参阅用法中的一些示例
pingus.PingTCP扩展: pingus.Ping TCP ping 类。
pingus.PingTCP是pingus.Ping的类型
new PingTCP(options)options <Object>host <string>设置目标主机名(域)或 IP 地址。port <number>使用pingtcp.send()设置目标端口。默认值: 80ports <Array> | <string>使用pingtcp.scan()时设置目标端口。使用端口号或查询字符串的数组。参见示例。timeout <number>设置超时。默认值: 2000resolveDNS <boolean>当host为域地址时解析 DNS A和AAAA记录。默认值: truednsServer <string>设置 DNS 服务器来解析 DNS 记录。filterBogon <boolean>过滤host中的 bogon IP 地址。默认值: true options.ports的示例ports: [ 21 , 22 , 80 , 443 ] ; // Scan port 21, 22, 80, 443 ports: '21,22,80,443' ; // Scan port 21, 22, 80, 443 ports: '21-80' ; // Scan ports in range 21 to 80 (21, 22, 23 ... 78, 79, 80) ports: '21-25,80,443' ; // Scan ports in range 21 to 25 and 80, 443 ports: '@' ; // Scan most used 1024 ports in protocol ports: '*' ; // Scan all ports (1 to 65535)pingtcp.send()请参阅ping.send() 。使用中的一些示例。
pingtcp.scan()使用 TCP ping 扫描端口。返回事件结果: 'result' 。请参阅用法中的一些示例。
pingus.PingUDP扩展: pingus.Ping UDP ping 类。
pingus.PingUDP是pingus.Ping的类型
new PingUDP(options)options <Object>host <string>设置目标主机名(域)或 IP 地址。port <number>使用pingudp.send()时设置目标端口。默认值: 68ports <Array> | <string>使用pingudp.scan()时设置目标端口。使用端口号或查询字符串的数组。与 PingTCP 相同。参见示例。buffer <Buffer>设置在已连接的 UDP ping 套接字上发送时的缓冲区。body <string>在已连接的 UDP ping 套接字上发送时设置正文。设置buffer选项时将被忽略。bytes <number>设置在已连接的 UDP ping 套接字上发送时的随机字节长度。设置body选项时将被忽略。默认值: 32timeout <number>设置超时。默认值: 2000resolveDNS <boolean>当host为域地址时解析 DNS A和AAAA记录。默认值: truednsServer <string>设置 DNS 服务器来解析 DNS 记录。filterBogon <boolean>过滤host中的 bogon IP 地址。默认值: truepingudp.send()请参阅ping.send() 。使用中的一些示例。
pingudp.scan()与pingtcp.scan()类似。
使用 UDP ping 扫描端口。返回事件结果: 'result' 。请参阅用法中的一些示例。
pingus.PingICMP扩展: pingus.Ping ICMP ping 类。
pingus.PingICMP是pingus.Ping的类型
new PingICMP(options)options <Object>host <string>设置目标主机名(域)或 IP 地址。ttl <number>设置 ttl。默认值: 128ttln <number>设置使用pingicmp.traceroute()时的起始 ttl。默认值: 1ttlx <number>使用pingicmp.traceroute()时设置最大 ttl。默认值: 64timeout <number>设置超时。默认值: 2000timeoutx <number>使用pingicmp.traceroute()时设置最大超时堆栈。默认值: 8resolveDNS <boolean>当host为域地址时解析 DNS A和AAAA记录。默认值: truednsServer <string>设置 DNS 服务器来解析 DNS 记录。filterBogon <boolean>过滤host中的 bogon IP 地址。默认值: truepingicmp.send()请参阅ping.send() 。使用中的一些示例。
pingicmp.traceroute()运行跟踪路由。使用中的一些示例。
Callback或Promise ( async/await )请参阅发送 Ping 样式中的示例。
pingus.tcp(options[, callback])发送 TCP ping。
pingus.tcpscan(options[, callback])使用 TCP ping 扫描端口。
pingus.udp(options[, callback])发送 UDP ping。
pingus.udpscan(options[, callback])使用 UDP ping 扫描端口。
pingus.wol(mac, options[, callback])mac <string>设置目标 MAC 地址。options <Object>与new PingUDP(options)的选项相同发送魔术包 UDP ping 以使用 WOL 功能。
pingus.icmp(options[, callback])发送 ICMP ping。
pingus.traceroute(options[, callback])运行跟踪路由。
ESM(打字稿)
import pingus from 'pingus' ;中山大学
const pingus = require ( 'pingus' ) ; Class扩展EventEmitter // TCP ping to localhost:80
new pingus . PingTCP ( { host : 'localhost' } )
. on ( 'result' , ( result ) => {
console . log ( result . toPrimitiveJSON ( ) ) ;
} )
. on ( 'error' , ( err , result ) => {
throw err ;
} )
. send ( ) ;Callback // TCP ping to localhost:80
pingus . tcp ( { host : 'localhost' } , ( err , result ) => {
if ( err ) {
throw err ;
}
console . log ( result ) ;
} ) ;Promise // TCP ping to localhost:80
pingus
. tcp ( { host : 'localhost' } )
. then ( ( result ) => {
console . log ( result . toPrimitiveJSON ( ) ) ;
} )
. catch ( ( err ) => {
throw err ;
} ) ;async/await // TCP ping to localhost:80
const result = await pingus . tcp ( { host : 'localhost' } ) ;
console . log ( result . toPrimitiveJSON ( ) ) ; {
type : 'ping/tcp' ,
status : 'open' ,
host : 'localhost' ,
ip : '127.0.0.1' ,
ips : [ '127.0.0.1' ] ,
time : 2 ,
port : 80 ,
name : 'http' ,
banner : ''
} // TCP ping to localhost:22
new pingus . PingTCP ( { host : 'localhost' , port : 22 } )
. on ( 'result' , ( result ) => {
console . log ( result . toPrimitiveJSON ( ) ) ;
} )
. on ( 'error' , ( err , result ) => {
throw err ;
} )
. send ( ) ; {
type : 'ping/tcp' ,
status : 'open' ,
host : 'localhost' ,
ip : '127.0.0.1' ,
ips : [ '127.0.0.1' ] ,
time : 2 ,
port : 22 ,
name : 'ssh' ,
banner : 'SSH-2.0-OpenSSH_8.9p1 Ubuntu-3'
} // TCP ping scan to localhost
new pingus . PingTCP ( {
host : 'localhost' ,
ports : [ 21 , 22 , 80 , 443 , 3306 , 8080 ] ,
} )
. on ( 'result' , ( result ) => {
console . log ( result . toPrimitiveJSON ( ) ) ;
} )
. on ( 'error' , ( err , result ) => {
throw err ;
} )
. scan ( ) ; {
type : 'ping/tcp/scan' ,
status : 'finish' ,
host : 'localhost' ,
ip : '127.0.0.1' ,
ips : [ '127.0.0.1' ] ,
time : 2009 ,
port : 80 ,
name : 'http' ,
banner : '' ,
ports : [ 21 , 22 , 80 , 443 , 3306 , 8080 ] ,
statuses : {
open : [ 22 , 80 , 8080 ] ,
reset : [ ] ,
close : [ 21 , 443 , 3306 ] ,
filtered : [ ] ,
error : [ ]
} ,
names : {
'21' : 'ftp' ,
'22' : 'ssh' ,
'80' : 'http' ,
'443' : 'https' ,
'3306' : 'mysql' ,
'8080' : 'http-alt'
} ,
banners : { '22' : 'SSH-2.0-OpenSSH_8.9p1 Ubuntu-3' } ,
errors : { }
} // UDP ping to localhost:19132
new pingus . PingUDP ( { host : 'localhost' , port : 19132 } )
. on ( 'result' , ( result ) => {
console . log ( result . toPrimitiveJSON ( ) ) ;
} )
. on ( 'error' , ( err , result ) => {
throw err ;
} )
. send ( ) ; {
type : 'ping/udp' ,
status : 'close' ,
host : 'localhost' ,
ip : '127.0.0.1' ,
ips : [ '127.0.0.1' ] ,
time : 2 ,
port : 19132 ,
name : 'minecraft-be'
} // UDP ping scan to localhost
new pingus . PingUDP ( {
host : 'localhost' ,
ports : [ 67 , 68 , 161 , 162 , 445 ] ,
} )
. on ( 'result' , ( result ) => {
console . log ( result . toPrimitiveJSON ( ) ) ;
} )
. on ( 'error' , ( err , result ) => {
throw err ;
} )
. scan ( ) ; {
error : undefined ,
type : 'ping/udp/scan' ,
status : 'finish' ,
host : 'localhost' ,
ip : '127.0.0.1' ,
ips : [ '127.0.0.1' ] ,
time : 2003 ,
ports : [ 67 , 68 , 161 , 162 , 445 ] ,
statuses : {
open : [ 68 ] ,
reset : [ ] ,
close : [ 67 , 161 , 162 , 445 ] ,
filtered : [ ] ,
error : [ ]
} ,
names : {
'67' : 'bootps' ,
'68' : 'bootpc' ,
'161' : 'snmp' ,
'162' : 'snmptrap' ,
'445' : 'microsoft-ds'
} ,
banners : { } ,
errors : { }
} // Send magic packet using UDP ping to 00-00-00-00-00-00
pingus
. wol ( '00-00-00-00-00-00' )
. then ( ( result ) => {
console . log ( result . toPrimitiveJSON ( ) ) ;
} )
. catch ( ( error ) => {
throw error ;
} ) ; {
type : 'ping/udp' ,
status : 'open' ,
host : '255.255.255.255' ,
ip : '255.255.255.255' ,
ips : [ '255.255.255.255' ] ,
time : 2 ,
port : 9 ,
name : 'discard'
} // ICMP ping to example.com
new pingus . PingICMP ( { host : 'example.com' } )
. on ( 'result' , ( result ) => {
console . log ( result . toPrimitiveJSON ( ) ) ;
} )
. on ( 'error' , ( err , result ) => {
throw err ;
} )
. send ( ) ; {
type : 'ping/icmp' ,
status : 'reply' ,
host : 'example.com' ,
ip : '93.184.215.14' ,
ips : [ '93.184.215.14' , '2606:2800:021f:cb07:6820:80da:af6b:8b2c' ] ,
time : 130 ,
ttl : 128 ,
bytes : 32 ,
reply : {
source : '93.184.216.34' ,
type : 0 ,
code : 0 ,
typestr : 'ECHO_REPLY' ,
codestr : 'NO_CODE' ,
body : '767284c4'
}
} // ICMP ping to example.com using ttl = 10
new pingus . PingICMP ( { host : 'example.com' , ttl : 10 } )
. on ( 'result' , ( result ) => {
console . log ( result . toPrimitiveJSON ( ) ) ;
} )
. on ( 'error' , ( err , result ) => {
throw err ;
} )
. send ( ) ; {
type : 'ping/icmp' ,
status : 'exception' ,
host : 'example.com' ,
ip : '93.184.215.14' ,
ips : [ '93.184.215.14' , '2606:2800:021f:cb07:6820:80da:af6b:8b2c' ] ,
time : 133 ,
ttl : 10 ,
bytes : 32 ,
reply : {
source : '152.195.76.133' ,
type : 11 ,
code : 0 ,
typestr : 'TIME_EXCEEDED' ,
codestr : 'NO_CODE' ,
body : ']8X"bx00CQx00x00x00x00'
}
} // Traceroute to example.com
new pingus . PingICMP ( { host : 'example.com' , timeout : 500 } )
. on ( 'result' , ( result ) => {
console . log ( result . toPrimitiveJSON ( ) ) ;
} )
. on ( 'error' , ( err , result ) => {
throw err ;
} )
. traceroute ( ) ; {
type : 'ping/icmp/traceroute' ,
status : 'finish' ,
host : 'example.com' ,
ip : '93.184.215.14' ,
ips : [ '93.184.215.14' , '2606:2800:021f:cb07:6820:80da:af6b:8b2c' ] ,
time : 7614 ,
ttl : 128 ,
bytes : 32 ,
hops : [
{
status : 'time_exceeded' ,
ip : '172.19.80.1' ,
ttl : 1 ,
rtt : { min : 0 , max : 1 , avg : 1 }
} ,
{
status : 'time_exceeded' ,
ip : '172.30.1.254' ,
ttl : 2 ,
rtt : { min : 0 , max : 1 , avg : 1 }
} ,
{
status : 'timeout' ,
ip : null ,
ttl : 3 ,
rtt : { min : 2001 , max : 2002 , avg : 2002 }
} ,
{
status : 'time_exceeded' ,
ip : '112.188.59.77' ,
ttl : 4 ,
rtt : { min : 2 , max : 3 , avg : 3 }
} ,
{
status : 'time_exceeded' ,
ip : '112.188.53.13' ,
ttl : 5 ,
rtt : { min : 1 , max : 2 , avg : 2 }
} ,
{
status : 'time_exceeded' ,
ip : '112.174.47.177' ,
ttl : 6 ,
rtt : { min : 7 , max : 8 , avg : 8 }
} ,
{
status : 'time_exceeded' ,
ip : '112.174.91.130' ,
ttl : 7 ,
rtt : { min : 7 , max : 8 , avg : 8 }
} ,
{
status : 'time_exceeded' ,
ip : '112.174.87.102' ,
ttl : 8 ,
rtt : { min : 129 , max : 130 , avg : 130 }
} ,
{
status : 'time_exceeded' ,
ip : '206.72.210.112' ,
ttl : 9 ,
rtt : { min : 127 , max : 128 , avg : 128 }
} ,
{
status : 'time_exceeded' ,
ip : '152.195.76.151' ,
ttl : 10 ,
rtt : { min : 132 , max : 134 , avg : 133 }
} ,
{
status : 'reply' ,
ip : '93.184.215.14' ,
ttl : 11 ,
rtt : { min : 126 , max : 126 , avg : 126 }
}
]
}