日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

Python黑科技:在家远程遥控公司电脑,python+微信一键连接!

發(fā)布時(shí)間:2023/12/10 python 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python黑科技:在家远程遥控公司电脑,python+微信一键连接! 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

有時(shí)候需要遠(yuǎn)程家里的臺(tái)式機(jī)使用,因?yàn)槲移綍r(shí)都是用 MAC 多,但是遠(yuǎn)程喚醒只能針對局域網(wǎng),比較麻煩,于是我想用微信實(shí)現(xiàn)遠(yuǎn)程喚醒機(jī)器。


*注意:全文代碼可左右滑動(dòng)查看

準(zhǔn)備工作

本程序主要是實(shí)現(xiàn)遠(yuǎn)程管理 Windows10操作系統(tǒng)的開機(jī)和關(guān)機(jī):

在 Windows機(jī)器的相同內(nèi)網(wǎng)中放一個(gè) Linux 主機(jī),我這里用樹莓派代替,如果你是用 OpenWrt 之類的路由器也可以。

Linux 主機(jī)需要能夠遠(yuǎn)程訪問,我這里是有 FRP 將樹莓派的端口映射到我的公網(wǎng) Linux 主機(jī)上。所以可以隨時(shí)遠(yuǎn)程 SSH 過去。

Windows 機(jī)器的網(wǎng)卡必須是有線連接,支持網(wǎng)絡(luò)喚醒功能。

開機(jī)實(shí)現(xiàn)思路

首先通過微信發(fā)送開機(jī)指令,這里我使用的是 itchat 程序會(huì)調(diào)用 Paramiko 庫去 SSH 遠(yuǎn)程到內(nèi)網(wǎng)的樹莓派執(zhí)行 WakeOnLan 命令去喚醒 Windows 主機(jī)。

pi@raspberrypi:~$wakeonlan -i 192.168.1.0.14:dd:a9:ea:0b:96
Sending magic packet to 192.168.1.0:9 with 14:dd:a9:ea:0b:96
程序會(huì)通過 ICMP 協(xié)議, ping 下需要喚醒的目標(biāo)主機(jī)然后進(jìn)行過濾,一個(gè)正常的 ICMP 包是64字節(jié),過濾打印出這個(gè)64。

例如 ping 百度:

→ ~ping www.baidu.com
PING www.a.shifen.com (180.97.33.108):56 data bytes
64 bytes from 180.97.33.108: icmp_seq=0 ttl=53 time=8.865 ms
64 bytes from 180.97.33.108: icmp_seq=1 ttl=53 time=9.206 ms
64 bytes from 180.97.33.108: icmp_seq=2 ttl=53 time=8.246 ms
用一段 Linux 命令去過濾是否有64,這里為啥要用 head -n 1 呢?

因?yàn)橛锌赡軙?huì)出現(xiàn)2行,經(jīng)過測試,我們只需要取64這個(gè)值就可以了:

ping 192.168.1.182-c 1 | grep 64 | cut -d "" -f 1|head -n 1
如果有則表示開機(jī)成功已經(jīng)聯(lián)網(wǎng)了,返回開機(jī)成功,否則程序繼續(xù)往下走,去喚醒,然后在 ping 一次確認(rèn)是否開機(jī),如果為是則返回開機(jī)成功,否則返回失敗。程序執(zhí)行成功后,在我的網(wǎng)站根目錄創(chuàng)建一個(gè) shutdown 文件,用于后面的關(guān)機(jī)操作:

@!/usr/bin/python

-- coding:utf-8 --

import itchat
import paramiko
import os
import time
import sys
reload(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)
def text_reply(msg):
if msg['ToTserName'] != 'filehelper': return
if msg['Text'] == u'開機(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)
except paramiko.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(parmiko.AutoAddPolicy())ssh.connect(hostname=hostname,uername=username,pkey=key,port=port)#執(zhí)行喚醒命令stdin,stdout,stderr=ssh.exec_command('ping 192.168.1.182 -c 1 | grep 64 | cut -d "" -f 1|head -n 1')sshCheckOpen = stdout.read()sshCheckOpen = sshCheckOpen.strip('\n')print type(sshCheckOpen)print sshCheckOpen#進(jìn)行判斷,如果為64,則說明 ping 成功,說明設(shè)備已經(jīng)在開機(jī)狀態(tài),程序結(jié)束,否則執(zhí)行喚醒if sshCheckOpen == '64':connect_ok_time = time.strftime(*%Y-%m-%d %H:%M:%S",time.localtime())itchat.send(connect_ok_time+u'設(shè)備已經(jīng)開機(jī)',touserName='filehelper')else:ssh_time = time.strftime(%Y-%m-%d %H:%M:%S",)time.localtime())itchat.send(ssh_time+u'開始連接遠(yuǎn)程主機(jī)',touserName='filehelper')stdin,stdout,stderr=ssh.exec_command('wakeonlan -1 192.168.1.0 14:dd:a9:ea"0b:96')itchat.send(wakeonlan_time+u'執(zhí)行喚醒,等待設(shè)備開機(jī)聯(lián)網(wǎng)',toUserName='filehelper')#由于開機(jī)需要一些時(shí)間去啟動(dòng)網(wǎng)絡(luò),所以這里等等60stime.sleep(60)#執(zhí)行ping命令,-c 1 表示只ping一下,然后過濾有沒有64,如果有則獲取64傳給sshConStatusstdin,stdout,stderr=ssh.exec_command('ping 192.168.1.182 -c 1c | grep 64 | cut -d "" -f 1|head -n 1')sshConStatus = stdout.read()sshConStatus = sshConStatus.strip('\n')print type(sshConStatus)print sshConStatus#進(jìn)行判斷,如果為64,則說明ping成功,設(shè)備已經(jīng)聯(lián)網(wǎng),可以進(jìn)行遠(yuǎn)程連接了,否則發(fā)送失敗消息if sshConStatus == '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è)備喚醒失敗,請檢查設(shè)備是否接電源',toUserName='filehelper')ssh.close()#在網(wǎng)站根目錄創(chuàng)建一個(gè)空文件,命名為 shutdownos.system('touch /www/shutdown')print '執(zhí)行開機(jī)消息成功'

