前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >OpenWRT简易版serverchan脚本推送至企业微信

OpenWRT简易版serverchan脚本推送至企业微信

作者头像
hiplon
发布2023-10-18 08:25:43
4100
发布2023-10-18 08:25:43
举报
文章被收录于专栏:VNF

OpenWRT简易版serverchan脚本推送至企业微信

By HKL, on Monday 2021-05-31 15:20, tagged: 🏷️Linux 🏷️Operating 🏷️Networking

本文主要介绍简易版serverchan脚本推送至企业微信。

脚本代码如下

代码语言:javascript
复制
#!/bin/sh
export PATH='/usr/sbin:/usr/bin:/sbin:/bin'

# Paraments
resub=1
push1="1"				# Push online devices
push_ddns="1"			# Push ddns message
qywxpusher_enable="1"	# Enable ServerChan
#corp_id='wwcxxxxxxxxx'    #你的企业微信企业ID
#app_secret='xxxxxxxxx'    #你的企业微信应用secret
#app_id='100000X'    #你的企业微信应用ID



touch /tmp/tmp/lastIPAddress
[ ! -s /tmp/tmp/lastIPAddress ] && echo "Initialing!" > /tmp/tmp/lastIPAddress

touch /tmp/tmp/pushDevice
[ ! -s /tmp/tmp/pushDevice ] && echo "$push1" > /tmp/tmp/pushDevice

# Get wan IP
# Check curl exist, if not, use wget
getIpAddress() {
    curltest=`which curl`
    if [ -z "$curltest" ] || [ ! -s "`which curl`" ] ; then
        wget --no-check-certificate --quiet --output-document=- "http://members.3322.org/dyndns/getip"
    else
        curl -k -s "http://members.3322.org/dyndns/getip"
    fi
}
# load last IP
lastIPAddress() {
        inter="/tmp/tmp/lastIPAddress"
        cat $inter
}



# Get online devices
test(){
	alias=`cat /root/devicelist`
        cat /proc/net/arp | grep -v "Mask" | sed 's/(//;s/)//' | while read -r IP HW FLAGS MAC MASK DEVICE
        do
        if [ $DEVICE == "br-lan" ]; then
            #echo $DEVICE
                NAME=`echo "$alias" | awk '/'$MAC'\ '$IP'/{print $4}'`
            if [ ! -n "$NAME" ]; then
                    NAME="Unknown"
            else
                    NAME=${NAME}
            fi
            echo $NAME $IP >> /tmp/tmp/newhostname.txt
            #echo $NAME $IP
        fi
        done
~

}


while [ "$qywxpusher_enable" == "1" ];
do
curltest=`which curl`
if [ -z "$curltest" ] ; then
    wget --continue --no-check-certificate  -q -T 10 http://www.baidu.com
	[ "$?" == "0" ] && check=200 || check=404
else
    check=`curl -k -s -w "%{http_code}" "http://www.baidu.com" -o /dev/null`
fi

if [ "$check" == "200" ] ; then
hostIP=$(getIpAddress)
lastIP=$(lastIPAddress)

if [ "$lastIP" != "$hostIP" ] && [ ! -z "$hostIP" ] ; then
	sleep 60
    # Check again
	hostIP=$(getIpAddress)
    lastIP=$(lastIPAddress)
 fi

if [ "$lastIP" != "$hostIP" ] && [ ! -z "$hostIP" ] ; then
    logger -t "公网IP变动" "目前 IP: ${hostIP}"
    logger -t "公网IP变动" "上次 IP: ${lastIP}"
	if [ "$?" == "0" ] ; then
		if [ "$push_ddns" = "1" ] ; then
            wxpushcontent='New WAN IP address: '${hostIP}
            msg_token=`curl -s -X GET "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$corp_id&corpsecret=$app_secret"`
            echo $msg_token > /tmp/tmp/wx.json
            wx_token=`jsonfilter -i /tmp/tmp/wx.json -e '$.access_token'`
            curl -s -X POST "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$wx_token" \
               -H "Content-Type: application/json" \
              --data "{\"touser\":\"@all\",\"agentid\":\"$app_id\",\"text\": {\"content\":\"$wxpushcontent\"}, \"msgtype\":\"text\",\"safe\":0}" &
			logger -t "wechat push" "IP: ${hostIP} pushed"
		fi
		echo -n $hostIP > /tmp/tmp/lastIPAddress
	fi
