Linux Bash/Shell通过爱快路由器开机远程电脑(利用主板和网卡Wake ON LAN原理)

使用说明:将以下代码保存为bootpc,在终端中输入bootpc命令即可,注意事先需要测试电脑主板Wake ON Lan功能是否正常,并且在爱快路由器《网络唤醒》界面添加对应设备的MAC地址;

注:以下代码中的登录验证信息和爱快路由器IP地址需要根据自己的实际情况做相应修改!

#!/bin/bash
#通过爱快路由器Wake ON LAN功能开机远程电脑
#以下第一个curl命令登录信息过程已脱敏,登录鉴权请使用浏览器F12抓包即可,根据实际情况修改xxxxx具体数据

cookieFile="/tmp/cookie.tmp.$$"

##定义默认要唤醒设备的MAC地址;可通过指定$1参数覆盖
macAddress="a8:1e:47:9b:76:80"
[ ! -z "$1" ] && macAddress="$1"

trap "rm -f $cookieFile" 0

StampToTime(){
	local connectTime
	if [ "$(type -t stamptotime)" = "file" ];
	then
		connectTime=$(stamptotime "$1")
	else
		connectTime=$(date +'%Y-%m-%d %H:%M:%S' -d "1970-01-01 UTC ${1} seconds")
	fi	
	echo "$connectTime"
}

check_jqcommand(){
	[ -z "$(type -t jq)" ] && {
		echo -e "JSON解析工具\`jq\`命令不存在,请先安装 jq 命令行工具!"
		echo -e "\n官方网址:\n\thttps://stedolan.github.io/jq/"
		exit 1
	}
}
check_jqcommand

curl -sSf 'http://192.168.10.1/Action/login' \
  -H 'Connection: keep-alive' \
  -H 'Pragma: no-cache' \
  -H 'Cache-Control: no-cache' \
  -H 'Accept: application/json, text/plain, */*' \
  -H 'DNT: 1' \
  -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36' \
  -H 'Content-Type: application/json;charset=UTF-8' \
  -H 'Origin: http://192.168.10.1' \
  -H 'Referer: http://192.168.10.1/login' \
  -H 'Accept-Language: zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7,de;q=0.6,zh-TW;q=0.5' \
  --data-binary '{"username":"xxxx","passwd":"xxxxxxxxxxxx","pass":"xxxxxxxxxxx","remember_password":""}' \
  --cookie-jar $cookieFile >/dev/null

deviceInfo=$(curl -sSf 'http://192.168.10.1/Action/call' \
  -H 'Connection: keep-alive' \
  -H 'Pragma: no-cache' \
  -H 'Cache-Control: no-cache' \
  -H 'Accept: application/json, text/plain, */*' \
  -H 'DNT: 1' \
  -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36' \
  -H 'Content-Type: application/json;charset=UTF-8' \
  -H 'Origin: http://192.168.10.1' \
  -H 'Referer: http://192.168.10.1/' \
  -H 'Accept-Language: zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7,de;q=0.6,zh-TW;q=0.5' \
  --data-binary '{"func_name":"wakeup","action":"show","param":{"TYPE":"total,data","limit":"0,20","ORDER_BY":"","ORDER":""}}' \
  -b $cookieFile)

#echo "$deviceInfo"|jq -r '.'

deviceId=$(echo "$deviceInfo"|jq -r '.Data.data[]|select(.mac=="'"${macAddress,,}"'")|.id|tostring')

if [ -z "$deviceId" ];then
	echo "MAC地址错误或设备MAC不在预定义列表..."
	exit 0
fi

echo "启动远程设备..."
echo "设备MAC:$macAddress,设备ID:$deviceId ;"

bootInfo=$(curl -sSf 'http://192.168.10.1/Action/call' \
  -H 'Connection: keep-alive' \
  -H 'Pragma: no-cache' \
  -H 'Cache-Control: no-cache' \
  -H 'Accept: application/json, text/plain, */*' \
  -H 'DNT: 1' \
  -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36' \
  -H 'Content-Type: application/json;charset=UTF-8' \
  -H 'Origin: http://192.168.10.1' \
  -H 'Referer: http://192.168.10.1/' \
  -H 'Accept-Language: zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7,de;q=0.6,zh-TW;q=0.5' \
  --data-binary '{"func_name":"wakeup","action":"wake_id","param":{"id":'"${deviceId}"'}}' \
  -b $cookieFile)
  
echo "$bootInfo"|jq -r '.'

posted @ 2022-04-02 06:02  晴云孤魂  阅读(1595)  评论(0编辑  收藏  举报