@ -10,28 +10,63 @@ function get_config(){
# 初始化设置信息
function read_config(){
get_config "pushbot_enable" "dd_webhook" "pushbot_ipv4" "ipv4_interface" "pushbot_ipv6" "ipv6_interface" "pushbot_up" "pushbot_down" "pushbot_sheep" "pushbot_whitelist" "pushbot_blacklist" "pushbot_interface" "starttime" "endtime" "cpuload_enable" "cpuload" "temperature_enable" "temperature" "device_name" "err_enable" "network_err_event" "err_sheep_enable" "system_time_event" "autoreboot_time" "network_restart_time" "public_ip_event" "public_ip_retry_count" "soc_code" "sleeptime" "up_timeout" "down_timeout" "device_aliases" "debuglevel" "cpuload" "temperature" "send_mode" "regular_time" "regular_time_2" "regular_time_3" "interval_time" "thread_num" "timeout_retry_count" "send_we" "we_webhook" "pp_token" "pp_channel" "pp_webhook" "pp_topic_enable" "pp_topic" "fs_webhook" "bark_srv_enable" "bark_srv" "bark_token" "err_device_aliases" "oui_dir" "oui_data" "client_usage" "client_usage_max" "client_usage_disturb" "client_usage_whitelist" "reset_regularly" "ipv4_urllist" "ipv6_urllist" "lite_enable" "content_current_device" "content_nowtime" "content_content"
get_config "pushbot_enable" "lite_enable" "device_name" "sleeptime" "oui_dir" "oui_data" "reset_regularly" "debuglevel" "device_aliases" \
"pushbot_ipv4" "ipv4_interface" "pushbot_ipv6" "ipv6_interface" "pushbot_up" "pushbot_down" "cpuload_enable" "cpuload" "temperature_enable" "temperature" \
"regular_time" "regular_time_2" "regular_time_3" "interval_time" \
"client_usage" "client_usage_max" "client_usage_disturb" "client_usage_whitelist" \
"web_logged" "ssh_logged" "web_login_failed" "ssh_login_failed" "login_max_num" "web_login_black" "ip_white_list" "ip_black_timeout" \
"pushbot_sheep" "starttime" "endtime" "pushbot_whitelist" "pushbot_blacklist" "pushbot_interface" "MAC_online_list" "MAC_offline_list" \
"up_timeout" "down_timeout" "timeout_retry_count" "thread_num" "soc_code" \
"err_enable" "err_sheep_enable" "err_device_aliases" "network_err_event" "system_time_event" "autoreboot_time" "network_restart_time" "public_ip_event" "public_ip_retry_count" \
"jsonpath" "dd_webhook" "we_webhook" "pp_token" "pp_channel" "pp_webhook" "pp_topic_enable" "pp_topic" "fs_webhook" "bark_srv_enable" "bark_srv" "bark_token"
for str_version in "wrtbwmon" "iputils-arping" "curl" "iw"; do
eval `echo ${str_version:0:2}"_version"`=`opkg list-installed|grep -w ^${str_version}|awk '{print $3}'` 2>/dev/null
done
dir="/tmp/pushbot/" && mkdir -p ${dir}
tempjsonpath="/tmp/pushbot/temp.json"
ip_blacklist_path="/usr/bin/pushbot/api/ip_blacklist"
[ ! -z "$oui_dir" ] && [ "$oui_dir" -eq "1" ] && oui_base="${dir}oui_base.txt" || oui_base="/usr/bin/pushbot/oui_base.txt"
debuglevel=`echo "$debuglevel"` && [ -z "$debuglevel" ] && logfile="/dev/null" || logfile="${dir}pushbot.log"
pushbot_blacklist=`echo "$pushbot_blacklist"|sed 's/ /\n/g'` 2>/dev/null
pushbot_whitelist=`echo "$pushbot_whitelist"|sed 's/ /\n/g'` 2>/dev/null
device_aliases=`echo "$device_aliases"|sed 's/ /\n/g'|sed 's/-/ /'` 2>/dev/null
err_device_aliases=`echo "$err_device_aliases"|sed 's/ /\n/g'` 2>/dev/null
client_usage_whitelist=`echo "$client_usage_whitelist"|sed 's/ /\n/g'` 2>/dev/null
ip_white_list=`echo "$ip_white_list"|sed 's/ /\n/g'` 2>/dev/null
mark_mac_list="${MAC_online_list} ${MAC_offline_list}"
mark_mac_list=`echo "$mark_mac_list"|sed 's/ /\n/g'|sed 's/-/ /'` 2>/dev/null
ipv4_urllist=`cat /usr/bin/pushbot/api/ipv4.list` 2>/dev/null
ipv6_urllist=`cat /usr/bin/pushbot/api/ipv6.list` 2>/dev/null
[ -z "$pushbot_ipv4" ] && pushbot_ipv4=0
[ -z "$pushbot_ipv6" ] && pushbot_ipv6=0
[ "$iw_version" ] && wlan_interface=`iw dev|grep Interface|awk '{print $2}'` >/dev/null 2>&1
[ -z "$up_timeout" ] || [ "$up_timeout" -eq "0" ] && up_timeout="2"
[ -z "$down_timeout" ] || [ "$down_timeout" -eq "0" ] && down_timeout="20";down_timeout=`expr ${down_timeout} / 2 + 1`
[ -z "$timeout_retry_count" ] && timeout_retry_count="2";[ "$timeout_retry_count" -eq "0" ] && timeout_retry_count="1"
markdown_splitline="\n\n---\n\n";markdown_linefeed="\n\n";markdown_tab=" ";markdown_space=" ";greenfont="<font color=#92D050>";bluefont="<font color=#76CCFF>";fontend="</font>";
tabletab=${bluefont}┋${fontend};boldstar="**";titlel4="####";titlebold="**";titleboldend="**";titleblue="<font color=#76CCFF>";titlegreen="<font color=#92D050>";titleend="</font>";titlered="<font color=#FF6666>"
[ ! -z "$send_we" ] && [ "$send_we" -eq "1" ] && markdown_splitline="\n\n-------\n\n" && markdown_tab=" "
[ ! -z "$send_we" ] && [ "$send_we" -eq "2" ] && percentsym=25 && markdown_splitline="%0D%0A%0D%0A" && markdown_linefeed="%0D%0A%0D%0A" && greenfont="" && bluefont="" && fontend="" && tabletab=" " && boldstar=""
[ ! -z "$send_we" ] && [ "$send_we" -eq "3" ] && titlel4="" && titlebold="【" && titleboldend="】" && titleblue="" && titlegreen="" && titleend="" && titlered="" && greenfont="" && bluefont="" && fontend="" && tabletab=" " && boldstar="" && markdown_splitline="\n---\n" && markdown_linefeed="\n"
[ ! -z "$send_we" ] && [ "$send_we" -eq "4" ] && titlel4="" && titlebold="【" && titleboldend="】" && titleblue="" && titlegreen="" && titleend="" && titlered="" && greenfont="" && bluefont="" && fontend="" && tabletab=" " && boldstar="" && markdown_splitline="%0D%0A%0D%0A" && markdown_linefeed="%0D%0A%0D%0A" && percentsym=25 && [ -z "$bark_srv" ] && bark_srv="https://api.day.app"
# [ ! -z "$content_content" ] && [ "$content_content" -eq "1" ] && str_title_start="" && str_title_end="" && str_splitline="" && str_linefeed="" && str_tab=""
[ ! -z "$bark_token" ] && [ -z "$bark_srv" ] && bark_srv="https://api.day.app"
# 字符串读取
str_title_start=`/usr/bin/jq -r '.str_title_start' ${jsonpath}`
str_title_end=`/usr/bin/jq -r '.str_title_end' ${jsonpath}`
str_linefeed=`/usr/bin/jq -r '.str_linefeed' ${jsonpath}`
str_splitline=`/usr/bin/jq -r '.str_splitline' ${jsonpath}`
str_space=`/usr/bin/jq -r '.str_space' ${jsonpath}`
str_tab=`/usr/bin/jq -r '.str_tab' ${jsonpath}`
font_red=`/usr/bin/jq -r '.font_red' ${jsonpath}`
font_green=`/usr/bin/jq -r '.font_green' ${jsonpath}`
font_green2=`/usr/bin/jq -r '.font_green2' ${jsonpath}`
font_blue=`/usr/bin/jq -r '.font_blue' ${jsonpath}`
font_purple=`/usr/bin/jq -r '.font_purple' ${jsonpath}`
font_end=`/usr/bin/jq -r '.font_end' ${jsonpath}`
font_end2=`/usr/bin/jq -r '.font_end2' ${jsonpath}`
percentsym=`/usr/bin/jq -r '.percentsym' ${jsonpath}`
boldstar=`/usr/bin/jq -r '.boldstar' ${jsonpath}`
table_tab=`/usr/bin/jq -r '.tabletab' ${jsonpath}`
( echo "$lite_enable"|grep -q "content" ) && str_title_start="" && str_title_end="" && str_splitline="" && str_linefeed="" && str_tab=""
}
# 初始化
function pushbot_init(){
@ -43,15 +78,34 @@ function pushbot_init(){
fi
down_oui &
deltemp
get_syslog
add_ip_black
rm -f ${dir}fd1 ${dir}sheep_usage ${dir}old_sheep_usage ${dir}client_usage_aliases ${dir}old_client_usage_aliases /usr/bin/pushbot/errlog >/dev/null 2>&1
[ ! -f "/usr/sbin/wrtbwmon" ] && echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!】未安装 wrtbwmon ,流量统计不可用" >> ${logfile}
[ -z "$ip_version" ] && echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!】无法获取依赖项 iputils-arping 版本号,请确认插件是否正常运行" >> ${logfile}
[ -z "$cu_version" ] && echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!】无法获取依赖项 curl 版本号,请确认插件是否正常运行" >> ${logfile}
[ -z "$dd_webhook" ] && [ -z "$pp_token" ] && [ -z "$we_webhook" ] && [ -z "$fs_webhook" ] && [ -z "$bark_token " ] && echo "`date "+%Y-%m-%d %H:%M:%S"` 【! ! ! 】请填写正确的Token/Webhook " >> ${logfile} && return 1
[ -z "${dd_webhook}${pp_token}${we_webhook}${fs_webhook}${bark_token} " ] && echo "`date "+%Y-%m-%d %H:%M:%S"` 【! ! ! 】请填写正确的Token/Webhook " >> ${logfile} && return 1
local interfacelist=`getinterfacelist` && [ -z "$interfacelist" ] && echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!】无法正确获取接口信息,请确认插件是否正常运行" >> ${logfile}
return 0
}
# 推送
function diy_send(){
( ! echo "$lite_enable"|grep -q "content" ) && ( ! echo "$lite_enable"|grep -q "nowtime" ) && local nowtime=`date "+%Y-%m-%d %H:%M:%S"`
local diyurl=`/usr/bin/jq -r .url ${3}` && local diyurl=`eval echo ${diyurl}`
local type=`/usr/bin/jq -r '.type' ${3}` && local type=`eval echo ${type}`
local data=`/usr/bin/jq -r '.data' ${3}` && local data=`eval echo ${data}`
local content_type=`/usr/bin/jq -r '.content_type' ${3}`
/usr/bin/jq ".type + $type" ${jsonpath} > ${tempjsonpath}
/usr/bin/jq -r '.[]' ${tempjsonpath}|grep -w "null" && echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!】参数值错误,请检查设置项 `/usr/bin/jq -r '.' ${tempjsonpath}|grep "null"`" >> ${logfile} && return 1
[ -f ${tempjsonpath} ] && local logrow=$(grep -c "" ${tempjsonpath}) || local logrow="0"
[ $logrow -eq "0" ] && echo "`date "+%Y-%m-%d %H:%M:%S"` 【! ! ! 】json 文件生成失败,请检查文件格式" >> ${logfile} && return 1
/usr/bin/jq -r '.[]' ${tempjsonpath}|grep "null" && echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!】参数变量生成失败,请检查设置项 `/usr/bin/jq -r '.' ${tempjsonpath}|grep "null"`" >> ${logfile}
curl -X POST -H "$content_type" -d "${data}" "${diyurl}"
}
# 下载设备MAC厂商信息
function down_oui(){
[ -f ${oui_base} ] && local logrow=$(grep -c "" ${oui_base}) || local logrow="0"
@ -74,8 +128,8 @@ utstarcom\|volans\|xerox\|xiaomi\|zdc\|zhongxing\|smartisan" > ${oui_base} && ec
# 清理临时文件
function deltemp(){
unset title content
rm -f ${dir}title ${dir}content ${dir}top ${dir}t mp_downlist ${dir}send_enable.lock >/dev/null 2>&1
unset title content ipAddress_logrow online_list online_mac mac_online_status
rm -f ${dir}title ${dir}content ${dir}tmp_downlist ${dir}send_enable.lock ${tempjsonpath} >/dev/null 2>&1
LockFile unlock
[ -f ${logfile} ] && local logrow=$(grep -c "" ${logfile}) || local logrow="0"
[ $logrow -gt 500 ] && sed -i '1,100d' ${logfile} && echo "`date "+%Y-%m-%d %H:%M:%S"` 【清理】日志超出上限,删除前 100 条" >> ${logfile}
@ -99,16 +153,15 @@ function getip(){
elif [ $1 == "hostipv4" ] ;then
function get_hostipv4()
{
[ -z "$ipv4_urllist" ] && local ipv4_urllist="www.cip.cc myip.ipip.net ipv4.ddnspod.com ifcfg.cn speed.neu.edu.cn/getIP.php ddns.oray.com/checkip www.net.cn/static/customercare/yourip.asp"
local url_number=`expr $(echo "$ipv4_urllist"|grep -o ' '|wc -l) + 1`
local ipv4_URL=`echo "$ipv4_urllist"|awk -v i=$(rand 1 $url_number) '{print $i}'`
local url_number=`echo "$ipv4_urllist"|wc -l`
local ipv4_URL=`echo "$ipv4_urllist"| sed -n "$(rand 1 $url_number)p"|sed -e 's/\r//g'`
[ ! -z "$ipv4_interface" ] && local hostIP=$(curl -k -s -4 --interface ${ipv4_interface} -m 5 ${ipv4_URL}) || local hostIP=$(curl -k -s -4 -m 5 ${ipv4_URL})
echo $hostIP|grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'|head -n1
}
local hostIP=`get_hostipv4`
[ -z "$hostIP" ] && local hostIP=`get_hostipv4`
[ -z "$hostIP" ] && local hostIP=`get_hostipv4`
echo $hostIP
echo $hostIP # 重试,偷懒,有空再优化
elif [ $1 == "wanipv6" ] ;then
[ ! -z "$ipv6_interface" ] && local wanIPv6=$(ip addr show ${ipv6_interface}|grep -v deprecated|grep -A1 'inet6 [^f:]'|sed -nr ':a;N;s#^ +inet6 ([a-f0-9:]+)/.+? scope global .*? valid_lft ([0-9]+sec) .*#\2 \1#p;ta'|sort -nr|head -n1|awk '{print $2}')
[ -z "$ipv6_interface" ] && local wanIPv6=$(ip addr show|grep -v deprecated|grep -A1 'inet6 [^f:]'|sed -nr ':a;N;s#^ +inet6 ([a-f0-9:]+)/.+? scope global .*? valid_lft ([0-9]+sec) .*#\2 \1#p;ta'|sort -nr|head -n1|awk '{print $2}')
@ -116,11 +169,10 @@ function getip(){
elif [ $1 == "hostipv6" ] ;then
function get_hostipv6()
{
[ -z "$ipv6_urllist" ] && local ipv6_urllist="ip.sb ipv6.ddnspod.com api-ipv6.ip.sb/ip speed.neu6.edu.cn/getIP.php v6.myip.la/json"
local urlv6_number=`expr $(echo "$ipv6_urllist"|grep -o ' '|wc -l) + 1`
local ipv6_URL=`echo "$ipv6_urllist"|awk -v i=$(rand 1 $url_number) '{print $i}'`
local urlv6_number=`echo "$ipv6_urllist"|wc -l`
local ipv6_URL=`echo "$ipv6_urllist"| sed -n "$(rand 1 $urlv6_number)p"|sed -e 's/\r//g'`
[ ! -z "$ipv6_interface" ] && local hostIPv6=$(curl -k -s -6 --interface ${ipv6_interface} -m 5 ${ipv6_URL}) || local hostIPv6=$(curl -k -s -6 -m 5 ${ipv6_URL})
echo $hostIPv6|grep -oE '([\da-fA-F0-9]{1,4}(:{1,2})){1,15}[\da-fA-F0-9]{1,4}'
echo $hostIPv6|grep -oE '([\da-fA-F0-9]{1,4}(:{1,2})){1,15}[\da-fA-F0-9]{1,4}'|head -n1
}
local hostIPv6=`get_hostipv6`
[ -z "$hostIPv6" ] && local hostIPv6=`get_hostipv6`
@ -210,17 +262,18 @@ function getcpu(){
# 获取SOC温度 (取所有传感器温度最大值)
function soc_temp(){
[ -z "$soc_code" ] && soc_code="cat /sys/class/thermal/thermal_zone*/temp|sort -nr|head -n1|cut -c-2"
[ "$soc_code" == "sensors" ] && soc_code="sensors|grep °C|sed -nr 's#^.*:.*\+(.*)°C .*#\1#gp'|sort -nr|head -n1"
echo "$soc_code"|awk '{run=$0;system(run)}' 2>/dev/null
[ -z "$soc_code" ] && local soctemp=`sensors 2>/dev/null|grep °C|sed -nr 's#^.*:.*\+(.*)°C .*#\1#gp'|sort -nr|head -n1`
[ -z "$soc_code" ] && [ -z "$soctemp" ] && local soctemp=`cat /sys/class/thermal/thermal_zone*/temp 2>/dev/null|sort -nr|head -n1|cut -c-2`
[ ! -z "$soctemp" ] && echo "$soctemp" && return
[ ! -z "$soc_code" ] && echo "$soc_code"|awk '{run=$0;system(run)}' 2>/dev/null
}
# 流量数据
function usage(){
[ ! -f "/usr/sbin/wrtbwmon" ] || [ ! "$1" ] && return
if [ $1 == "update" ] ;then
function version_le() { test "$(echo "$@"|tr " " "\n"|sort -V |head -n 1)" == "$1"; }
function version_ge() { test "$(echo "$@"|tr " " "\n"|sort -rV |head -n 1)" == "$1"; }
function version_le() { test "$(echo "$@"|tr " " "\n"|sort -n |head -n 1)" == "$1"; }
function version_ge() { test "$(echo "$@"|tr " " "\n"|sort -r|head -n 1)" == "$1"; }
[ ! -z "$wr_version" ] && ( version_ge "${wr_version}" "1.2.0" ) && wrtbwmon -f ${dir}usage.db 2>/dev/null && return
[ ! -z "$wr_version" ] && ( version_le "${wr_version}" "1.0.0" ) || [ -z "$wr_version" ] && wrtbwmon update ${dir}usage.db 2>/dev/null && return
elif [ $1 == "get" ] ;then
@ -239,7 +292,7 @@ function usage(){
# 流量数据单位换算
function bytes_for_humans {
[ ! "$1" ] && return
[ "$1" -gt 1073741824 ] && echo "`awk 'BEGIN{printf "%.2f\n",'$1'/'1073741824'}'`G" && return
[ "$1" -gt 1073741824 ] && echo "`awk 'BEGIN{printf "%.2f\n",'$1'/'1073741824'}'` G" && return
[ "$1" -gt 1048576 ] && echo "`awk 'BEGIN{printf "%.2f\n",'$1'/'1048576'}'` M" && return
[ "$1" -gt 1024 ] && echo "`awk 'BEGIN{printf "%.2f\n",'$1'/'1024'}'` K" && return
echo "${1} bytes"
@ -271,18 +324,18 @@ function get_client_usage(){
local ip_name=`getname ${ip} ${mac}`
local tmp_usage=$(bytes_for_humans $(expr `usage get ${mac} bytes` - ${get_client_usage_bytes}))
local time_up=`cat ${dir}ipAddress|grep -w ${ip}|awk '{print $4}'|grep -v "^$"|sort -u|head -n1`
local ip_total=`usage get $mac` && [ ! -z "$ip_total" ] && local ip_total="${markdown_linefeed}${markdown_tab}总计流量: ${markdown_space}${markdown_space}${markdown_space}${markdown _space}${ip_total}"
local ip_total=`usage get $mac` && [ ! -z "$ip_total" ] && local ip_total="${str_linefeed}${str_tab}总计流量: ${str_space}${str_space}${str_space}${str _space}${ip_total}"
local time1=`date +%s`
local time1=$(time_for_humans `expr ${time1} - ${time_up}`)
if [ -z "$title" ]; then
title="${ip_name} 流量异常"
content="${content}${markdown_splitline}${titlel4} ${titlered}设备流量异常${titleend}${markdown_linefeed}${markdown_tab}客户端名:${markdown_space}${markdown_space}${markdown_space}${markdown_space}${markdown_space}${ip_name}${markdown_linefeed}${markdown_tab}客户端IP: ${markdown_space}${markdown_space}${markdown_space}${markdown_space}${ip}${markdown_linefeed}${markdown_tab}客户端MAC: ${markdown_space}${markdown_space}${markdown_space}${markdown_space}${mac}$ip_total${markdown_linefeed}${markdown_tab}一分钟内流量: ${markdown_space}${markdown_space}${tmp_usage}${markdown_linefeed}${markdown_tab}在线时间: ${markdown_space}${markdown_space}${markdown_space}${markdown _space}${time1}"
content="${content}${str_splitline}${str_title_start}${font_red} 设备流量异常${font_end}${str_title_end}${str_linefeed}${str_tab}客户端名:${str_space}${str_space}${str_space}${str_space}${str_space}${ip_name}${str_linefeed}${str_tab}客户端IP: ${str_space}${str_space}${str_space}${str_space}${ip}${str_linefeed}${str_tab}客户端MAC: ${str_space}${str_space}${str_space}${str_space}${mac}$ip_total${str_linefeed}${str_tab}一分钟内流量: ${str_space}${str_space}${tmp_usage}${str_linefeed}${str_tab}在线时间: ${str_space}${str_space}${str_space}${str _space}${time1}"
elif ( echo "$title"|grep -q "流量异常" ); then
title="${ip_name} ${title}"
content="${content}${markdown_splitline}${markdown_tab}客户端名:${markdown_space}${markdown_space}${markdown_space}${markdown_space}${markdown_space}${ip_name}${markdown_linefeed}${markdown_tab}客户端IP: ${markdown_space}${markdown_space}${markdown_space}${markdown_space}${ip}${markdown_linefeed}${markdown_tab}客户端MAC: ${markdown_space}${markdown_space}${markdown_space}${markdown_space}${mac}$ip_total${markdown_linefeed}${markdown_tab}一分钟内流量: ${markdown_space}${markdown_space}${markdown_space}${tmp_usage}${markdown_linefeed}${markdown_tab}在线时间: ${markdown_space}${markdown_space}${markdown_space}${markdown _space}${time1}"
content="${content}${str_splitline}${str_tab}客户端名:${str_space}${str_space}${str_space}${str_space}${str_space}${ip_name}${str_linefeed}${str_tab}客户端IP: ${str_space}${str_space}${str_space}${str_space}${ip}${str_linefeed}${str_tab}客户端MAC: ${str_space}${str_space}${str_space}${str_space}${mac}$ip_total${str_linefeed}${str_tab}一分钟内流量: ${str_space}${str_space}${str_space}${tmp_usage}${str_linefeed}${str_tab}在线时间: ${str_space}${str_space}${str_space}${str _space}${time1}"
else
title="设备状态变化"
content="${content}${markdown_splitline}${titlel4} ${titlered}设备流量异常${titleend}${markdown_linefeed}${markdown_tab}客户端名:${markdown_space}${markdown_space}${markdown_space}${markdown_space}${markdown_space}${ip_name}${markdown_linefeed}${markdown_tab}客户端IP: ${markdown_space}${markdown_space}${markdown_space}${markdown_space}${ip}${markdown_linefeed}${markdown_tab}客户端MAC: ${markdown_space}${markdown_space}${markdown_space}${markdown_space}${mac}$ip_total${markdown_linefeed}${markdown_tab}一分钟内流量: ${markdown_space}${markdown_space}${markdown_space}${tmp_usage}${markdown_linefeed}${markdown_tab}在线时间: ${markdown_space}${markdown_space}${markdown_space}${markdown _space}${time1}"
content="${content}${str_splitline}${str_title_start}${font_red} 设备流量异常${font_end}${str_title_end}${str_linefeed}${str_tab}客户端名:${str_space}${str_space}${str_space}${str_space}${str_space}${ip_name}${str_linefeed}${str_tab}客户端IP: ${str_space}${str_space}${str_space}${str_space}${ip}${str_linefeed}${str_tab}客户端MAC: ${str_space}${str_space}${str_space}${str_space}${mac}$ip_total${str_linefeed}${str_tab}一分钟内流量: ${str_space}${str_space}${str_space}${tmp_usage}${str_linefeed}${str_tab}在线时间: ${str_space}${str_space}${str_space}${str _space}${time1}"
fi
fi
done
@ -342,9 +395,9 @@ function cut_str {
# 随机数
function rand(){
min=$1
max=$(($2- $min + 1))
num=$(date +%s%N)
local min=$1
local max=$(($2- $min + 1))
local num=$(date +%s%N)
echo $(($num % $max + $min))
}
@ -360,7 +413,7 @@ function pushbot_first(){
done
wait
unset ip IPLIST
local IPLIST=`cat /proc/net/arp|grep "0x2\|0x6"|awk '{print $1}'|grep -v "^169.254."|grep -v "^$"|sort -u`
local IPLIST=`cat /proc/net/arp|grep "0x2\|0x6"|awk '{print $1}'|grep -v "^169.254."|grep -v "^$"|sort -u|grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' `
for ip in $IPLIST; do
read -u 5
{
@ -370,7 +423,6 @@ function pushbot_first(){
done
wait
}
# 创建计划任务
function pushbot_cron(){
function del_cron(){
@ -381,11 +433,11 @@ function pushbot_cron(){
/etc/init.d/cron start
}
del_cron
if [ -z "$pushbot_enable" ] || [ -z "$send_mode" -a -z "$reset_regularly" ] ; then
if [ -z "$pushbot_enable" ]; then
re_cron
return
fi
# 重置流量
if [ ! -z "$reset_regularly" ] && [ "$reset_regularly" -eq "1" ]; then
crontab -l 2>/dev/null > conf && echo -e "0 0 * * * rm /tmp/pushbot/usage.db >/dev/null 2>&1" >> conf && crontab conf && rm -f conf >/dev/null 2>&1
@ -409,8 +461,7 @@ function pushbot_disturb(){
if [ `date +%H` -ge $endtime -a $starttime -lt $endtime ] || [ `date +%H` -lt $starttime -a $starttime -lt $endtime ] || [ `date +%H` -lt $starttime -a `date +%H` -ge $endtime -a $starttime -gt $endtime ]; then
unset sheep_starttime
rm -f ${dir}sheep_usage ${dir}old_sheep_usage 2>/dev/null
disturb_text="【钉钉推送】"
[ ! -z "$send_we" ] && [ "$send_we" -eq "1" ] && disturb_text="【企业微信推送】"
disturb_text=`/usr/bin/jq -r '._api' ${jsonpath}`
return 0
else
[ -z "$sheep_starttime" ] && echo "`date "+%Y-%m-%d %H:%M:%S"` 【免打扰】夜深了,该休息了" >> ${logfile} && sheep_starttime=`date +%s`
@ -436,17 +487,20 @@ function LockFile(){
LockFile lock
fi
[ $1 = "unlock" ] && rm -f ${dir}pushbot.lock >/dev/null 2>&1
return
return 0
}
# 检测黑白名单
function blackwhitelist(){
[ ! "$1" ] && return 1
[ -z "$pushbot_whitelist" ] && [ -z "$pushbot_blacklist" ] && [ -z "$pushbot_interface" ] && return
[ ! -z "$pushbot_whitelist" ] && ( ! echo "$pushbot_whitelist"|grep -q -i -w $1) && return
[ ! -z "$pushbot_blacklist" ] && ( echo "$pushbot_blacklist"|grep -q -i -w $1) && return
[ ! -z "$pushbot_interface" ] && ( echo `getinterface ${1}`|grep -q -i -w $pushbot_interface ) && return
return 1
[ -z "$pushbot_whitelist" ] && [ -z "$pushbot_blacklist" ] && [ -z "$pushbot_interface" ] && [ -z "$MAC_online_list" ] && [ -z "$MAC_offline_list" ] && return 0
[ ! -z "$pushbot_whitelist" ] && ( echo "$pushbot_whitelist"|grep -q -i -w $1 ) && return 1
[ ! -z "$pushbot_blacklist" ] && ( ! echo "$pushbot_blacklist"|grep -q -i -w $1 ) && return 1
[ ! -z "$pushbot_interface" ] && ( ! echo `getinterface ${1}`|grep -q -i -w $pushbot_interface ) && return 1
[ ! -z "$MAC_online_list" ] && [ ! -z "$mac_online_status" ] && return 1
[ ! -z "$MAC_online_list" ] && ( echo "$MAC_online_list"|grep -q -i -w $1 ) && return 1
[ ! -z "$MAC_offline_list" ] && [ -z "$mac_online_status" ] && return 1
return 0
}
function get_client(){
@ -514,7 +568,7 @@ function unattended(){
[ -z "$err_enable" ] || [ "$err_enable" -ne "1" ] && return
[ ! -z "$err_sheep_enable" ] && [ "$err_sheep_enable" -eq "1" ] && [ -z "$sheep_starttime" ] && return
geterrdevicealiases;[ $? -eq "1" ] && return
if [ ! -z "$system_time_event" ]; then
local interfaceuptime=`getinterfaceuptime`
if [ ! -z "$autoreboot_time" ] && [ `cat /proc/uptime|awk -F. '{run_hour=$1/3600;printf("%d",run_hour)}'` -ge "$autoreboot_time" ] && [ "$system_time_event" -eq "1" ]; then
@ -659,39 +713,41 @@ function ip_changes(){
echo "`date "+%Y-%m-%d %H:%M:%S"` ${disturb_text}当前IP: ${IPv4}" >> ${logfile}
echo IPv4 $IPv4 > ${dir}ip && echo -e IPv6 $last_IPv6 >> ${dir}ip
title="IP 地址变化"
content="${content}${markdown_splitline}${titlel4} ${titlegreen}IP 地址变化${titleend}${markdown_linefeed}${markdown _tab}当前 IP: ${IPv4}"
content="${content}${str_splitline}${str_title_start}${font_green} IP 地址变化${font_end}${str_title_end}${str_linefeed}${str _tab}当前 IP: ${IPv4}"
elif [ ! -z "$pushbot_ipv4" ] && [ "$pushbot_ipv4" -ne "0" ] && [ -z "$IPv4" ]; then
echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!】获取 IPv4 地址失败" >> ${logfile}
fi
if [ ! -z "$pushbot_ipv6" ] && [ "$pushbot_ipv6" -ne "0" ] && [ ! -z "$IPv6" ] && ( ! echo "$IPv6"|grep -w -q ${last_IPv6} ); then
echo "`date "+%Y-%m-%d %H:%M:%S"` ${disturb_text}当前IPv6: ${IPv6}" >> ${logfile}
echo IPv4 $IPv4 > ${dir}ip && echo -e IPv6 $IPv6 >> ${dir}ip
[ -z "$title" ] && title="IPv6 地址变化"
[ ! -z "$title" ] && title="IP 地址变化"
content="${content}${markdown_splitline}${titlel4} ${titlegreen}IPv6 地址变化${titleend}${markdown_linefeed}${markdown_tab}当前 IPv6: ${IPv6}"
content="${content}${str_splitline}${str_title_start}${font_green} IPv6 地址变化${font_end}${str_title_end}${str_linefeed}${str_tab}当前 IPv6: ${IPv6}"
elif [ ! -z "$pushbot_ipv6" ] && [ "$pushbot_ipv6" -ne "0" ] && [ -z "$IPv6" ]; then
echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!】获取 IPv6 地址失败" >> ${logfile}
fi
else
echo "`date "+%Y-%m-%d %H:%M:%S"` ${disturb_text}路由器已经重启!" >> ${logfile}
[ ! -z "$pushbot_ipv4" ] && [ "$pushbot_ipv4" -ne "0" ] && echo "`date "+%Y-%m-%d %H:%M:%S"` 当前IP: ${IPv4}" >> ${logfile}
[ ! -z "$pushbot_ipv6" ] && [ "$pushbot_ipv6" -ne "0" ] && echo "`date "+%Y-%m-%d %H:%M:%S"` 当前IPv6: ${IPv6}" >> ${logfile}
echo IPv4 $IPv4 > ${dir}ip && echo -e IPv6 $IPv6 >> ${dir}ip
title="路由器重新启动"
content="${content}${markdown_splitline}${titlel4} ${titlegreen}路由器重新启动${title end}"
[ ! -z "$pushbot_ipv4" ] && [ "$pushbot_ipv4" -ne "0" ] && content="${content}${markdown_linefeed}${markdown _tab}当前IP: ${IPv4}"
[ ! -z "$pushbot_ipv6" ] && [ "$pushbot_ipv6" -ne "0" ] && content="${content}${markdown_linefeed}${markdown _tab}当前IPv6: ${IPv6}"
content="${content}${str_splitline}${str_title_start}${font_green} 路由器重新启动${font_end}${str_title_ end}"
[ ! -z "$pushbot_ipv4" ] && [ "$pushbot_ipv4" -ne "0" ] && content="${content}${str_linefeed}${str _tab}当前IP: ${IPv4}"
[ ! -z "$pushbot_ipv6" ] && [ "$pushbot_ipv6" -ne "0" ] && content="${content}${str_linefeed}${str _tab}当前IPv6: ${IPv6}"
fi
if [ ! -z "$content" ] ;then
[ -z "$ddns_enabled" ] && ddns_enabled=$(uci show ddns|grep "enabled"|grep "1")
[ -z "$ddns_enabled" ] && ddns_logrow=0 || ddns_logrow=$(echo "$ddns_enabled"|wc -l)
if [ $ddns_logrow -ge 1 ]; then
/etc/init.d/ddns stop >/dev/null 2>&1
sleep 10
/etc/init.d/ddns start >/dev/null 2>&1
/etc/init.d/ddns restart >/dev/null 2>&1
fi
[ -z "$zerotier_enabled" ] && zerotier_enabled=$(uci get zerotier.sample_config.enabled)
if [ ! -z "$zerotier_enabled" ] && [ $zerotier_enabled -eq "1" ] ; then
/etc/init.d/zerotier restart >/dev/null 2>&1
fi
fi
}
@ -700,34 +756,46 @@ function ip_changes(){
function up(){
[ -f ${dir}ipAddress ] && ( cat ${dir}ipAddress|grep -q -w $1 ) && return
local ip_mac=`getmac $1`
local ip_name=`getname ${1} ${ip_mac}`
local ip_interface=`getinterface ${ip_mac}`
getping ${1} ${up_timeout} "1";local ping_online=$?
if [ "$ping_online" -eq "0" ]; then
LockFile lock
[ ! -z "$pushbot_blacklist" ] && local tmp_mac=`echo "${pushbot_blacklist}"|grep -w -i ${ip_mac}`
[ ! -z "$pushbot_whitelist" ] && local tmp_mac=`echo "${pushbot_whitelist}"|grep -w -i ${ip_mac}`
if [ ! -z "$tmp_mac" ] && ( cat ${dir}ipAddress|grep -q -w -i ${tmp_mac} ); then
usage down $1
echo "$1 ${ip_mac} ${ip_name} `date +%s` ${ip_interface}" >> ${dir}ipAddress
LockFile unlock && return
elif [ ! -z "$tmp_mac" ] && [ -f "${dir}tmp_downlist" ] && ( cat ${dir}tmp_downip|grep -q -w -i ${tmp_mac} ); then
local tmp_downip=`cat ${dir}tmp_downlist|grep -w -i ${tmp_mac}|awk '{print $1}'|grep -v "^$"|sort -u|head -n1`
usage down $tmp_downip
sed -i "/^${tmp_downip} /d" ${dir}tmp_downlist
LockFile unlock && return
fi
[ -f "${dir}tmp_downlist" ] && local tmp_downip=`cat ${dir}tmp_downlist|grep -w ${1}|grep -v "^$"|sort -u|head -n1`
if [ ! -z "$tmp_downip" ]; then
cat ${dir}tmp_downlist|grep -w ${1}|grep -v "^$"|sort -u|head -n1 >> ${dir}ipAddress
sed -i "/^${1} /d" ${dir}tmp_downlist
else
usage down $1
local ip_name=`getname ${1} ${ip_mac}`
blackwhitelist ${ip_mac};local ip_blackwhite=$?
echo "$1 ${ip_mac} ${ip_name} `date +%s` ${ip_interface}" >> ${dir}ipAddress
blackwhitelist ${ip_mac};local ip_blackwhite=$?
[ -f "${dir}send_enable.lock" ] || [ -z "$pushbot_up" ] || [ -z "$ip_blackwhite" ] && LockFile unlock && return
[ ! -z "$pushbot_up" ] && [ "$pushbot_up" -ne "1" ] && LockFile unlock && return
[ ! -z "$ip_blackwhite" ] && [ "$ip_blackwhite" -ne "0" ] && LockFile unlock && return
[ -z "$ip_blackwhite" ] || [ "$ip_blackwhite" -ne "0" ] && LockFile unlock && return
[ -f "${dir}title" ] && local title=`cat ${dir}title`
[ -f "${dir}content" ] && local content=`cat ${dir}content`
[ -f "${dir}content" ] && local content=`cat ${dir}content`
if [ -z "$title" ]; then
local title="$ip_name 连接了你的路由器"
local content="${markdown_splitline}${titlel4} ${titlegreen}新设备连接${titleend}${markdown_linefeed}${markdown_tab}客户端名:${markdown_space}${markdown_space}${markdown_space}${markdown_space}${markdown_space}${ip_name}${markdown_linefeed}${markdown_tab}客户端IP: ${markdown_space}${markdown_space}${markdown_space}${markdown_space}${1}${markdown_linefeed}${markdown_tab}客户端MAC: ${markdown_space}${markdown_space}${markdown_space}${markdown_space}${ip_mac}${markdown_linefeed}${markdown_tab}网络接口:${markdown_space}${markdown_space}${markdown_space}${markdown_space}${markdown _space}${ip_interface}"
local content="${str_splitline}${str_title_start}${font_green} 新设备连接${font_end}${str_title_end}${str_linefeed}${str_tab}客户端名:${str_space}${str_space}${str_space}${str_space}${str_space}${ip_name}${str_linefeed}${str_tab}客户端IP: ${str_space}${str_space}${str_space}${str_space}${1}${str_linefeed}${str_tab}客户端MAC: ${str_space}${str_space}${str_space}${str_space}${ip_mac}${str_linefeed}${str_tab}网络接口:${str_space}${str_space}${str_space}${str_space}${str _space}${ip_interface}"
elif ( echo ${title}|grep -q "连接了你的路由器" ); then
local title="${ip_name} ${title}"
local content="${markdown_splitline}${markdown_tab}客户端名:${markdown_space}${markdown_space}${markdown_space}${markdown_space}${markdown_space}${ip_name}${markdown_linefeed}${markdown_tab}客户端IP: ${markdown_space}${markdown_space}${markdown_space}${markdown_space}${1}${markdown_linefeed}${markdown_tab}客户端MAC: ${markdown_space}${markdown_space}${markdown_space}${markdown_space}${ip_mac}${markdown_linefeed}${markdown_tab}网络接口:${markdown_space}${markdown_space}${markdown_space}${markdown_space}${markdown _space}${ip_interface}"
local content="${str_splitline}${str_tab}客户端名:${str_space}${str_space}${str_space}${str_space}${str_space}${ip_name}${str_linefeed}${str_tab}客户端IP: ${str_space}${str_space}${str_space}${str_space}${1}${str_linefeed}${str_tab}客户端MAC: ${str_space}${str_space}${str_space}${str_space}${ip_mac}${str_linefeed}${str_tab}网络接口:${str_space}${str_space}${str_space}${str_space}${str _space}${ip_interface}"
else
local title="设备状态变化"
local content="${markdown_splitline}${titlel4} ${titlegreen}新设备连接${titleend}${markdown_linefeed}${markdown_tab}客户端名:${markdown_space}${markdown_space}${markdown_space}${markdown_space}${markdown_space}${ip_name}${markdown_linefeed}${markdown_tab}客户端IP: ${markdown_space}${markdown_space}${markdown_space}${markdown_space}${1}${markdown_linefeed}${markdown_tab}客户端MAC: ${markdown_space}${markdown_space}${markdown_space}${markdown_space}${ip_mac}${markdown_linefeed}${markdown_tab}网络接口:${markdown_space}${markdown_space}${markdown_space}${markdown_space}${markdown _space}${ip_interface}"
fi
local content="${str_splitline}${str_title_start}${font_green} 新设备连接${font_end}${str_title_end}${str_linefeed}${str_tab}客户端名:${str_space}${str_space}${str_space}${str_space}${str_space}${ip_name}${str_linefeed}${str_tab}客户端IP: ${str_space}${str_space}${str_space}${str_space}${1}${str_linefeed}${str_tab}客户端MAC: ${str_space}${str_space}${str_space}${str_space}${ip_mac}${str_linefeed}${str_tab}网络接口:${str_space}${str_space}${str_space}${str_space}${str _space}${ip_interface}"
fi
echo "`date "+%Y-%m-%d %H:%M:%S"` ${disturb_text}新设备 ${ip_name} ${1} 连接了">> ${logfile}
#[ ! -z "$pushbot_blacklist" ] && local title="你偷偷关注的设备上线了"
[ ! -z "$title" ] && echo "$title" >${dir}title
@ -769,21 +837,24 @@ function down_send(){
blackwhitelist ${ip_mac};local ip_blackwhite=$?
[ -z "$pushbot_down" ] || [ -z "$ip_blackwhite" ] && continue
[ ! -z "$pushbot_down" ] && [ "$pushbot_down" -ne "1" ] && continue
[ ! -z "$ip_blackwhite" ] && [ "$ip_blackwhite" -ne "0" ] && continue
[ -z "$ip_blackwhite" ] || [ "$ip_blackwhite" -ne "0" ] && continue
[ ! -z "$pushbot_blacklist" ] && local tmp_mac=`echo "${pushbot_blacklist}"|grep -w -i ${ip_mac}`
[ ! -z "$pushbot_whitelist" ] && local tmp_mac=`echo "${pushbot_whitelist}"|grep -w -i ${ip_mac}`
[ ! -z "$tmp_mac" ] && ( cat ${dir}ipAddress|grep -q -w -i ${tmp_mac} ) && continue
local ip_name=`getname ${ip} ${ip_mac}`
local time_up=`cat ${dir}tmp_downlist|grep -w ${ip}|awk '{print $4}'|grep -v "^$"|sort -u|head -n1`
local ip_total=`usage get $ip_mac` && [ ! -z "$ip_total" ] && local ip_total="${markdown_linefeed}${markdown_tab}总计流量: ${markdown_space}${markdown_space}${markdown_space}${markdown_space}${ip_total}"
local ip_total=`usage get $ip_mac` && [ ! -z "$ip_total" ] && local ip_total="${str_linefeed}${str_tab}总计流量: ${str_space}${str_space}${str_space}${str _space}${ip_total}"
local time1=`date +%s`
local time1=$(time_for_humans `expr ${time1} - ${time_up}`)
if [ -z "$title" ]; then
title="${ip_name} 断开连接"
content="${content}${markdown_splitline}${titlel4} ${titlered}设备断开连接${titleend}${markdown_linefeed}${markdown_tab}客户端名:${markdown_space}${markdown_space}${markdown_space}${markdown_space}${markdown_space}${ip_name}${markdown_linefeed}${markdown_tab}客户端IP: ${markdown_space}${markdown_space}${markdown_space}${markdown_space}${ip}${markdown_linefeed}${markdown_tab}客户端MAC: ${markdown_space}${markdown_space}${markdown_space}${markdown_space}${ip_mac}$ip_total${markdown_linefeed}${markdown_tab}在线时间: ${markdown_space}${markdown_space}${markdown_space}${markdown _space}${time1}"
content="${content}${str_splitline}${str_title_start}${font_red} 设备断开连接${font_end}${str_title_end}${str_linefeed}${str_tab}客户端名:${str_space}${str_space}${str_space}${str_space}${str_space}${ip_name}${str_linefeed}${str_tab}客户端IP: ${str_space}${str_space}${str_space}${str_space}${ip}${str_linefeed}${str_tab}客户端MAC: ${str_space}${str_space}${str_space}${str_space}${ip_mac}$ip_total${str_linefeed}${str_tab}在线时间: ${str_space}${str_space}${str_space}${str _space}${time1}"
elif ( echo "$title"|grep -q "断开连接" ); then
title="${ip_name} ${title}"
content="${content}${markdown_splitline}${markdown_tab}客户端名:${markdown_space}${markdown_space}${markdown_space}${markdown_space}${markdown_space}${ip_name}${markdown_linefeed}${markdown_tab}客户端IP: ${markdown_space}${markdown_space}${markdown_space}${markdown_space}${ip}${markdown_linefeed}${markdown_tab}客户端MAC: ${markdown_space}${markdown_space}${markdown_space}${markdown_space}${ip_mac}$ip_total${markdown_linefeed}${markdown_tab}在线时间: ${markdown_space}${markdown_space}${markdown_space}${markdown _space}${time1}"
content="${content}${str_splitline}${str_tab}客户端名:${str_space}${str_space}${str_space}${str_space}${str_space}${ip_name}${str_linefeed}${str_tab}客户端IP: ${str_space}${str_space}${str_space}${str_space}${ip}${str_linefeed}${str_tab}客户端MAC: ${str_space}${str_space}${str_space}${str_space}${ip_mac}$ip_total${str_linefeed}${str_tab}在线时间: ${str_space}${str_space}${str_space}${str _space}${time1}"
else
title="设备状态变化"
content="${content}${markdown_splitline}${titlel4} ${titlered}设备断开连接${titleend}${markdown_linefeed}${markdown_tab}客户端名:${markdown_space}${markdown_space}${markdown_space}${markdown_space}${markdown_space}${ip_name}${markdown_linefeed}${markdown_tab}客户端IP: ${markdown_space}${markdown_space}${markdown_space}${markdown_space}${ip}${markdown_linefeed}${markdown_tab}客户端MAC: ${markdown_space}${markdown_space}${markdown_space}${markdown_space}${ip_mac}$ip_total${markdown_linefeed}${markdown_tab}在线时间: ${markdown_space}${markdown_space}${markdown_space}${markdown _space}${time1}"
content="${content}${str_splitline}${str_title_start}${font_red} 设备断开连接${font_end}${str_title_end}${str_linefeed}${str_tab}客户端名:${str_space}${str_space}${str_space}${str_space}${str_space}${ip_name}${str_linefeed}${str_tab}客户端IP: ${str_space}${str_space}${str_space}${str_space}${ip}${str_linefeed}${str_tab}客户端MAC: ${str_space}${str_space}${str_space}${str_space}${ip_mac}$ip_total${str_linefeed}${str_tab}在线时间: ${str_space}${str_space}${str_space}${str _space}${time1}"
fi
echo "`date "+%Y-%m-%d %H:%M:%S"` ${disturb_text}设备 ${ip_name} ${ip} 断开连接 " >> ${logfile}
done
@ -792,29 +863,30 @@ function down_send(){
# 当前设备列表
function current_device(){
( echo "$lite_enable"|grep -q "content" ) || ( echo "$lite_enable"|grep -q "device" ) && return
[ -f ${dir}ipAddress ] && local logrow=$(grep -c "" ${dir}ipAddress) || local logrow="0";[ $logrow -eq "0" ] && return
[ -f ${dir}usage.db ] && local ip_total_db="总计流量"
content="${content}${markdown_splitline}${titlel4} ${titlebold}${titleblue}现有在线设备 ${logrow} 台,具体如下${titleend}${titleboldend}${markdown_linefeed}${markdown_tab}IP 地址${markdown_tab}${tabletab}${ip_total_db}${tableta b}${boldstar}客户端名${boldstar}"
[ -f ${dir}usage.db ] && local ip_total_db="总计流量${str_space}${str_space}${str_space}${str_space} "
content="${content}${str_splitline}${str_title_start}${font_blue} 现有在线设备 ${logrow} 台,具体如下${font_end}${str_title_end}${str_linefeed}${str_tab}IP 地址${str_space}${str_space}${str_space}${str_space}${str_space}${str_space}${str_space}${str_space}${str_space}${ip_total_d b}${boldstar}客户端名${boldstar}"
local IPLIST=`cat ${dir}ipAddress|awk '{print $1}'`
for ip in $IPLIST; do
local ip_mac=`getmac ${ip}`
local ip_total=`usage get ${ip_mac}`
local ip_name=`getname ${ip} ${ip_mac}`
local ip_name=`cut_str $ip_name 15`
if [ "${#ip}" -lt "15" ]; then
if [ "${#ip}" -lt "15" ]; then
local n=`expr 15 - ${#ip}`
for i in `seq 1 $n`; do
local ip="${ip}"
local ip="${ip}${str_space} "
done
unset i n
fi
if [ ! -z "$ip_total" ]; then
if [ ! -z "$ip_total" ]; then
local n=`expr 11 - ${#ip_total}`
for i in `seq 1 $n`; do
local ip_total="${ip_total}"
local ip_total="${ip_total}${str_space} "
done
fi
content="${content}${markdown_linefeed}${markdown_tab}${ip}${tabletab}${ip_total}${tabletab}${boldstar}${greenfont}${ip_name}${fontend }${boldstar}"
content="${content}${str_linefeed}${str_tab}${ip}${ip_total}${boldstar}${font_green2}${ip_name}${font_end2 }${boldstar}"
unset i n ip_total ip_mac ip_name
done
}
@ -825,18 +897,18 @@ function cpu_load(){
[ -z "$temperature_time" ] && temperature_time=`date +%s`
local cpu_wendu=`soc_temp`;
[ -z "$cpu_wendu" ] && echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!】无法读取设备温度,请检查命令" >> ${logfile}
if [ "$cpu_wendu" -gt "$temperature" ]; then
echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!警报!!】 CPU 温度过高: ${cpu_wendu}" >> ${logfile}
else
temperature_time=`date +%s`
fi
if [ "$((`date +%s`-$temperature_time))" -ge "300" ] && [ -z "$temperaturecd_time" ]; then
title="CPU 温度过高!"
temperaturecd_time=`date +%s`
echo "`date "+%Y-%m-%d %H:%M:%S"` ${disturb_text} CPU 温 度过高: ${cpu_wendu}" >> ${logfile}
content="${content}${markdown_splitline}${titlel4} ${titlered}CPU 温度过高${titleend}${markdown_linefeed}${markdown_tab}CPU 温度已连续五分钟超过预设${markdown_linefeed}${markdown_tab}接下来一小时不再提示${markdown_linefeed}${markdown_tab}当前温度:${cpu_wendu} "
echo "`date "+%Y-%m-%d %H:%M:%S"` ${disturb_text} CPU 温度过高: ${cpu_wendu}" >> ${logfile}
content="${content}${str_splitline}${str_title_start}${font_red} CPU 温度过高${font_end}${str_title_end}${str_linefeed}${str_tab}CPU 温度已连续五分钟超过预设${str_linefeed}${str_tab}接下来一小时不再提示${str_linefeed}${str_tab}当前温度:${cpu_wendu}℃ "
elif [ ! -z "$temperaturecd_time" ] && [ "$((`date +%s`-$temperaturecd_time))" -ge "3300" ] ;then
unset temperaturecd_time
fi
@ -846,14 +918,14 @@ function cpu_load(){
[ -z "$cpuload_time" ] && cpuload_time=`date +%s`
local cpu_fuzai=`cat /proc/loadavg|awk '{print $1}'` 2>/dev/null
[ -z "$cpu_fuzai" ] && echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!】无法读取设备负载,请检查命令" >> ${logfile}
if [ `expr $cpu_fuzai \> $cpuload` -eq "1" ]; then
echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!警报!!】 CPU 负载过高: ${cpu_fuzai}" >> ${logfile}
cputop log
else
cpuload_time=`date +%s`
fi
if [ "$((`date +%s`-$cpuload_time))" -ge "300" ] && [ -z "$cpucd_time" ]; then
unset getlogtop
if [ ! -z "$title" ] && ( echo "$title"|grep -q "过高" ); then
@ -863,7 +935,7 @@ function cpu_load(){
fi
cpucd_time=`date +%s`
echo "`date "+%Y-%m-%d %H:%M:%S"` ${disturb_text} CPU 负 载过高: ${cpu_fuzai}" >> ${logfile}
content="${content}${markdown_splitline}${titlel4} ${titlered}CPU 负载过高${titleend}${markdown_linefeed}${markdown_tab}CPU 负载已连续五分钟超过预设${markdown_linefeed}${markdown_tab}接下来一小时不再提示${markdown_linefeed}${markdown _tab}当前负载:${cpu_fuzai}"
content="${content}${str_splitline}${font_red}CPU 负载过高${font_end}${str_linefeed}${str_tab}CPU 负载已连续五分钟超过预设${str_linefeed}${str_tab}接下来一小时不再提示${str_linefeed}${str _tab}当前负载:${cpu_fuzai}"
cputop
elif [ ! -z "$cpucd_time" ] && [ "$((`date +%s`-$cpucd_time))" -ge "3300" ] ;then
unset cpucd_time
@ -872,65 +944,214 @@ function cpu_load(){
}
function cputop(){
[ -z "$1" ] && content="${content}${markdown_splitline}${titlel4} 当前 CPU 占用前三的进程"
local i=1 && local top_i=5 && `top -bn 1 > ${dir}top` >/dev/null 2>&1
while [ $i -le 3 ]; do
if ( ! cat ${dir}top|awk 'NR=='${top_i}|grep -q "top -bn 1" ); then
local top_name=`cat ${dir}top|awk 'NR=='${top_i}|awk '{print $8}'`;[ "$top_name" == "/bin/sh" ] || [ "$top_name" == "/bin/bash" ] && local top_name=`cat ${dir}top|awk 'NR=='${top_i}|awk '{print $9}'`
local top_load=`cat ${dir}top|awk 'NR=='${top_i}|awk '{print $7}'`
local temp_top="${top_name} ${top_load}"
[ ! -z "$1" ] && local logtop="$logtop $temp_top"
[ -z "$1" ] && content="${content}${markdown_linefeed}${markdown_tab}${temp_top}"
local i=`expr ${i} + 1`
fi
local top_i=`expr ${top_i} + 1`
[ -z "$1" ] && content="${content}${str_splitline}${str_title_start} 当前 CPU 占用前三的进程${str_title_end}"
local gettop=`top -bn 1|grep -v "top -bn 1"`
for i in `seq 5 7`; do
local top_name=`echo "${gettop}"|awk 'NR=='${i}|awk '{print ($8 ~ /\/bin\/sh|\/bin\/bash/) ? $9 : $8}'`
local top_load=`echo "${gettop}"|awk 'NR=='${i}|awk '{print $7}'`
local temp_top="${top_name} ${top_load}"
[ ! -z "$1" ] && local logtop="$logtop $temp_top"
[ -z "$1" ] && content="${content}${str_linefeed}${str_tab}${temp_top}"
done
unset i
[ ! -z "$1" ] && echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!警报!!】 CPU 占用前三: ${logtop}" >> ${logfile}
rm -f ${dir}top >/dev/null 2>&1
}
# 生成日志监控文件,避免后台影响 wait 语句
function get_syslog(){
kill -9 `pgrep -f "logread -f -p notice"` 2>/dev/null
[ -z "$web_logged" ] && [ -z "$ssh_logged" ] && [ -z "$web_login_failed" ] && [ -z "$ssh_login_failed" ] && return
rm -f ${dir}login_monitor >/dev/null 2>&1
cat>${dir}get_syslog<<EOF
#!/bin/sh
logread -f -p notice >> ${dir}login_monitor &
EOF
chmod 0755 ${dir}get_syslog && ${dir}get_syslog
rm -f ${dir}get_syslog >/dev/null 2>&1
}
# 登录提醒通知
function login_send(){
[ -z "$web_logged" ] && [ -z "$ssh_logged" ] && [ -z "$web_login_failed" ] && [ -z "$ssh_login_failed" ] && return
[ ! -f ${dir}login_monitor ] && return
cat ${dir}login_monitor|grep -i "accepted login"|awk '{print $4" "$NF}' >> ${dir}web_login
cat ${dir}login_monitor|grep -i "Password auth succeeded\|Pubkey auth succeeded"|grep -Eo "[0-9]{2}:[0-9]{2}:[0-9]{2}.*[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"|awk '{print $1" "$NF" "$5}' >> ${dir}ssh_login
cat ${dir}login_monitor|grep -i "failed login"|grep -Eo "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}" >> ${dir}web_failed
cat ${dir}login_monitor|grep -i "Bad password attempt\|Login attempt for nonexistent user from"|grep -Eo "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}" >> ${dir}ssh_failed
echo "" > ${dir}login_monitor
add_ip_black
local login_ip_list=`cat ${dir}web_login|awk '{print $2}'|grep -v "^$"|sort -u|head -n1`
for login_ip in $login_ip_list; do
[ -z "$login_ip" ] && continue
echo "$ip_white_list"|grep -w -q "$login_ip" && continue
local web_login_time=`cat ${dir}web_login|grep -w ${login_ip}|awk '{print $1}'|grep -v "^$"|sort -u|head -n1`
local web_login_mode=`cat ${dir}web_login|grep -w ${login_ip}|awk '{print $3}'|grep -v "^$"|sort -u|head -n1`
if [ ! -z "$web_logged" ] && [ "$web_logged" -eq "1" ]; then
if [ -z "$title" ]; then
title="${login_ip} 通过 Web 登录了路由器"
content="${content}${str_splitline}${str_title_start}${font_green} 登录信息${font_end}${str_title_end}${str_linefeed}${str_tab}时间:${str_space}${str_space}${str_space}${str_space}${str_space}${web_login_time}${str_linefeed}${str_tab}设备 IP: ${str_space}${str_space}${str_space}${str_space}${login_ip}${content_mode}"
elif ( echo "$title"|grep -q "登录了路由器" ); then
title="${login_ip} ${title}"
content="${content}${str_splitline}${str_tab}时间:${str_space}${str_space}${str_space}${str_space}${str_space}${web_login_time}${str_linefeed}${str_tab}设备 IP: ${str_space}${str_space}${str_space}${str_space}${login_ip}${content_mode}"
else
title="设备状态变化"
content="${content}${str_splitline}${str_title_start}${font_green} 登录信息${font_end}${str_title_end}${str_linefeed}${str_tab}时间:${str_space}${str_space}${str_space}${str_space}${str_space}${web_login_time}${str_linefeed}${str_tab}设备 IP: ${str_space}${str_space}${str_space}${str_space}${login_ip}${content_mode}"
fi
fi
echo "`date "+%Y-%m-%d %H:%M:%S"` ${disturb_text}设备 ${login_ip} 通过 web ${web_login_mode} 登录了路由器 " >> ${logfile}
done
echo "" > ${dir}web_login
unset login_ip login_ip_list
local login_ip_list=`cat ${dir}ssh_login|awk '{print $2}'|grep -v "^$"|sort -u|head -n1`
for login_ip in $login_ip_list; do
[ -z "$login_ip" ] && continue
echo "$ip_white_list"|grep -w -q "$login_ip" && continue
local ssh_login_time=`cat ${dir}ssh_login|grep -w ${login_ip}|awk '{print $1}'|grep -v "^$"|sort -u|head -n1`
local ssh_login_mode=`cat ${dir}ssh_login|grep -w ${login_ip}|awk '{print $3}'|grep -v "^$"|sort -u|head -n1`
[ ! -z "$ssh_login_mode" ] && local content_mode="${str_linefeed}${str_tab}登录方式: ${str_space}${str_space}${str_space}${str_space}${ssh_login_mode}"
if [ ! -z "$ssh_logged" ] && [ "$ssh_logged" -eq "1" ]; then
if [ -z "$title" ]; then
title="${login_ip} 通过 SSH 登录了路由器"
content="${content}${str_splitline}${str_title_start}${font_green} 登录信息${font_end}${str_title_end}${str_linefeed}${str_tab}时间:${str_space}${str_space}${str_space}${str_space}${str_space}${ssh_login_time}${str_linefeed}${str_tab}设备 IP: ${str_space}${str_space}${str_space}${str_space}${login_ip}${content_mode}"
elif ( echo "$title"|grep -q "登录了路由器" ); then
title="${login_ip} ${title}"
content="${content}${str_splitline}${str_tab}时间:${str_space}${str_space}${str_space}${str_space}${str_space}${ssh_login_time}${str_linefeed}${str_tab}设备 IP: ${str_space}${str_space}${str_space}${str_space}${login_ip}${content_mode}"
else
title="设备状态变化"
content="${content}${str_splitline}${str_title_start}${font_green} 登录信息${str_title_end}${str_linefeed}${str_tab}时间:${str_space}${str_space}${str_space}${str_space}${str_space}${ssh_login_time}${str_linefeed}${str_tab}设备 IP: ${str_space}${str_space}${str_space}${str_space}${login_ip}${content_mode}"
fi
fi
echo "`date "+%Y-%m-%d %H:%M:%S"` 【info】设备 ${login_ip} 通过 SSH ${ssh_login_mode} 登录了路由器 " >> ${logfile}
done
echo "" > ${dir}ssh_login
unset login_ip login_ip_list
local login_ip_list=`cat ${dir}web_failed|awk '{print $1}'|grep -v "^$"|sort -u|head -n1`
for login_ip in $login_ip_list; do
[ -z "$login_ip" ] && continue
echo "$ip_white_list"|grep -w -q "$login_ip" && continue
local web_login_sum=`cat ${dir}web_failed|grep -w "${login_ip}"|wc -l`
if [ "$web_login_sum" -ge "$login_max_num" ] ;then
if [ ! -z "$web_login_failed" ] && [ "$web_login_failed" -eq "1" ]; then
if [ -z "$title" ]; then
title="${login_ip} 通过 Web 频繁尝试登录"
content="${content}${str_splitline}${str_title_start}${font_red} 登录信息${font_end}${str_title_end}${str_linefeed}${str_tab}设备 IP: ${str_space}${str_space}${str_space}${str_space}${login_ip}"
elif ( echo "$title"|grep -q "频繁尝试登录" ); then
title="${login_ip} ${title}"
content="${content}${str_splitline}${str_tab}设备 IP: ${str_space}${str_space}${str_space}${str_space}${login_ip}"
else
title="设备状态变化"
content="${content}${str_splitline}${str_title_start}${font_red} 登录信息${font_end}${str_title_end}${str_linefeed}${str_tab}设备 IP: ${str_space}${str_space}${str_space}${str_space}${login_ip}"
fi
fi
sed -i "/^${login_ip}$/d" ${dir}web_failed
echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!】设备 ${login_ip} 通过 Web 频繁尝试登录" >> ${logfile}
add_ip_black $login_ip
fi
done
unset login_ip
local login_ip_list=`cat ${dir}ssh_failed|awk '{print $1}'|grep -v "^$"|sort -u|head -n1`
for login_ip in $login_ip_list; do
[ -z "$login_ip" ] && continue
echo "$ip_white_list"|grep -w -q "$login_ip" && continue
local ssh_login_sum=`cat ${dir}ssh_failed|grep -w "${login_ip}"|wc -l`
if [ "$ssh_login_sum" -ge "$login_max_num" ] ;then
if [ ! -z "$ssh_login_failed" ] && [ "$ssh_login_failed" -eq "1" ]; then
if [ -z "$title" ]; then
title="${login_ip} 通过 SSH 频繁尝试登录"
content="${content}${str_splitline}${str_title_start}${font_red} 登录信息${font_end}${str_title_end}${str_linefeed}${str_tab}设备 IP: ${str_space}${str_space}${str_space}${str_space}${login_ip}"
elif ( echo "$title"|grep -q "频繁尝试登录" ); then
title="${login_ip} ${title}"
content="${content}${str_splitline}${str_tab}设备 IP: ${str_space}${str_space}${str_space}${str_space}${login_ip}"
else
title="设备状态变化"
content="${content}${str_splitline}${str_title_start}${font_red} 登录信息${font_end}${str_title_end}${str_linefeed}${str_tab}设备 IP: ${str_space}${str_space}${str_space}${str_space}${login_ip}"
fi
fi
sed -i "/^${login_ip}$/d" ${dir}ssh_failed
echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!】设备 ${login_ip} 通过 SSH 频繁尝试登录" >> ${logfile}
add_ip_black $login_ip
fi
done
unset login_ip
}
# 添加黑名单
function add_ip_black(){
[ -f "${ip_blacklist_path}" ] && local logrow=$(grep -c "" ${ip_blacklist_path}) || local logrow="0"
[ ! -f "${ip_blacklist_path}" ] && local logrow="0"
[ ! -z "$web_login_black" ] && [ "$web_login_black" -eq "0" ] || [ -z "$web_login_black" ] && local logrow="0"
ipset flush ip_blacklist >/dev/null 2>&1
if [ $logrow -le "0" ]; then
iptables -D INPUT -m set --match-set ip_blacklist src -j DROP >/dev/null 2>&1
ipset destroy ip_blacklist >/dev/null 2>&1
return
fi
ipset list ip_blacklist >/dev/null 2>&1 || ipset create ip_blacklist hash:ip timeout ${ip_black_timeout} >/dev/null 2>&1
iptables -C INPUT -m set --match-set ip_blacklist src -j DROP >/dev/null 2>&1 || iptables -I INPUT -m set --match-set ip_blacklist src -j DROP >/dev/null 2>&1
echo "$1" >> ${ip_blacklist_path}
for ip_black in `cat ${ip_blacklist_path}`; do
ip_black=`echo "$ip_black"|grep -Eo "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"`
ipset -! add ip_blacklist $ip_black >/dev/null 2>&1
done
ipset list ip_blacklist|grep -Eo "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}" > ${ip_blacklist_path}
}
# 发送定时数据
function send(){
echo "`date "+%Y-%m-%d %H:%M:%S"` 【定时数据】创建定时任务" >> ${logfile}
pushbot_disturb;local send_disturb=$?
get_config "send_title" "router_status" "client_list" "router_temp" "router_wan"
get_config "send_title" "router_status" "router_temp" "router_wan" "client_list"
[ -z "$send_title" ] && local send_title="路由状态:"
[ ! -z "$client_list" ] && [ "$client_list" -eq "1" ] && > ${dir}send_enable.lock && pushbot_first &
[ ! -z "$1" ] && local send_title="发送测试:" && local send_content="${str_splitline}${str_title_start}内容1${str_title_end}${str_linefeed}${str_tab}设备1${str_linefeed}${str_tab}设备2${str_splitline}${str_title_start}内容2${str_title_end}${str_linefeed}${str_tab}设备3${str_linefeed}${str_tab}设备4"
[ -z "$1" ] && [ ! -z "$client_list" ] && [ "$client_list" -eq "1" ] && > ${dir}send_enable.lock && pushbot_first &
if [ ! -z "$router_status" ] && [ "$router_status" -eq "1" ]; then
if [ -z "$1" ] && [ ! -z "$router_status" ] && [ "$router_status" -eq "1" ]; then
local systemload=`cat /proc/loadavg|awk '{print $1" "$2" "$3}'`
local cpuload=`getcpu`
local ramload=`free -m|sed -n '2p'|awk '{printf "%.2f%%\n",($3/$2)*100}'`
local systemstatustime=`cat /proc/uptime|awk -F. '{run_days=$1 / 86400;run_hour=($1 % 86400)/3600;run_minute=($1 % 3600)/60;run_second=$1 % 60;printf("运行时间:%d天%d时%d分%d秒",run_days,run_hour,run_minute,run_second)}'`;unset run_days run_hour run_minute run_second
local send_content="${send_content}${markdown_splitline}${titlel4} ${titlebold}${titleblue}系统运行状态${titleend}${titleboldend}"
local send_content="${send_content}${markdown_linefeed}${markdown_tab}平均负载:${systemload}"
local send_content="${send_content}${markdown_linefeed}${markdown_tab}CPU占用: ${cpuload}${percentsym}"
local send_content="${send_content}${markdown_linefeed}${markdown_tab}内存占用:${ramload}${percentsym}"
local send_content="${send_content}${markdown_linefeed}${markdown_tab}${systemstatustime}"
local send_content="${send_content}${str_splitline}${str_title_start}${font_blue} 系统运行状态${font_end}${str_title_ end}"
local send_content="${send_content}${str_linefeed}${str _tab}平均负载:${systemload}"
local send_content="${send_content}${str_linefeed}${str _tab}CPU占用: ${cpuload}${percentsym}"
local send_content="${send_content}${str_linefeed}${str _tab}内存占用:${ramload}${percentsym}"
local send_content="${send_content}${str_linefeed}${str _tab}${systemstatustime}"
fi
[ ! -z "$router_temp" ] && [ "$router_temp" -eq "1" ] && local cputemp=`soc_temp` && local send_content="${send_content}${markdown_splitline}${titlel4} ${titlebold}${titleblue}设备温度${titleend}${titleboldend}${markdown_linefeed}${markdown_tab}CPU: ${cputemp}℃" #%e2%84%83="℃"
if [ -z "$1" ] && [ ! -z "$router_temp" ] && [ "$router_temp" -eq "1" ]; then
local cputemp=`soc_temp`
[ ! -z "$cputemp" ] && local send_content="${send_content}${str_splitline}${str_title_start}${font_blue} 设备温度${font_end}${str_title_end}${str_linefeed}${str_tab}CPU: ${cputemp}℃"
[ -z "$cputemp" ] && local send_content="${send_content}${str_splitline}${str_title_start}${font_red} 设备温度${font_end}${str_title_end}${str_linefeed}${str_tab}无法获取设备温度"
fi
if [ ! -z "$router_wan" ] && [ "$router_wan" -eq "1" ]; then
if [ -z "$1" ] && [ ! -z "$router_wan" ] && [ "$router_wan" -eq "1" ]; then
local send_wanIP=`getip wanipv4`;local send_hostIP=`getip hostipv4`
local send_content="${send_content}${markdown_splitline}${titlel4} ${titlebold}${titleblue}WAN 口信息${titleend}${titleboldend}${markdown_linefeed}${markdown_tab}接口ip:${send_wanIP}"
local send_content="${send_content}${markdown_linefeed}${markdown_tab}外网ip:${send_hostIP}"
local send_content="${send_content}${str_splitline}${str_title_start}${font_blue} WAN 口信息${font_end}${str_title_end}${str_linefeed}${str _tab}接口ip:${send_wanIP}"
local send_content="${send_content}${str_linefeed}${str _tab}外网ip:${send_hostIP}"
if [ ! -z "$pushbot_ipv6" ] && [ "$pushbot_ipv6" -ne "0" ]; then
local send_wanIPv6=`getip wanipv6`;local send_hostIPv6=`getip hostipv6`
local send_content="${send_content}${markdown_linefeed}${markdown_tab}ipv6 :${send_wanIPv6}"
local send_content="${send_content}${markdown_linefeed}${markdown_tab}外网v6:${send_hostIPv6}"
local send_content="${send_content}${str_linefeed}${str _tab}ipv6 :${send_wanIPv6}"
local send_content="${send_content}${str_linefeed}${str _tab}外网v6:${send_hostIPv6}"
fi
( ! echo "$send_wanIP"|grep -q -w ${send_hostIP} ) && local send_content="${send_content}${markdown_linefeed}${markdown_tab}外网 ip 与接口 ip 不一致,你的 ip 不是公网 ip"
( ! echo "$send_wanIP"|grep -q -w ${send_hostIP} ) && local send_content="${send_content}${str_linefeed}${str _tab}外网 ip 与接口 ip 不一致,你的 ip 不是公网 ip"
local interfaceuptime=`getinterfaceuptime`
[ ! -z "$interfaceuptime" ] && local wanstatustime=`getinterfaceuptime|awk -F. '{run_days=$1 / 86400;run_hour=($1 % 86400)/3600;run_minute=($1 % 3600)/60;run_second=$1 % 60;printf("在线时间:%d天%d时%d分%d秒",run_days,run_hour,run_minute,run_second)}'` && unset run_days run_hour run_minute run_second
local send_content="${send_content}${markdown_linefeed}${markdown_tab}${wanstatustime}"
local send_content="${send_content}${str_linefeed}${str _tab}${wanstatustime}"
fi
if [ ! -z "$client_list" ] && [ "$client_list" -eq "1" ]; then
if [ -z "$1" ] && [ ! -z "$client_list" ] && [ "$client_list" -eq "1" ]; then
wait
local IPLIST=`cat ${dir}ipAddress 2>/dev/null|awk '{print $1}'`
[ -z "$IPLIST" ] && local send_content="${send_content}${markdown_splitline}${titlel4} ${titlebold}${titlered}当前无在线设备${titleend}${titleboldend}" || local send_content="${send_content}${markdown_splitline}${titlel4} ${titlebold}${titleblue}在线设备${titleend}${titleboldend}"
[ -f ${dir}ipAddress ] && local logrow=$(grep -c "" ${dir}ipAddress) || local logrow="0"
[ "$logrow" -eq "0" ] && local send_content="${send_content}${str_splitline}${str_title_start}${font_red} 当前无在线设备${font_end}${str_title_end}" || local send_content="${send_content}${str_splitline}${str_title_start}${font_blue} 现有在线设备 ${logrow} 台${font_end}${str_title_end}"
for ip in $IPLIST; do
local time_up=`cat ${dir}ipAddress|grep -w ${ip}|awk '{print $4}'|grep -v "^$"|sort -u|head -n1`
local time1=`date +%s`
@ -939,24 +1160,16 @@ function send(){
local ip_name=`getname ${ip} ${ip_mac}`
local ip_total=`usage get ${ip_mac}`;[ ! -z "$ip_total" ] && local ip_total="总计流量:${ip_total} "
local ip_name=`cut_str $ip_name 18`
local send_content="${send_content}${markdown_linefeed}${markdown_tab}${greenfont}【${ip_name}】${fontend} ${ip}${markdown_linefeed}${markdown _tab}${ip_total}在线 ${time1}"
local send_content="${send_content}${str_linefeed}${str_tab}${font_green2}【${ip_name}】${font_end2} ${ip}${str_linefeed}${str _tab}${ip_total}在线 ${time1}"
unset ip_total time_down time_up time1 ip_mac ip_name
done
fi
[ ! -z "$device_name" ] && local send_title="【$device_name】${send_title}"
local send_title=`echo "$send_title"|sed $'s/\ / /g'|sed $'s/\"/%22/g'|sed $'s/\#/%23/g'|sed $'s/\&/%26/g'|sed $'s/\,/%2C/g'|sed $'s/\//%2F/g'|sed $'s/\:/%3A/g'|sed $'s/\;/%3B/g'|sed $'s/\=/%3D/g'|sed $'s/\@/%40/g'`
[ -z "$send_content" ] && local send_content="${markdown_splitline}${titlel4} ${titlered}我遇到了一个难题${titleend}${markdown_linefeed}${markdown_tab}定时发送选项错误,你没有选择需要发送的项目,该怎么办呢${markdown_splitline}"
local nowtime=`date "+%Y-%m-%d %H:%M:%S"`
pushbot_send="curl -s \"https://oapi.dingtalk.com/robot/send?access_token=${dd_webhook}\" -H 'Content-Type: application/json' -d '{\"msgtype\": \"markdown\",\"markdown\": {\"title\":\"${send_title}\",\"text\":\"${titlel4} <font color=#6A65FF>${send_title}</font>${markdown_linefeed}${nowtime}${markdown_linefeed}${send_content}\"}}'"
wechatent_send="curl -s \"https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=${we_webhook}\" -H 'Content-Type: application/json' -d '{\"msgtype\": \"markdown\",\"markdown\": {\"title\":\"${send_title}\",\"content\":\"${titlel4} <font color=#6A65FF>${send_title}</font>${markdown_linefeed}${nowtime}${markdown_linefeed}${send_content}\"}}'"
feishu_send="curl -X POST https://open.feishu.cn/open-apis/bot/v2/hook/${fs_webhook} -H 'Content-Type: application/json' -d '{\"msg_type\": \"post\",\"content\": {\"post\": {\"zh_cn\": {\"title\": \"${send_title}\",\"content\": [[{\"tag\": \"text\",\"text\": \"${nowtime}${markdown_linefeed}${send_content}\"}]]}}}}'"
[ "$send_disturb" -eq "0" ] && [ -z "$send_we" ] && eval $pushbot_send >/dev/null 2>&1
[ "$send_disturb" -eq "0" ] && [ ! -z "$send_we" ] && [ "$send_we" -eq "1" ] && eval $wechatent_send >/dev/null 2>&1
[ "$send_disturb" -eq "0" ] && [ ! -z "$send_we" ] && [ "$send_we" -eq "2" ] && curl -s "http://pushplus.plus/send?token=${pp_token}" -d "&channel=${pp_channel}&webhook=${pp_webhook}&topic=${pp_topic}&title=${send_title}&content=${nowtime}${markdown_linefeed}${send_content}&template=markdown" >/dev/null 2>&1
[ "$send_disturb" -eq "0" ] && [ ! -z "$send_we" ] && [ "$send_we" -eq "3" ] && eval $feishu_send >/dev/null 2>&1
[ "$send_disturb" -eq "0" ] && [ ! -z "$send_we" ] && [ "$send_we" -eq "4" ] && curl -s "${bark_srv}/${bark_token}/${send_title}/${nowtime}${markdown_linefeed}${send_content}?isArchive=1" >/dev/null 2>&1
[ -z "$send_content" ] && local send_content="${str_splitline}${str_title_start} 我遇到了一个难题${str_title_end}${str_linefeed}${str_tab}定时发送选项错误,你没有选择需要发送的项目,该怎么办呢${str_splitline}"
[ "$send_disturb" -eq "0" ] && diy_send "${send_title}" "${send_content}" "${jsonpath}" >/dev/null 2>&1
[ $? -eq 1 ] && echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!】定时推送失败,请检查网络或设置信息" >> ${logfile} || echo "`date "+%Y-%m-%d %H:%M:%S"` ${disturb_text}定时推送任务完成" >> ${logfile}
deltemp
echo "`date "+%Y-%m-%d %H:%M:%S"` ${disturb_text}定时推送任务完成" >> ${logfile}
}
# 初始化
@ -979,6 +1192,7 @@ if [ "$1" ] ;then
[ $1 == "send" ] && send
[ $1 == "soc" ] && echo `soc_temp` > ${dir}soc_tmp
[ $1 == "client" ] && get_client
[ $1 == "test" ] && send test
exit
fi
@ -995,11 +1209,19 @@ while [ "$pushbot_enable" -eq "1" ]; do
pushbot_disturb;disturb=$?
# 外网IP变化检测
if [ ! -z "$pushbot_ipv4" ] && [ ! -z "$pushbot_ipv6" ] && [ "$pushbot_ipv4" -ne "0" ] || [ "$pushbot_ipv6" -ne "0" ]; then
[ -f ${dir}ipAddress ] && ipAddress_logrow=$(grep -c "" ${dir}ipAddress) || ipAddress_logrow="0";
if [ $ipAddress_logrow -ne "0" ]; then
online_list=`cat ${dir}ipAddress|awk '{print $2}'|grep -v "^$"|sort -u`
for online_mac in $online_list; do
[ ! -z "$online_mac" ] && mac_online_status="`echo "$mark_mac_list"|grep -i $online_mac|grep -v "^$"|sort -u|head -n1`${mac_online_status}"
done
fi
if [ "$pushbot_ipv4" -ne "0" ] || [ "$pushbot_ipv6" -ne "0" ]; then
rand_geturl
ip_changes
fi
# 设备列表
if [ ! -f "${dir}send_enable.lock" ]; then
[ ! -z "$title" ] && echo "$title" > ${dir}title
@ -1008,36 +1230,32 @@ while [ "$pushbot_enable" -eq "1" ]; do
[ -f "${dir}title" ] && title=`cat ${dir}title` && rm -f ${dir}title >/dev/null 2>&1
[ -f "${dir}content" ] && content=`cat ${dir}content` && rm -f ${dir}content >/dev/null 2>&1
fi
# 离线缓存区推送
[ ! -f "${dir}send_enable.lock" ] && down_send
# 当前设备列表
[ ! -z "$content" ] && [ ! -f "${dir}send_enable.lock" ] && [ -z "$content_current_device" ] || [ "$content_current_device" -eq "0" ] && current_device
[ ! -z "$content" ] && [ ! -f "${dir}send_enable.lock" ] && current_device
# 无人值守任务
[ ! -f "${dir}send_enable.lock" ] && unattended
# CPU 检测
[ ! -f "${dir}send_enable.lock" ] && cpu_load
# 异常流量检测
[ ! -f "${dir}send_enable.lock" ] && get_client_usage
# 登录提醒通知
[ ! -f "${dir}send_enable.lock" ] && login_send
# 通知
if [ ! -f "${dir}send_enable.lock" ] && [ ! -z "$title" ] && [ ! -z "$content" ]; then
[ -z "$content_nowtime" ] || [ "$content_nowtime" -eq "0" ] && nowtime=`date "+%Y-%m-%d %H:%M:%S"`
[ ! -z "$device_name" ] && title="【$device_name】$title"
title=`echo "$title"|sed $'s/\ / /g'|sed $'s/\"/%22/g'|sed $'s/\#/%23/g'|sed $'s/\&/%26/g'|sed $'s/\,/%2C/g'|sed $'s/\//%2F/g'|sed $'s/\:/%3A/g'|sed $'s/\;/%3B/g'|sed $'s/\=/%3D/g'|sed $'s/\@/%40/g'`
[ ! -z "$content_content" ] && [ "$content_content" -eq "1" ] && content=""
pushbot_send="curl -s \"https://oapi.dingtalk.com/robot/send?access_token=${dd_webhook}\" -H 'Content-Type: application/json' -d '{\"msgtype\": \"markdown\",\"markdown\": {\"title\":\"${title}\",\"text\":\"${titlel4} ${titlebold}<font color=#6A65FF>${title}</font>${titleboldend}${markdown_linefeed}${nowtime}${markdown_linefeed}${content}${markdown_linefeed}${titlebold}<font color=#6A65FF>${title}</font>${titleboldend}\"}}'"
wechatent_send="curl -s \"https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=${we_webhook}\" -H 'Content-Type: application/json' -d '{\"msgtype\": \"markdown\",\"markdown\": {\"title\":\"${title}\",\"content\":\"${titlel4} ${titlebold}<font color=#6A65FF>${title}</font>${titleboldend}${markdown_linefeed}${nowtime}${markdown_linefeed}${content}${markdown_linefeed}${titlebold}<font color=#6A65FF>${title}</font>${titleboldend}\"}}'"
feishu_send="curl -X POST https://open.feishu.cn/open-apis/bot/v2/hook/${fs_webhook} -H 'Content-Type: application/json' -d '{\"msg_type\": \"post\",\"content\": {\"post\": {\"zh_cn\": {\"title\": \"${title}\",\"content\": [[{\"tag\": \"text\",\"text\": \"${nowtime}${markdown_linefeed}${content}\"}]]}}}}'"
[ "$disturb" -eq "0" ] && [ -z "$send_we" ] && eval $pushbot_send >/dev/null 2>&1
[ "$disturb" -eq "0" ] && [ ! -z "$send_we" ] && [ "$send_we" -eq "1" ] && eval $wechatent_send >/dev/null 2>&1
[ "$disturb" -eq "0" ] && [ ! -z "$send_we" ] && [ "$send_we" -eq "2" ] && curl -s "http://pushplus.plus/send?token=${pp_token}" -d "&channel=${pp_channel}&webhook=${pp_webhook}&topic=${pp_topic}&title=${title}&content=${nowtime}${markdown_linefeed}${content}&template=markdown" >/dev/null 2>&1
[ "$disturb" -eq "0" ] && [ ! -z "$send_we" ] && [ "$send_we" -eq "3" ] && eval $feishu_send 2>&1
[ "$disturb" -eq "0" ] && [ ! -z "$send_we" ] && [ "$send_we" -eq "4" ] && curl -s "${bark_srv}/${bark_token}/${title}/${nowtime}${markdown_linefeed}${content}?isArchive=1" >/dev/null 2>&1
fi
( echo "$lite_enable"|grep -q "content" ) && content="$title"
[ "$disturb" -eq "0" ] && diy_send "${title}" "${content}" "${jsonpath}" >/dev/null 2>&1
[ $? -eq 1 ] && echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!】推送失败,请检查网络或设置信息 " >> ${logfile}
fi
while [ -f "${dir}send_enable.lock" ]; do
sleep $sleeptime