Linux Bash/Shell获取爱快路由器WAN拨号时间和在线时长

家用宽带每隔一段时间会自动重播更换IP,对于经常需要确认外网IP变动或宽带重拨时间来进行一些自动化任务的朋友来说,确认宽带拨号时间很有用,以下Bash脚本实现不用登陆爱快路由器后台即可查看宽带在线时长和下次重拨时间:

#!/bin/bash
#查询爱快路由器WAN接口拨号联网时间
#1.替换IP地址10.10.10.3为你爱快路由器的实际IP
#2.注意使用浏览器F12抓包获取你自己的爱快用户和密码加密数据,替换xxxxx内容,此数据经过观察是固定不变的
##ikuai_wan_time.sh
cookieFile="/tmp/cookie.tmp.$$"

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"
}

curl -sSf 'http://10.10.10.3/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://10.10.10.3' \
  -H 'Referer: http://10.10.10.3/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":"admin","passwd":"xxxxxxxxxxxxxxxxxxxxxx","pass":"xxxxxxxxxxxxxxx","remember_password":""}' \
  --cookie-jar $cookieFile >/dev/null

wanInfo=$(curl -sSfL 'http://10.10.10.3/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://10.10.10.3' \
  -H 'Referer: http://10.10.10.3/' \
  -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":"syslog-wanpppoe","action":"show","param":{"TYPE":"total,data,interface","ORDER_BY":"id","ORDER":"desc","limit":"0,50"}}' \
  -b $cookieFile)

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

wanUptime=$(echo "$wanInfo"|jq -r '.Data.data[0].timestamp')
wanUptimeFull=$(StampToTime "${wanUptime}")

echo "爱快拨号状态查询:"

nowTimeStamp=$(date +%s)
startupTimeStamp=$wanUptime
wanOnlineTime=$(($nowTimeStamp-$startupTimeStamp))
nextTimeStamp=$(($startupTimeStamp+172800))
wanUpHourFull=$(echo "$wanOnlineTime"|awk '{printf "%.3f",$0/3600}')
wanUpHour=$(expr $wanOnlineTime / 3600 )
wanUpMin=$(expr \( $wanOnlineTime % 3600 \) / 60 )
connectTime=$(StampToTime "${startupTimeStamp}")
nextconnectTime=$(StampToTime "${nextTimeStamp}")


echo "联网时间戳:$startupTimeStamp"
echo "下次重拨时间戳:$nextTimeStamp"
echo "WAN在线时长(小时):$wanUpHourFull 小时"
echo "WAN在线时长(详细):$wanUpHour 小时 $wanUpMin 分"
echo "ADSL拨号联网时间:$connectTime"
echo "下次重连时间:$nextconnectTime"

Linux Bash/Shell获取爱快路由器WAN拨号时间和在线时长

上一篇:网络规划-连接到广域网


下一篇:CRN:2017年最酷的软件定义网络技术