{"id":172469,"date":"2024-11-07T13:23:20","date_gmt":"2024-11-07T05:23:20","guid":{"rendered":"https:\/\/server.hk\/cnblog\/172469\/"},"modified":"2024-11-07T13:23:21","modified_gmt":"2024-11-07T05:23:21","slug":"%e5%a6%82%e4%bd%95%e4%bf%9d%e7%95%99ping%e6%93%8d%e4%bd%9c%e7%b5%90%e6%9e%9c%e4%b8%a6%e5%ad%98%e5%84%b2%e5%88%b0%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%9f-ping%e6%93%8d%e4%bd%9c%e5%be%8c%e5%a6%82%e4%bd%95","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/172469\/","title":{"rendered":"\u5982\u4f55\u4fdd\u7559Ping\u64cd\u4f5c\u7d50\u679c\u4e26\u5b58\u5132\u5230\u6578\u64da\u5eab\uff1f (ping\u64cd\u4f5c\u5f8c\u5982\u4f55\u4fdd\u5b58\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"%e5%a6%82%e4%bd%95%e4%bf%9d%e7%95%99ping%e6%93%8d%e4%bd%9c%e7%b5%90%e6%9e%9c%e4%b8%a6%e5%ad%98%e5%84%b2%e5%88%b0%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%9f-CeJYTBhTwV\">\u5982\u4f55\u4fdd\u7559Ping\u64cd\u4f5c\u7d50\u679c\u4e26\u5b58\u5132\u5230\u6578\u64da\u5eab\uff1f<\/h1>\n<p>\u5728\u7db2\u7d61\u7ba1\u7406\u548c\u4f3a\u670d\u5668\u76e3\u63a7\u4e2d\uff0cPing\u64cd\u4f5c\u662f\u4e00\u500b\u5e38\u7528\u7684\u5de5\u5177\uff0c\u7528\u65bc\u6aa2\u67e5\u7db2\u7d61\u9023\u63a5\u7684\u53ef\u7528\u6027\u548c\u5ef6\u9072\u3002\u901a\u904ePing\u547d\u4ee4\uff0c\u7ba1\u7406\u54e1\u53ef\u4ee5\u7372\u53d6\u76ee\u6a19\u4f3a\u670d\u5668\u7684\u97ff\u61c9\u6642\u9593\uff0c\u9019\u5c0d\u65bc\u8a3a\u65b7\u7db2\u7d61\u554f\u984c\u548c\u6027\u80fd\u76e3\u63a7\u81f3\u95dc\u91cd\u8981\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u5c07Ping\u64cd\u4f5c\u7684\u7d50\u679c\u4fdd\u7559\u4e26\u5b58\u5132\u5230\u6578\u64da\u5eab\u4e2d\uff0c\u4ee5\u4fbf\u5f8c\u7e8c\u5206\u6790\u548c\u5831\u544a\u3002<\/p>\n<h2 id=\"ping%e6%93%8d%e4%bd%9c%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5-CeJYTBhTwV\">Ping\u64cd\u4f5c\u7684\u57fa\u672c\u6982\u5ff5<\/h2>\n<p>Ping\u662f\u4e00\u7a2e\u7db2\u7d61\u8a3a\u65b7\u5de5\u5177\uff0c\u901a\u904e\u5411\u76ee\u6a19IP\u5730\u5740\u767c\u9001ICMP\uff08Internet Control Message Protocol\uff09\u56de\u986f\u8acb\u6c42\uff0c\u4e26\u7b49\u5f85\u56de\u61c9\u4f86\u6e2c\u8a66\u7db2\u7d61\u9023\u63a5\u3002Ping\u64cd\u4f5c\u7684\u7d50\u679c\u901a\u5e38\u5305\u62ec\u4ee5\u4e0b\u5e7e\u500b\u65b9\u9762\uff1a<\/p>\n<ul>\n<li>\u76ee\u6a19IP\u5730\u5740<\/li>\n<li>\u97ff\u61c9\u6642\u9593\uff08\u4ee5\u6beb\u79d2\u70ba\u55ae\u4f4d\uff09<\/li>\n<li>\u4e1f\u5305\u7387<\/li>\n<li>\u64cd\u4f5c\u6642\u9593\u6233<\/li>\n<\/ul>\n<h2 id=\"%e5%a6%82%e4%bd%95%e5%9f%b7%e8%a1%8cping%e6%93%8d%e4%bd%9c-CeJYTBhTwV\">\u5982\u4f55\u57f7\u884cPing\u64cd\u4f5c<\/h2>\n<p>\u5728Linux\u7cfb\u7d71\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u57f7\u884cPing\u64cd\u4f5c\uff1a<\/p>\n<pre><code>ping -c 4 example.com<\/code><\/pre>\n<p>\u9019\u689d\u547d\u4ee4\u5c07\u5411example.com\u767c\u90014\u500bPing\u8acb\u6c42\uff0c\u4e26\u986f\u793a\u7d50\u679c\u3002\u70ba\u4e86\u5c07\u9019\u4e9b\u7d50\u679c\u5b58\u5132\u5230\u6578\u64da\u5eab\u4e2d\uff0c\u6211\u5011\u9700\u8981\u5c07\u5176\u8f38\u51fa\u9032\u884c\u8655\u7406\u3002<\/p>\n<h2 id=\"%e5%b0%87ping%e7%b5%90%e6%9e%9c%e5%ad%98%e5%84%b2%e5%88%b0%e6%95%b8%e6%93%9a%e5%ba%ab-CeJYTBhTwV\">\u5c07Ping\u7d50\u679c\u5b58\u5132\u5230\u6578\u64da\u5eab<\/h2>\n<p>\u5728\u9019\u88e1\uff0c\u6211\u5011\u5c07\u4f7f\u7528Python\u8a9e\u8a00\u4f86\u57f7\u884cPing\u64cd\u4f5c\u4e26\u5c07\u7d50\u679c\u5b58\u5132\u5230MySQL\u6578\u64da\u5eab\u4e2d\u3002\u9996\u5148\uff0c\u78ba\u4fdd\u5df2\u5b89\u88dd\u5fc5\u8981\u7684\u5eab\uff1a<\/p>\n<pre><code>pip install mysql-connector-python<\/code><\/pre>\n<h3 id=\"%e6%ad%a5%e9%a9%9f1%ef%bc%9a%e8%a8%ad%e7%bd%ae%e6%95%b8%e6%93%9a%e5%ba%ab-CeJYTBhTwV\">\u6b65\u9a5f1\uff1a\u8a2d\u7f6e\u6578\u64da\u5eab<\/h3>\n<p>\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u5728MySQL\u4e2d\u5275\u5efa\u4e00\u500b\u6578\u64da\u5eab\u548c\u4e00\u500b\u8868\u4f86\u5b58\u5132Ping\u7d50\u679c\uff1a<\/p>\n<pre><code>CREATE DATABASE ping_results;\nUSE ping_results;\n\nCREATE TABLE ping_data (\n    id INT AUTO_INCREMENT PRIMARY KEY,\n    ip_address VARCHAR(255) NOT NULL,\n    response_time FLOAT NOT NULL,\n    packet_loss FLOAT NOT NULL,\n    timestamp DATETIME DEFAULT CURRENT_TIMESTAMP\n);<\/code><\/pre>\n<h3 id=\"%e6%ad%a5%e9%a9%9f2%ef%bc%9a%e5%9f%b7%e8%a1%8cping%e6%93%8d%e4%bd%9c%e4%b8%a6%e5%ad%98%e5%84%b2%e7%b5%90%e6%9e%9c-CeJYTBhTwV\">\u6b65\u9a5f2\uff1a\u57f7\u884cPing\u64cd\u4f5c\u4e26\u5b58\u5132\u7d50\u679c<\/h3>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0bPython\u4ee3\u78bc\u4f86\u57f7\u884cPing\u64cd\u4f5c\u4e26\u5c07\u7d50\u679c\u5b58\u5132\u5230\u6578\u64da\u5eab\u4e2d\uff1a<\/p>\n<pre><code>import os\nimport mysql.connector\nfrom datetime import datetime\n\n# \u8a2d\u7f6e\u6578\u64da\u5eab\u9023\u63a5\ndb = mysql.connector.connect(\n    host=\"localhost\",\n    user=\"your_username\",\n    password=\"your_password\",\n    database=\"ping_results\"\n)\n\ncursor = db.cursor()\n\n# \u57f7\u884cPing\u64cd\u4f5c\nhostname = \"example.com\"\nresponse = os.popen(f\"ping -c 4 {hostname}\").read()\n\n# \u89e3\u6790Ping\u7d50\u679c\nlines = response.split(\"n\")\nfor line in lines:\n    if \"time=\" in line:\n        response_time = float(line.split(\"time=\")[1].split(\" \")[0])\n        packet_loss = 0  # \u5047\u8a2d\u6c92\u6709\u4e1f\u5305\n        timestamp = datetime.now()\n\n        # \u5c07\u7d50\u679c\u63d2\u5165\u6578\u64da\u5eab\n        cursor.execute(\"INSERT INTO ping_data (ip_address, response_time, packet_loss, timestamp) VALUES (%s, %s, %s, %s)\",\n                       (hostname, response_time, packet_loss, timestamp))\n        db.commit()\n\ncursor.close()\ndb.close()<\/code><\/pre>\n<h2 id=\"%e6%95%b8%e6%93%9a%e5%88%86%e6%9e%90%e8%88%87%e5%a0%b1%e5%91%8a-CeJYTBhTwV\">\u6578\u64da\u5206\u6790\u8207\u5831\u544a<\/h2>\n<p>\u4e00\u65e6Ping\u7d50\u679c\u5b58\u5132\u5230\u6578\u64da\u5eab\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528SQL\u67e5\u8a62\u4f86\u5206\u6790\u6578\u64da\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u67e5\u8a62\u5e73\u5747\u97ff\u61c9\u6642\u9593\u6216\u4e1f\u5305\u7387\uff1a<\/p>\n<pre><code>SELECT AVG(response_time) AS avg_response_time, AVG(packet_loss) AS avg_packet_loss FROM ping_data;<\/code><\/pre>\n<p>\u9019\u6a23\u7684\u67e5\u8a62\u53ef\u4ee5\u5e6b\u52a9\u60a8\u4e86\u89e3\u7db2\u7d61\u6027\u80fd\u7684\u8da8\u52e2\uff0c\u4e26\u53ca\u6642\u767c\u73fe\u6f5b\u5728\u7684\u554f\u984c\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-CeJYTBhTwV\">\u7e3d\u7d50<\/h2>\n<p>\u901a\u904e\u5c07Ping\u64cd\u4f5c\u7684\u7d50\u679c\u5b58\u5132\u5230\u6578\u64da\u5eab\u4e2d\uff0c\u7db2\u7d61\u7ba1\u7406\u54e1\u53ef\u4ee5\u66f4\u6709\u6548\u5730\u76e3\u63a7\u548c\u5206\u6790\u7db2\u7d61\u6027\u80fd\u3002\u9019\u4e0d\u50c5\u6709\u52a9\u65bc\u53ca\u6642\u767c\u73fe\u554f\u984c\uff0c\u9084\u80fd\u70ba\u672a\u4f86\u7684\u512a\u5316\u63d0\u4f9b\u6578\u64da\u652f\u6301\u3002\u5982\u679c\u60a8\u9700\u8981\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u57f7\u884c\u9019\u4e9b\u64cd\u4f5c\uff0c\u8acb\u8003\u616e\u6211\u5011\u7684\u670d\u52d9\u3002\u6211\u5011\u63d0\u4f9b\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u65b9\u6848\uff0c\u5e6b\u52a9\u60a8\u8f15\u9b06\u7ba1\u7406\u4f3a\u670d\u5668\u548c\u7db2\u7d61\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u5c07Ping\u64cd\u4f5c\u7d50\u679c\u6709\u6548\u4fdd\u7559\u4e26\u5b58\u5132\u5230\u6578\u64da\u5eab\u4e2d\uff0c\u78ba\u4fdd\u6578\u64da\u7684\u6301\u4e45\u6027\u8207\u53ef\u67e5\u8a62\u6027\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-172469","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/172469","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=172469"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/172469\/revisions"}],"predecessor-version":[{"id":172470,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/172469\/revisions\/172470"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=172469"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=172469"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=172469"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}