關(guān)機(jī)部分實(shí)現(xiàn)

當(dāng)接收關(guān)機(jī)指令時(shí),程序會(huì)去刪除網(wǎng)站根目錄的 shutdown 文件,客戶端我寫了幾行代碼,去通過 Requests 庫每隔30s 發(fā)送 HTTP head 請求去判斷文件是否是404,如果是404 這說明文件不存在,調(diào)用系統(tǒng)關(guān)機(jī)操作,執(zhí)行關(guān)機(jī)。

然后 SSH 到樹莓派去 ping 目標(biāo)主機(jī),如果返回為空,則說明關(guān)機(jī)成功,否則關(guān)機(jī)失敗。這只是針對 Windows 的關(guān)機(jī),如果目標(biāo)主機(jī)是 Linux 則簡單多了:

if msg['Text'] == u'關(guān)機(jī)':
#刪除網(wǎng)站根目錄的shutdown 文件
rmfile = os.system('rm -rf //www/shutdown')
if rmfile == 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')
paramko.util.log_to_file('ssh_key-login.log')
privatekey = os.path.expanduser(key_file)
try:
key = paramiko.RSAKey.from_privat_key_file(privatekey)
except paramiko.PasswordRepuiredExceptin:
key = paramiko.RSAKey.from_privat_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īng)為關(guān)機(jī)需要一段時(shí)間,如果設(shè)置太短,可能網(wǎng)絡(luò)還沒斷開time.sleep(60)stdin,stdout,stderr=ssh.exec_command('ping 192.168.1.182 -c 1 | grep 64 | cut -d "" -f 1|head -n 1')sshConStatus = stdout.read()sshConStatus = sshConStatus.strip('\n')print type(sshConStatus)print sshConStatus #如果獲取的值為空,則說明已經(jīng)關(guān)機(jī),否則關(guān)機(jī)失敗if sshConStatus != '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ī)失敗,請連接桌面檢查客戶端程序是否正常執(zhí)行',toUserName='filehelper')ssh.close()

itchat.auto_login(hotReload=True,enobleCmdQR=2)
itchat.run()

客戶端代碼,寫完扔計(jì)劃任務(wù),開機(jī)啟動(dòng):

import requests
import os
import time
while 1:
time.sleep(30)
r = requests.head("heep://awen.me/shutdown")
print r.status_code
if r.status_code == 404:
os.system("shutdown -s -t 5")

使用 TeamViewer 連接:

缺點(diǎn)

網(wǎng)頁端微信必須一直登錄,不方便,這個(gè)就需要微信不能斷網(wǎng)了。

WakeOnLan 是廣播 MAC 地址的,貌似不能返回是否成功沒,所以還是要 ping 主機(jī)看看通不通,判斷下。

需要一個(gè)樹莓派做跳板機(jī),否則也不能喚醒內(nèi)網(wǎng)設(shè)備。

如果只允許自己控制最好是使用文件助手來發(fā)送消息,因?yàn)槟J(rèn)情況下,任何人都可以給你發(fā)送指令開機(jī)。

Windows需要安裝TeamViewer并且設(shè)置為開機(jī)自動(dòng)啟動(dòng)以及綁定賬號(hào)設(shè)置無人值守模式。這樣方便遠(yuǎn)程,如果是Linux 則不需要開啟 ssh 就可以了。

最后,如果你跟我一樣都喜歡python,也在學(xué)習(xí)python的道路上奔跑,歡迎你加入python學(xué)習(xí)群:839383765 群內(nèi)每天都會(huì)分享最新業(yè)內(nèi)資料,分享python免費(fèi)課程,共同交流學(xué)習(xí),讓學(xué)習(xí)變(編)成(程)一種習(xí)慣!

轉(zhuǎn)載于:https://blog.51cto.com/14186420/2359302

總結(jié)

以上是生活随笔為你收集整理的Python黑科技:在家远程遥控公司电脑,python+微信一键连接!的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。