前兩天有人問我.有沒有測試與一個服務器之間的 連接狀態(tài)的函數(shù)....
emmm,這個么當然沒有.
眾所周知.測試與服務器之間的鏈接狀態(tài),通常都是使用 ping命令,所以,我們測試與目標網絡的連接狀態(tài),理所當然的使用ping來實現(xiàn).ping個十次八次的然后拿到他的 最大值/最小值/平均值/平均偏差,通過這些數(shù)值去判斷與服務器之間的連接狀態(tài)是否穩(wěn)定.
例如:
ping -c 10 www.baidu.com
PING www.a.shifen.com (61.135.169.121) 56(84) bytes of data.
64 bytes from 61.135.169.121: icmp_seq=1 ttl=53 time=6.82 ms
64 bytes from 61.135.169.121: icmp_seq=2 ttl=53 time=6.23 ms
64 bytes from 61.135.169.121: icmp_seq=3 ttl=53 time=7.28 ms
64 bytes from 61.135.169.121: icmp_seq=4 ttl=53 time=103 ms
64 bytes from 61.135.169.121: icmp_seq=5 ttl=53 time=96.0 ms
64 bytes from 61.135.169.121: icmp_seq=6 ttl=53 time=11.1 ms
64 bytes from 61.135.169.121: icmp_seq=7 ttl=53 time=11.1 ms
64 bytes from 61.135.169.121: icmp_seq=8 ttl=53 time=7.74 ms
64 bytes from 61.135.169.121: icmp_seq=9 ttl=53 time=10.9 ms
64 bytes from 61.135.169.121: icmp_seq=10 ttl=53 time=12.0 ms
--- www.a.shifen.com ping statistics ---
10 packets transmitted, 10 received, 0% packet loss, time 9014ms
rtt min/avg/max/mdev = 6.236/27.247/103.125/36.251 ms
那我們ping10次百度,然后查看返回的結果 第16行 rtt開頭的那部分
min:最小值
avg:平均值
max:最大值
mdev:平均偏差值
一般看mdev這個值,這個值越小,說明你與目標服務器的連接狀態(tài)越穩(wěn)定,如果越大,則說明越不穩(wěn)定.
至于取值...就看你自己習慣使用哪種方式取了.是使用shell取值還是使用js來取值.這種使用shell執(zhí)行的命令,我還是比較喜歡使用shell的方式去取值,那一起來看最終的實現(xiàn):
/**
 * 測試與目標網絡的ping值,返回最小值 最大值 平均值 平均偏差值
 * @param {string} target 目標網絡的域名或者IP
 * @param {int} count  測試次數(shù),次數(shù)越大,測試值越準確,但時間會越長
 */
function pingTest(target, count) {
    var res = shell("ping -c "+ count +" "+ target +" |grep 'rtt' |awk '{print $4}'").result;
        res = res.replace("\\n", "");
    var resArry = res.split("/");
    return {
        min : resArry[0], //最小值
        max : resArry[2], //最大值 
        avg : resArry[1], //平均值
        mdev: resArry[3]  //與平均值的偏差,值越大,說明網絡越不穩(wěn)定
    };
}
var t = pingTest("www.baidu.com", 10);
console.log(t.mdev);
附:獲取單次ping的time值
var res = shell("ping -c 1 www.baidu.com |awk '{print $7}' |grep -o '[0-9].*'");
console.log(res);
- 
                                服務器
                                +關注關注 13文章 10029瀏覽量 90458
- 
                                網絡
                                +關注關注 14文章 8059瀏覽量 92528
- 
                                Ping
                                +關注關注 0文章 72瀏覽量 16735
發(fā)布評論請先 登錄
zigbee終端和路由配置網絡狀態(tài)變了
LTE網絡進行容量測試的方法
無線傳感器網絡設計的目標
基于 FPGA 的目標檢測網絡加速電路設計
隨機模糊神經網絡在目標狀態(tài)信息融合中的應用
基于小波神經網絡的目標跟蹤的研究
目標檢測傳感器網絡參數(shù)設計
基于OpenFlow的網絡層移動目標防御方案
 
    
 
           
        
 
         與目標網絡的網絡狀態(tài)測試
與目標網絡的網絡狀態(tài)測試 
                 
  
     
            
             
             
                 
             工商網監(jiān)
工商網監(jiān)
        
評論