{"id":173480,"date":"2020-03-09T09:00:54","date_gmt":"2020-03-09T01:00:54","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=173480"},"modified":"2020-02-27T14:57:05","modified_gmt":"2020-02-27T06:57:05","slug":"python-wake-linux","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/python-wake-linux.html","title":{"rendered":"\u5927\u795e\u6559\u4f60\u7528Python\u5b9e\u73b0Wake On Lan\u8fdc\u7a0b\u5f00\u673a\u529f\u80fd"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4ecb\u7ecd\u4e86\u4f7f\u7528Python\u5b9e\u73b0Wake On Lan\u8fdc\u7a0b\u5f00\u673a\u529f\u80fd\uff0c\u6587\u4e2d\u7ed9\u5927\u5bb6\u8865\u5145\u4ecb\u7ecd\u4e86python\u901a\u8fc7wakeonlan\u5524\u9192\u5185\u7f51\u7535\u8111\u5f00\u673a\uff0c\u975e\u5e38\u4e0d\u9519\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u8ddf\u968f\u5c0f\u7f16\u4e00\u8d77\u5b66\u4e60\u5427<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

Wake-On-LAN\u7b80\u79f0WOL\uff0c\u662f\u4e00\u79cd\u7535\u6e90\u7ba1\u7406\u529f\u80fd\uff1b\u5982\u679c\u5b58\u5728\u7f51\u7edc\u6d3b\u52a8\uff0c\u5219\u5141\u8bb8\u8bbe\u5907\u5c06\u64cd\u4f5c\u7cfb\u7edf\u4ece\u5f85\u673a\u6216\u4f11\u7720\u6a21\u5f0f\u4e2d\u5524\u9192\u3002\u8bb8\u591a\u4e3b\u677f\u5382\u5546\u652f\u6301IBM\u63d0\u51fa\u7684\u7f51\u7edc\u5524\u9192\u6807\u51c6\u3002\u8be5\u6807\u51c6\u5141\u8bb8\u7f51\u7edc\u7ba1\u7406\u5458\u8fdc\u7a0b\u6253\u5f00PC\u673a\u7535\u6e90\uff0c\u4ee5\u4fbf\u8fdb\u884c\u6587\u4ef6\u5347\u7ea7\u3001\u8d44\u6e90\u8ddf\u8e2a\u548c\u8bbe\u5907\u6e05\u70b9\u7b49\u5de5\u4f5c\u3002<\/p>\n

\u5148\u770b\u4e0b\u4f7f\u7528Python\u5b9e\u73b0WakeOn Lan\u8fdc\u7a0b\u5f00\u673a\u529f\u80fd\uff0c\u5177\u4f53\u4ecb\u7ecd\u5982\u4e0b\uff1a<\/p>\n

\u521b\u5efa\u9b54\u6cd5\u5524\u9192\u5305<\/strong><\/span><\/div>\n

\u683c\u5f0f\u5316mac\u5730\u5740\uff0c\u751f\u6210\u9b54\u6cd5\u5524\u9192\u5305\uff0c\u7136\u540e\u53d1\u9001\u5305\uff0c\u9996\u5148\u7535\u8111\u9700\u8981\u6253\u5f00wake on lan\u529f\u80fd<\/p>\n

\u521b\u5efamain_wake_on_lan.py\u6587\u4ef6<\/p>\n

import socket\r\nimport binascii\r\nimport struct\r\nimport re\r\n'''\r\n\u683c\u5f0f\u5316mac\u5730\u5740\uff0c\u751f\u6210\u9b54\u6cd5\u5524\u9192\u5305\uff0c\u7136\u540e\u53d1\u9001\u3002\r\nmac\u683c\u5f0f\uff1a mac = A1B2C3D4E5F6\r\n\u5524\u9192\u5305\u683c\u5f0f\uff1a send_data = binascii.unhexlify('FF'*6 + str(mac)*16)\r\n'''\r\nMAC = \"18:31:BF:B0:36:8F\"\r\n \r\n# MAC = \"98-90-96-C1-FE-CB\"\r\n \r\n# \u683c\u5f0f\u5316MAC\u5730\u5740989096C1FECB\u4e3a\u8fd9\u79cd\u5f62\u5f0f\r\ndef format_mac0(mac):\r\n if len(mac) == 12:\r\n  pass\r\n elif len(mac) == 17:\r\n  if mac.count(':') == 5 or mac.count('-') == 5:\r\n   sep = mac[2]\r\n   mac = mac.replace(sep, '')\r\n  else:\r\n   raise ValueError('Incorrect MAC format')\r\n else:\r\n  raise ValueError('Incorrect MAC format')\r\n return mac\r\ndef format_mac(mac):\r\n mac_re = re.compile(r'''\r\n      (^([0-9A-F]{1,2}[-]){5}([0-9A-F]{1,2})$\r\n      |^([0-9A-F]{1,2}[:]){5}([0-9A-F]{1,2})$\r\n      |^([0-9A-F]{1,2}[.]){5}([0-9A-F]{1,2})$\r\n      )''', re.VERBOSE | re.IGNORECASE)\r\n # print(re.match(mac_re, mac))\r\n if re.match(mac_re, mac):\r\n  if mac.count(':') == 5 or mac.count('-') == 5 or mac.count('.'):\r\n   sep = mac[2]\r\n   mac_fm = mac.replace(sep, '')\r\n   return mac_fm\r\n else:\r\n  raise ValueError('Incorrect MAC format')\r\n \r\n# \u65b9\u6cd5\u4e00\uff1a\u5c06989096C1FECB\u683c\u5f0f\u7684mac\u5730\u5740\u521b\u5efa\u5524\u9192\u5305\r\ndef create_magic_packet0(mac):\r\n data = b'FF' * 6 + (mac * 16).encode()\r\n print(data)\r\n \r\n print(type(data))\r\n send_data = b''\r\n for i in range(0, len(data), 2):\r\n  send_data = send_data + struct.pack(b'B', int(data[i: i + 2], 16)) # int(data[i: i+2], 16) \u628a16\u8fdb\u5236\u8f6c\u6362\u6210\u6574\u6570\r\n print(type(send_data))\r\n return send_data\r\n# \u65b9\u6cd5\u4e8c\uff1a\u5c06989096C1FECB\u683c\u5f0f\u7684mac\u5730\u5740\u521b\u5efa\u5524\u9192\u5305\uff0c\u4f7f\u7528binascii.unhexlify()\u65b9\u6cd5\r\ndef create_magic_packet(mac):\r\n data = 'FF' * 6 + str(mac) * 16\r\n # print(data)\r\n # print(type(data))\r\n send_data = binascii.unhexlify(data)\r\n # print(type(send_data))\r\n return send_data\r\ndef send_magic_packet(send_data):\r\n # broadcast_address = '192.168.255.255'\r\n broadcast_address = '255.255.255.255'\r\n port = 9\r\n s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)\r\n s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)\r\n s.sendto(send_data, (broadcast_address, port))\r\nif __name__ == '__main__':\r\n # print('mac\u5730\u5740\uff1a', format_mac(MAC))\r\n mac = format_mac(MAC)\r\n send_data = create_magic_packet(mac)\r\n # print(send_data)\r\n send_magic_packet(send_data)<\/pre>\n

\u8c03\u7528\u53d1\u5305\u51fd\u6570<\/p>\n

\u521b\u5efamain_boot_computer.py<\/p>\n

import main_wake_on_lan\r\nimport sys\r\n \r\n \r\ndef boot_computer():\r\n try:\r\n  pyname, parameter = sys.argv\r\n  if parameter == '-h':\r\n   print('\u53c2\u6570\u4f7f\u7528\u65b9\u6cd5\uff1apython3 main_boot_computer.py mac\u5730\u5740\\npython3 main_boot_computer.py 98:90:96:C1:FE:CB')\r\n  else:\r\n   print('\u6b63\u5728\u5411 ', parameter, ' \u53d1\u9001\u9b54\u6cd5\u5524\u9192\u5305\uff01')\r\n   # mac = '98:90:96:C1:FE:CB'\r\n \r\n   mac = main_wake_on_lan.format_mac(parameter)\r\n   send_data = main_wake_on_lan.create_magic_packet(mac)\r\n \r\n   main_wake_on_lan.send_magic_packet(send_data)\r\n   return '\u6210\u529f\u5411' + parameter + '\u53d1\u9001\u5524\u9192\u5305\uff01'\r\n except ValueError:\r\n  print('\u672a\u6536\u5230\u4f20\u5165\u7684\u53c2\u6570\\n\u83b7\u53d6\u5e2e\u52a9\uff1apython3 main_boot_computer.py -h')\r\n \r\nif __name__ == '__main__':\r\n boot_computer()<\/pre>\n

\u4f7f\u7528\u65b9\u6cd5<\/p>\n

python3 main_boot_computer.py mac\u5730\u5740<\/pre>\n
\u8865\u5145\uff1apython\u901a\u8fc7wakeonlan\u5524\u9192\u5185\u7f51\u7535\u8111\u5f00\u673a<\/strong><\/span><\/div>\n

\u9996\u5148\u9700\u8981pip3 install wakeonlan<\/p>\n

\u7136\u540e\u5728\u7535\u8111\u9700\u8981\u4f60\u7684\u7f51\u5361\u652f\u6301\u7f51\u7edc\u5524\u9192\u8ba1\u7b97\u673a\u3002<\/p>\n

\u7136\u540e\u5728\u4e3b\u677fBIOS\u5f00\u542f\u652f\u6301\u5524\u9192\u3002<\/p>\n

\u5728\u7cfb\u7edf\u7f51\u5361\u5c5e\u6027\u91cc\u9009\u4e0a\u201c\u5141\u8bb8\u8ba1\u7b97\u673a\u5173\u95ed\u6b64\u8bbe\u5907\u4ee5\u8282\u7ea6\u7535\u6e90\u201d\uff0c\u201c\u5141\u8bb8\u6b64\u8bbe\u5907\u5524\u9192\u8ba1\u7b97\u673a\u201d<\/p>\n

\u7136\u540e\u4ee5\u4e0b\u5c31\u662fpython\u4ee3\u7801\uff0c\u975e\u5e38\u7b80\u5355\u3002<\/p>\n

from wakeonlan import send_magic_packet\r\nsend_magic_packet('4d.2d.6b.12.9b.1f')<\/pre>\n
\u603b\u7ed3<\/strong><\/span><\/div>\n

\u4ee5\u4e0a\u6240\u8ff0\u662f\u5c0f\u7f16\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u7684\u4f7f\u7528Python\u5b9e\u73b0Wake On Lan\u8fdc\u7a0b\u5f00\u673a\u529f\u80fd\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"

Wake-On-LAN\u7b80\u79f0WOL\uff0c\u662f\u4e00\u79cd\u7535\u6e90\u7ba1\u7406\u529f\u80fd\uff1b\u5982\u679c\u5b58\u5728\u7f51\u7edc\u6d3b\u52a8\uff0c\u5219\u5141\u8bb8\u8bbe\u5907\u5c06\u64cd\u4f5c\u7cfb\u7edf\u4ece\u5f85\u673a\u6216\u4f11\u7720\u6a21\u5f0f\u4e2d […]<\/p>\n","protected":false},"author":1482,"featured_media":84518,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-173480","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/173480","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1482"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=173480"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/173480\/revisions"}],"predecessor-version":[{"id":173482,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/173480\/revisions\/173482"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/84518"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=173480"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=173480"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=173480"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}