事情的起因
因?yàn)樵O(shè)備多,還怕費(fèi)電,所以懶得手動(dòng)開(kāi)關(guān)機(jī)
實(shí)戰(zhàn)環(huán)境
1.天貓精靈/小米音響 都可以
2.openwrt路由器,所有設(shè)備在同一個(gè)局域網(wǎng)(ping通,當(dāng)然不在一個(gè)也可以,不過(guò)麻煩一點(diǎn),但是必須能ping通,百度如何ping通,走網(wǎng)上說(shuō)的組網(wǎng)通道,例如向日葵x1組的網(wǎng)也行)
3.設(shè)備需要支持 wol ,用openwrt里的網(wǎng)絡(luò)喚醒試一下,能喚醒再接著看下文
4.注冊(cè) 巴法云 ,能免費(fèi)使用的都很牛x,感謝!!!
開(kāi)始發(fā)車
1.天貓精靈/小愛(ài)同學(xué) 連接 八法云
1.巴法云 https://cloud.bemfa.com/
2.添加主題
添加完成到 天貓精靈/小愛(ài)同學(xué)app 添加巴法云,具體不再贅述,百度知道
2.openwrt
由于穩(wěn)定需要害怕把openwrt給高掛,在路由器里只添加幾個(gè)腳本,其他在docker里拉一個(gè)50多md的Ubuntu容器里面搞
- ssh 連接到openwrt 推薦NxShell,其他也可以
-
vim pc_xxx.sh
#xxx為你添加設(shè)備的名字,見(jiàn)名知意就行 - 輸入i 進(jìn)入插入模式 粘貼
#!/bin/bash
echo "xxx電腦,這里只是輸出看一下,改成你的設(shè)備名字"
echo "magic package is ready to sent"
# br-lan 你的的設(shè)備所在的接口
etherwake -D -i "br-lan" "MAC地址,這里要修改啊!!!!!!"
echo "magic package has been sent"
ESC鍵,輸入 :wq
回車
4.給與執(zhí)行操作權(quán)限 chmod +x pc_xxx.sh
xxx是你自己起的名字
5.輸入 ./pc_xxx.sh
試一下 注意: etherwake軟件要有 沒(méi)有的話 opkg install etherwake
或者百度知道… 不能喚醒的話就 game over 了
有幾個(gè)電腦就見(jiàn)幾個(gè)啟動(dòng)文件,實(shí)際上一個(gè)也行不過(guò)哪有復(fù)制粘貼來(lái)三個(gè)快,一個(gè)的話需要接受mac作為參數(shù),具體百度一下…
3.docker環(huán)節(jié)
1.拉取鏡像 xxx你的端口,不能用就換一個(gè) -p xxx:22
docker run -e TZ=Asia/Shanghai -p 8022:22 -it --name ubuntuserver ubuntu:latest /bin/bash
進(jìn)入終端,openwrt上的docker
其他的docker UI都有類似功能,或者剛剛拉鏡像時(shí)別出去,直接就到控制臺(tái)了
su root
passwd 123456 #密碼自己設(shè)置
# 安裝 ssh 方便以后鏈接
apt install openssh-server
# 啟動(dòng)ssh
service ssh restart
# 設(shè)置ssh開(kāi)機(jī)啟動(dòng)
systemctl enable ssh
systemctl enable ssh
能用最好,可惜docker里不能用systemctl 著實(shí)惡心
解決方案vim /root/startup_run.sh
startup_run.sh腳本內(nèi)容:
#!/bin/bash
LOGTIME=$(date "+%Y-%m-%d %H:%M:%S")
echo "[$LOGTIME] startup run..." >>/root/startup_run.log
service ssh start >>/root/startup_run.log
給執(zhí)行權(quán)限 chmod +x /root/startup_run.sh
將腳本加入到啟動(dòng)文件中vim /root/.bashrc
在.bashrc末尾增加如下語(yǔ)句
# startup run
if [ -f /root/startup_run.sh ]; then
./root/startup_run.sh
fi
修改ssh配置允許登陸vim /etc/ssh/sshd_config
PermitRootLogin前的#好去掉,后面改成yes
PermitRootLogin yes
重啟Ubuntu容器,用ssh工具鏈接,不成功嘗試百度解決
Ubuntu安裝需要的環(huán)境
apt update
apt upgrade
apt install python3 sshpass
# 不成功嘗試換源
腳本上傳到 /root
下,就是NxShell登陸后上傳的地方
上傳 wolup.py
文件 ,注意一定要按需,有幾臺(tái)設(shè)備修改幾臺(tái)
此處代碼參考 https://www.bilibili.com/read/cv23088336/ 這個(gè)大神
還有https://cloud.bemfa.com/docs/#/
一定要改成自己的
注意:sshpass 要先使用 ssh命令登陸一下你要喚醒或者遠(yuǎn)程關(guān)機(jī)的設(shè)備,不然可能因?yàn)橐粋€(gè)登陸提示你是否登陸的yes/no導(dǎo)致程序沒(méi)有反應(yīng),然后使用sshpass -p 密碼 ssh root@openwrt的ip 登陸看看是否成功到目標(biāo)設(shè)備,代碼中這句話后面是要執(zhí)行的命令,如果到不了目標(biāo)設(shè)備就沒(méi)有下一步了
# -*- coding: utf-8
import socket
import threading
import time
import os
import datetime
# 巴法云私鑰
client_id = "巴法云私鑰"
# 主題值
top_id = "PC1001,PC2001,PC3001"
# 華碩電腦
wol1 = 'sshpass -p 密碼 ssh root@openwrt的ip ./pc_huashuo.sh'
# 聯(lián)想電腦
wol2 = 'sshpass -p 密碼 ssh root@openwrt的ip ./pc_lianxiang.sh'
# pc3指的是黑群暉
wol3 = 'sshpass -p 密碼 ssh root@openwrt的ip ./pc_san.sh'
# 局域網(wǎng)連接openssh服務(wù)器,進(jìn)行關(guān)機(jī)操作
# 注意一定要是root登陸,不然沒(méi)有權(quán)限關(guān)機(jī),windows的是你平時(shí)登陸本地賬號(hào)的名字,一定要有權(quán)限
# 華碩電腦
shutdown1 = 'sshpass -p 密碼 ssh 用戶名@設(shè)備的ip "shutdown -s -t 0"'
# 聯(lián)想電腦
shutdown2 = 'sshpass -p 密碼 ssh 用戶名@設(shè)備的ip "shutdown -s -t 0"'
# 群暉或者linux
shutdown3 = 'sshpass -p 密碼 ssh 用戶名@設(shè)備的ip "sudo shutdown -h now"'
def connTCP():
global tcp_client_socket
# 創(chuàng)建socket
tcp_client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# IP 和端口
server_ip = 'bemfa.com'
server_port = 8344
try:
# 連接服務(wù)器
tcp_client_socket.connect((server_ip, server_port))
# 發(fā)送訂閱指令
substr = 'cmd=1&uid=' + client_id + '&topic=' + top_id + '\r\n'
tcp_client_socket.send(substr.encode("utf-8"))
except():
time.sleep(2)
connTCP()
# 心跳
def Ping():
# 發(fā)送心跳
try:
keep_live = 'ping\r\n'
tcp_client_socket.send(keep_live.encode("utf-8"))
except():
time.sleep(2)
connTCP()
# 開(kāi)啟定時(shí),30秒發(fā)送一次心跳
t = threading.Timer(30, Ping)
t.start()
# 獲取當(dāng)前時(shí)間
def nowTime():
now = datetime.datetime.now()
formatted_time = now.strftime("%Y-%m-%d %H:%M:%S")
return formatted_time
connTCP()
Ping()
while True:
# 接收服務(wù)器發(fā)送過(guò)來(lái)的數(shù)據(jù)
recvData = tcp_client_socket.recv(1024)
if len(recvData) != 0:
try:
# 返回的參數(shù) cmd=2&uid=xxx&topic=PC001&msg=on
res = recvData.decode('utf-8')
if res.endswith('\r\r\n'):
res = res.replace('\r\r\n', '')
print("時(shí)間:{}-->服務(wù)啟動(dòng)".format(nowTime()))
print('時(shí)間:{}-->接收到數(shù)據(jù):{}'.format(nowTime(),res))
if 'topic=PC1001' in res:
sw = str(res.split('&')[3].split('=')[1]).strip()
if str(sw) == str("on"):
try:
print("時(shí)間:{}-->正在打開(kāi)電腦1".format(nowTime()))
os.system(wol1)
except():
time.sleep(2)
print("打開(kāi)電腦1失敗")
elif str(sw) == str("off"):
try:
print("時(shí)間:{}-->正在關(guān)閉電腦1".format(nowTime()))
os.system(shutdown1)
except():
time.sleep(2)
print("關(guān)閉電腦1失敗")
elif 'topic=PC2001' in res:
sw = str(res.split('&')[3].split('=')[1]).strip()
if str(sw) == str("on"):
try:
print("時(shí)間:{}-->正在打開(kāi)電腦2".format(nowTime()))
os.system(wol2)
except():
time.sleep(2)
print("打開(kāi)電腦2失敗")
elif str(sw) == str("off"):
try:
print("時(shí)間:{}-->正在關(guān)閉電腦2".format(nowTime()))
os.system(shutdown2)
except():
time.sleep(2)
print("關(guān)閉電腦2失敗")
if 'topic=PC3001' in res:
sw = str(res.split('&')[3].split('=')[1]).strip()
if str(sw) == str("on"):
try:
print("時(shí)間:{}-->正在打開(kāi)電腦3".format(nowTime()))
os.system(wol3)
except():
time.sleep(2)
print("打開(kāi)電腦3失敗")
elif str(sw) == str("off"):
try:
print("時(shí)間:{}-->正在關(guān)閉電腦3".format(nowTime()))
os.system(shutdown3)
except():
time.sleep(2)
print("關(guān)閉電腦3失敗")
except():
time.sleep(2)
else:
print("conn err")
connTCP()
啟動(dòng)腳本 vim wolSetup.sh
#!/bin/bash
nohup /usr/bin/python3 -u /root/wolup.py 1>/root/wolup.log 2>&1 &
給與執(zhí)行權(quán)限chmod +x wolSetup.sh
加入開(kāi)機(jī)啟動(dòng) vim .bashrc
后面加上
# wol run
if [ -f /root/wolSetup.sh ]; then
./root/wolSetup.sh
fi
以為鏈接可能會(huì)斷加入定時(shí)任務(wù)
安裝 cronapt install cron
加入定時(shí)任務(wù)crontab -e
最后插入文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-781340.html
# 每2小時(shí)啟動(dòng)一次
0 */2 * * * ps -aux | grep wolup.py | grep -v grep | awk '{print $2}' | xargs kill -15; nohup /usr/bin/python3 -u /root/wolup.py 1>/root/wolup.log 2>&1 &
注意: 把ps -aux | grep wolup.py | grep -v grep | awk '{print $2}' | xargs echo 輸入控制臺(tái)先看看是不是把pid給弄出來(lái)了,如果不是吧$后面的數(shù)字改成1,2,3…一個(gè)一個(gè)實(shí)驗(yàn),不然程序沒(méi)干掉就無(wú)法重啟
查看定時(shí)任務(wù)service cron status
由于容器的原因定時(shí)任務(wù)未能開(kāi)機(jī)啟動(dòng),模仿ssh和wolup.py的方式加入開(kāi)機(jī)啟動(dòng) vim .bashrc
在 ./root/wolSetup.sh
后面加上service cron start
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-781340.html
window也可以安裝openssh的服務(wù)端和客戶端 ,百度搜一下,關(guān)機(jī)需要使用
另外 wolup.log 是日志可以觀察一下哪個(gè)環(huán)節(jié)有問(wèn)題
到了這里,關(guān)于天貓精靈/小愛(ài)同學(xué)+巴法云+Openwrt控制局域網(wǎng)電腦/群暉開(kāi)關(guān)機(jī)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!