有時(shí)候需要遠(yuǎn)程家里的臺(tái)式機(jī)使用,因?yàn)槲移綍r(shí)都是用 MAC 多,但是遠(yuǎn)程喚醒只能針對(duì)局域網(wǎng),比較麻煩,于是我想用微信實(shí)現(xiàn)遠(yuǎn)程喚醒機(jī)器。
準(zhǔn)備工作
本程序主要是實(shí)現(xiàn)遠(yuǎn)程管理 Windows10操作系統(tǒng)的開(kāi)機(jī)和關(guān)機(jī):
在 Windows機(jī)器的相同內(nèi)網(wǎng)中放一個(gè) Linux 主機(jī),我這里用樹(shù)莓派代替,如果你是用 OpenWrt 之類的路由器也可以。
Linux 主機(jī)需要能夠遠(yuǎn)程訪問(wèn),我這里是有 FRP 將樹(shù)莓派的端口映射到我的公網(wǎng) Linux 主機(jī)上。所以可以隨時(shí)遠(yuǎn)程 SSH 過(guò)去。
Windows 機(jī)器的網(wǎng)卡必須是有線連接,支持網(wǎng)絡(luò)喚醒功能。
開(kāi)機(jī)實(shí)現(xiàn)思路
首先通過(guò)微信發(fā)送開(kāi)機(jī)指令,這里我使用的是 itchat 程序會(huì)調(diào)用 Paramiko 庫(kù)去 SSH 遠(yuǎn)程到內(nèi)網(wǎng)的樹(shù)莓派執(zhí)行 WakeOnLan 命令去喚醒 Windows 主機(jī)。
pi@raspberrypi:~$wakeonlan-i192.168.1.014:dd:a9:ea:0b:96Sendingmagicpacketto192.168.1.0:9with14:dd:a9:ea:0b:96
程序會(huì)通過(guò) ICMP 協(xié)議, ping 下需要喚醒的目標(biāo)主機(jī)然后進(jìn)行過(guò)濾,一個(gè)正常的 ICMP 包是64字節(jié),過(guò)濾打印出這個(gè)64。
例如 ping 百度:
?~pingwww.baidu.comPINGwww.a.shifen.com(180.97.33.108):56databytes64bytesfrom180.97.33.108:icmp_seq=0ttl=53time=8.865ms64bytesfrom180.97.33.108:icmp_seq=1ttl=53time=9.206ms64bytesfrom180.97.33.108:icmp_seq=2ttl=53time=8.246ms
用一段 Linux 命令去過(guò)濾是否有64,這里為啥要用 head -n 1 呢?
因?yàn)橛锌赡軙?huì)出現(xiàn)2行,經(jīng)過(guò)測(cè)試,我們只需要取64這個(gè)值就可以了:
ping192.168.1.182-c1|grep64|cut-d""-f1|head-n1
如果有則表示開(kāi)機(jī)成功已經(jīng)聯(lián)網(wǎng)了,返回開(kāi)機(jī)成功,否則程序繼續(xù)往下走,去喚醒,然后在 ping 一次確認(rèn)是否開(kāi)機(jī),如果為是則返回開(kāi)機(jī)成功,否則返回失敗。程序執(zhí)行成功后,在我的網(wǎng)站根目錄創(chuàng)建一個(gè) shutdown 文件,用于后面的關(guān)機(jī)操作:
#!/usr/bin/python#-*-coding:utf-8-*-importitchatimportparamikoimportosimporttimeimportsysreload(sys)sys.setdefaultencoding('utf-8')hostname=''username=''port=key_file='/home/fangwenjun/.ssh/id_rsa'filename='/home/fangwenjun/.ssh/known_hosts'@itchat.msg_register(itchat.content.TEXT)deftext_reply(msg):ifmsg['ToUserName']!='filehelper':returnifmsg['Text']==u'開(kāi)機(jī)':paramiko.util.log_to_file('ssh_key-login.log')privatekey=os.path.expanduser(key_file)try:key=paramiko.RSAKey.from_private_key_file(privatekey)exceptparamiko.PasswordRequiredException:key=paramiko.RSAKey.from_private_key_file(privatekey,key_file_pwd)ssh=paramiko.SSHClient()ssh.load_system_host_keys(filename=filename)ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())ssh.connect(hostname=hostname,username=username,pkey=key,port=port)#執(zhí)行喚醒命令stdin,stdout,stderr=ssh.exec_command('ping192.168.1.182-c1|grep64|cut-d""-f1|head-n1')sshCheckOpen=stdout.read()sshCheckOpen=sshCheckOpen.strip(' ')printtype(sshCheckOpen)printsshCheckOpen#進(jìn)行判斷,如果為64,則說(shuō)明ping成功,說(shuō)明設(shè)備已經(jīng)在開(kāi)機(jī)狀態(tài),程序結(jié)束,否則執(zhí)行喚醒ifsshCheckOpen=='64':connect_ok_time=time.strftime("%Y-%m-%d%H:%M:%S",time.localtime())itchat.send(connect_ok_time+u'設(shè)備已經(jīng)開(kāi)機(jī)',toUserName='filehelper')else:ssh_time=time.strftime("%Y-%m-%d%H:%M:%S",time.localtime())itchat.send(ssh_time+u'開(kāi)始連接遠(yuǎn)程主機(jī)',toUserName='filehelper')stdin,stdout,stderr=ssh.exec_command('wakeonlan-i192.168.1.014:dd:a9:ea:0b:96')wakeonlan_time=time.strftime("%Y-%m-%d%H:%M:%S",time.localtime())itchat.send(wakeonlan_time+u'執(zhí)行喚醒,等待設(shè)備開(kāi)機(jī)聯(lián)網(wǎng)',toUserName='filehelper')#由于開(kāi)機(jī)需要一些時(shí)間去啟動(dòng)網(wǎng)絡(luò),所以這里等等60stime.sleep(60)#執(zhí)行ping命令,-c1表示只ping一下,然后過(guò)濾有沒(méi)有64,如果有則獲取64傳給sshConStatusstdin,stdout,stderr=ssh.exec_command('ping192.168.1.182-c1|grep64|cut-d""-f1|head-n1')sshConStatus=stdout.read()sshConStatus=sshConStatus.strip(' ')printtype(sshConStatus)printsshConStatus#進(jìn)行判斷,如果為64,則說(shuō)明ping成功,設(shè)備已經(jīng)聯(lián)網(wǎng),可以進(jìn)行遠(yuǎn)程連接了,否則發(fā)送失敗消息ifsshConStatus=='64':connect_ok_time=time.strftime("%Y-%m-%d%H:%M:%S",time.localtime())itchat.send(connect_ok_time+u'設(shè)備喚醒成功,您可以遠(yuǎn)程連接了',toUserName='filehelper')else:connect_err_time=time.strftime("%Y-%m-%d%H:%M:%S",time.localtime())itchat.send(connect_err_time+u'設(shè)備喚醒失敗,請(qǐng)檢查設(shè)備是否連接電源',toUserName='filehelper')ssh.close()#在網(wǎng)站根目錄創(chuàng)建一個(gè)空文件,命名為shutdownos.system('touch/www/shutdown')print'執(zhí)行開(kāi)機(jī)消息成功'
關(guān)機(jī)部分實(shí)現(xiàn)
當(dāng)接收關(guān)機(jī)指令時(shí),程序會(huì)去刪除網(wǎng)站根目錄的 shutdown 文件,客戶端我寫了幾行代碼,去通過(guò) Requests 庫(kù)每隔30s 發(fā)送 HTTP head 請(qǐng)求去判斷文件是否是404,如果是404 這說(shuō)明文件不存在,調(diào)用系統(tǒng)關(guān)機(jī)操作,執(zhí)行關(guān)機(jī)。
然后 SSH 到樹(shù)莓派去 ping 目標(biāo)主機(jī),如果返回為空,則說(shuō)明關(guān)機(jī)成功,否則關(guān)機(jī)失敗。這只是針對(duì) Windows 的關(guān)機(jī),如果目標(biāo)主機(jī)是 Linux 則簡(jiǎn)單多了:
ifmsg['Text']==u'關(guān)機(jī)':#刪除網(wǎng)站根目錄的shutdown文件rmfile=os.system('rm-rf/www/shutdown')ifrmfile==0:print'執(zhí)行關(guān)機(jī)消息成功'shutdown_time=time.strftime("%Y-%m-%d%H:%M:%S",time.localtime())itchat.send(shutdown_time+u'正在關(guān)機(jī)....',toUserName='filehelper')paramiko.util.log_to_file('ssh_key-login.log')privatekey=os.path.expanduser(key_file)try:key=paramiko.RSAKey.from_private_key_file(privatekey)exceptparamiko.PasswordRequiredException:key=paramiko.RSAKey.from_private_key_file(privatekey,key_file_pwd)ssh=paramiko.SSHClient()ssh.load_system_host_keys(filename=filename)ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())ssh.connect(hostname=hostname,username=username,pkey=key,port=port)itchat.send(shutdown_time+u'正在確認(rèn)設(shè)備是否完成關(guān)機(jī)操作,大約需要等待60s.',toUserName='filehelper')#等等60秒后確認(rèn),因?yàn)殛P(guān)機(jī)需要一段時(shí)間,如果設(shè)置太短,可能網(wǎng)絡(luò)還沒(méi)斷開(kāi)time.sleep(60)stdin,stdout,stderr=ssh.exec_command('ping192.168.1.182-c1|grep64|cut-d""-f1|head-n1')sshConStatus=stdout.read()sshConStatus=sshConStatus.strip(' ')printtype(sshConStatus)printsshConStatus#如果獲取的值為空,則說(shuō)明已經(jīng)關(guān)機(jī),否則關(guān)機(jī)失敗ifsshConStatus!='64':shutdown_success_err_time=time.strftime("%Y-%m-%d%H:%M:%S",time.localtime())itchat.send(shutdown_success_err_time+u'關(guān)機(jī)成功',toUserName='filehelper')else:shutdown_err_time=time.strftime("%Y-%m-%d%H:%M:%S",time.localtime())itchat.send(shutdown_err_time+u'關(guān)機(jī)失敗,請(qǐng)連接桌面檢查客戶端程序是否正常執(zhí)行',toUserName='filehelper')ssh.close()itchat.auto_login(hotReload=True,enableCmdQR=2)itchat.run()
客戶端代碼,寫完扔計(jì)劃任務(wù),開(kāi)機(jī)啟動(dòng):
importrequestsimportosimporttimewhile1:time.sleep(30)r=requests.head("https://awen.me/shutdown")printr.status_codeifr.status_code==404:os.system("shutdown-s-t5")
使用 TeamViewer 連接:
缺點(diǎn)
網(wǎng)頁(yè)端微信必須一直登錄,不方便,這個(gè)就需要微信不能斷網(wǎng)了。
WakeOnLan 是廣播 MAC 地址的,貌似不能返回是否成功沒(méi),所以還是要 ping 主機(jī)看看通不通,判斷下。
需要一個(gè)樹(shù)莓派做跳板機(jī),否則也不能喚醒內(nèi)網(wǎng)設(shè)備。
如果只允許自己控制最好是使用文件助手來(lái)發(fā)送消息,因?yàn)槟J(rèn)情況下,任何人都可以給你發(fā)送指令開(kāi)機(jī)。
Windows需要安裝TeamViewer并且設(shè)置為開(kāi)機(jī)自動(dòng)啟動(dòng)以及綁定賬號(hào)設(shè)置無(wú)人值守模式。這樣方便遠(yuǎn)程,如果是Linux 則不需要開(kāi)啟 ssh 就可以了。
代碼地址:https://github.com/monkey-wenjun/wchatwakeonlan
文章內(nèi)的代碼如果有 Bug,后續(xù)更新都在 GitHub 上,完整代碼請(qǐng)參考 GitHub ,此文章代碼不再更新。
原文:https://awen.me/post/3709919605.html
-
WINDOWS
+關(guān)注
關(guān)注
4文章
3614瀏覽量
91419 -
路由器
+關(guān)注
關(guān)注
22文章
3839瀏覽量
116742 -
python
+關(guān)注
關(guān)注
56文章
4827瀏覽量
86762
原文標(biāo)題:在家想遠(yuǎn)程公司電腦?Python +微信一鍵連接
文章出處:【微信號(hào):rgznai100,微信公眾號(hào):rgznai100】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄
一鍵還原_一鍵還原精靈等系統(tǒng)還原軟件集合
遠(yuǎn)程管理系統(tǒng)定制
python黑客操作:遠(yuǎn)程開(kāi)機(jī)和關(guān)機(jī)
有什么辦法可以實(shí)現(xiàn)電腦一鍵長(zhǎng)按開(kāi)關(guān)機(jī)功能嗎?
windows系統(tǒng)是怎樣去設(shè)置定時(shí)開(kāi)關(guān)機(jī)的
ROC-RK3328-CC板是怎樣連接Windows和遠(yuǎn)程管理可視化桌面的
如何遠(yuǎn)程管理windows2003服務(wù)器
微軟Windows10值得升級(jí)嗎?給你八個(gè)升級(jí)Windows10的理由!
Linux操作系統(tǒng)實(shí)用教程之如何Linux系統(tǒng)的遠(yuǎn)程管理
windows10操作系統(tǒng)安全模式的使用技巧
一鍵開(kāi)關(guān)機(jī)電路圖解析

微軟表態(tài):Windows10操作系統(tǒng)是“最后一代”
windows系統(tǒng)設(shè)置定時(shí)開(kāi)關(guān)機(jī)的方法

軟件配合實(shí)現(xiàn)的“一鍵開(kāi)關(guān)機(jī)電路

評(píng)論