在node中应用了一个ping类库,也就用了,以为是socket封装,之前一波学习ping之后突然想起来看看。一翻,失望了。基于系统ping。我们看下关键代码:
node-ping
关键代码
/** * Return a path to the ping executable in the system * @param {string} platform - Name of the platform * @param {bool} v6 - Ping via ipv6 or not * @return {string} - Executable path for system command ping * @throw if given platform is not supported */factory.getExecutablePath = function (platform, v6) { if (!this.isPlatformSupport(platform)) { throw new Error(util.format('Platform |%s| is not support', platform)); } var ret = null; if (platform === 'aix') { ret = '/usr/sbin/ping'; } else if (factory.isLinux(platform)) { ret = v6 ? 'ping6' : 'ping'; } else if (factory.isWindow(platform)) { ret = process.env.SystemRoot + '/system32/ping.exe'; } else if (factory.isMacOS(platform)) { ret = v6 ? '/sbin/ping6' : '/sbin/ping'; } return ret; };
不过这只能怪我的理所当然,没仔细去读readme,毕竟人家有写着呢。
DESCRIPTION
node-ping is a simple wrapper for the system ping utility
http-ping
pingjs/ping.js
关键代码
没差别。都是是前端http-ping,方法是img.load
// https://github.com/alfg/ping.js/blob/master/src/ping.js 23 line var self = this; self.wasSuccess = false; self.img = new Image(); self.img.onload = onload; self.img.onerror = onerror; var timer; var start = new Date(); function onload(e) { self.wasSuccess = true; pingCheck.call(self, e); }
node-net-ping
nospaceships/node-net-ping
这应该是我要的库,基于raw-socket。支持ipv4和ipv6(有空我也去抄一波)。如果要学习写ping的,习惯js的可以学习下这个项目。
区别
http-ping
前端浏览器使用,毕竟没有太多权限。缺点就是ping是ip包的一部分,而http-ping是ip包里包TCP里包HTTP,可参考但数据偏大。sys-ping
node中去使用sys-ping那基本就是进程process的使用了,缺点呢,系统内置的1s延迟。嗯。。。自己体会。sokcet-ping
等于造轮子,缺点的话就是复杂,而且node的封装可能有一点点性能损失,但优点是可以更灵活的去控制各个环节,自己的世界爱怎么写怎么写。
场景建议:
如果是检测数量不多服务器是否在线。三者都可,推荐前2种。
如果是ip段扫描,建议3
如果需要类似traceroute功能的,建议3
补充
今天发现孤陋寡闻了,linux 中的ping 和win 的还是有差别,比如linux 中,-f 和 -i 是可以调整ping 间隔。所以上诉场景建议还需在分系统。
参考
https://linux.die.net/man/8/ping
推荐本站淘宝优惠价购买喜欢的宝贝:
本文链接:https://zblog.hqyman.cn/post/10285.html 非本站原创文章欢迎转载,原创文章需保留本站地址!
休息一下~~