fi
if [ `cat /tmp/tmp/pushDevice` = "1" ] ; then
    # 设备上、下线提醒
    # 获取接入设备名称
    touch /tmp/tmp/newhostname.txt
    echo "接入设备名称" > /tmp/tmp/newhostname.txt	
    # 当前所有接入设备
	test
	# 读取已在线设备名称
    touch /tmp/tmp/hostname_online.txt
    [ ! -s /tmp/tmp/hostname_online.txt ] && echo "接入设备名称" > /tmp/tmp/hostname_online.txt
    # 上线
    awk 'NR==FNR{a[$0]++} NR>FNR&&a[$0]' /tmp/tmp/hostname_online.txt /tmp/tmp/newhostname.txt > /tmp/tmp/newhostname_same_online.txt
    awk 'NR==FNR{a[$0]++} NR>FNR&&!a[$0]' /tmp/tmp/newhostname_same_online.txt /tmp/tmp/newhostname.txt > /tmp/tmp/newhostname_uniqe_online.txt
    if [ -s "/tmp/tmp/newhostname_uniqe_online.txt" ] ; then
		content=`cat /tmp/tmp/newhostname_uniqe_online.txt | grep -v "^$"`
        onlineDev=`cat /tmp/tmp/hostname_online.txt | grep -v "^$"`
        wxpushcontent='设备上线通知\n------------------\n'${content}'\n\n------------------\n'${onlineDev}
        msg_token=`curl -s -X GET "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$corp_id&corpsecret=$app_secret"`
            echo $msg_token > /tmp/tmp/wx.json
            wx_token=`jsonfilter -i /tmp/tmp/wx.json -e '$.access_token'`
            curl -s -X POST "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$wx_token" \
               -H "Content-Type: application/json" \
              --data "{\"touser\":\"@all\",\"agentid\":\"$app_id\",\"text\": {\"content\":\"$wxpushcontent\"}, \"msgtype\":\"text\",\"safe\":0}" &

		logger -t "wechat push" "设备上线: ${content} pushed"
		cat /tmp/tmp/newhostname_uniqe_online.txt | grep -v "^$" >> /tmp/tmp/hostname_online.txt
    fi
    # 下线
    awk 'NR==FNR{a[$0]++} NR>FNR&&!a[$0]' /tmp/tmp/newhostname.txt /tmp/tmp/hostname_online.txt > /tmp/tmp/newhostname_uniqe_offline.txt
    if [ -s "/tmp/tmp/newhostname_uniqe_offline.txt" ] ; then
       content=`cat /tmp/tmp/newhostname_uniqe_offline.txt | grep -v "^$"`
       onlineDev=`cat /tmp/tmp/hostname_online.txt | grep -v "^$"`
       wxpushcontent='设备下线通知\n------------------\n'${content}'\n\n------------------\n'${onlineDev}
        msg_token=`curl -s -X GET "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$corp_id&corpsecret=$app_secret"`
            echo $msg_token > /tmp/tmp/wx.json
            wx_token=`jsonfilter -i /tmp/tmp/wx.json -e '$.access_token'`
            curl -s -X POST "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$wx_token" \
               -H "Content-Type: application/json" \
              --data "{\"touser\":\"@all\",\"agentid\":\"$app_id\",\"text\": {\"content\":\"$wxpushcontent\"}, \"msgtype\":\"text\",\"safe\":0}" &

       logger -t "wechat push" "设备下线: ${content} pushed"
       cat /tmp/tmp/newhostname.txt | grep -v "^$" > /tmp/tmp/hostname_online.txt
    fi
fi
resub=`expr $resub + 1`
[ "$resub" -gt 360 ] && resub=1
else
logger -t "wxpusher service" "Check network failed."
resub=1
fi
sleep 30
continue
done

把上面的代码保存在/root/serverchan.sh

再在管理界面-系统-startup最下面位置的脚本块的结尾加入一行

(/root/serverchan.sh) &

或者其他方式随系统启动脚本就成功了。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-05-31 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • OpenWRT简易版serverchan脚本推送至企业微信
相关产品与服务
弹性公网 IP
弹性公网 IP(Elastic IP,EIP)是可以独立购买和持有,且在某个地域下固定不变的公网 IP 地址,可以与 CVM、NAT 网关、弹性网卡和高可用虚拟 IP 等云资源绑定,提供访问公网和被公网访问能力;还可与云资源的生命周期解耦合,单独进行操作;同时提供多种计费模式,您可以根据业务特点灵活选择,以降低公网成本。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档