前端有什么包/库可以做到ping的时候获取如下的一条一条地数据?
% ping baidu.com
PING baidu.com (39.156.66.10): 56 data bytes
64 bytes from 39.156.66.10: icmp_seq=0 ttl=47 time=44.301 ms
64 bytes from 39.156.66.10: icmp_seq=1 ttl=47 time=43.313 ms
64 bytes from 39.156.66.10: icmp_seq=2 ttl=47 time=42.916 ms
64 bytes from 39.156.66.10: icmp_seq=3 ttl=47 time=43.272 ms
64 bytes from 39.156.66.10: icmp_seq=4 ttl=47 time=49.445 ms
64 bytes from 39.156.66.10: icmp_seq=5 ttl=47 time=50.274 ms
64 bytes from 39.156.66.10: icmp_seq=6 ttl=47 time=43.728 ms
64 bytes from 39.156.66.10: icmp_seq=7 ttl=47 time=43.077 ms
64 bytes from 39.156.66.10: icmp_seq=8 ttl=47 time=43.127 ms
64 bytes from 39.156.66.10: icmp_seq=9 ttl=47 time=43.001 ms
^C
--- baidu.com ping statistics ---
10 packets transmitted, 10 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 42.916/44.645/50.274/2.642 ms
我经过寻找,有npm i ping
这样的包,但是不能获取如上的一条一条地数据,
var ping = require('ping');
var hosts = ['192.168.1.1', 'google.com', 'yahoo.com'];
hosts.forEach(function(host){
ping.sys.probe(host, function(isAlive){
var msg = isAlive ? 'host ' + host + ' is alive' : 'host ' + host + ' is dead';
console.log(msg);
});
});
直接获取到结果:
host yahoo.com is alive
host google.com is alive
host 192.168.1.1 is dead
但是没有我上面截图的效果,我想要直接进ping进而得到每个发出的ping包的返回结果。请问是否有这样的库呢?
你要在命令行下执行的话,直接用 child_process 的 spawn 就可以了呀