Linux | 一个未完成的脚本

放一个有问题的脚本

待修改

#!/bin/sh
source /etc/storage/script/init.sh
export PATH='/etc/storage/bin:/tmp/script:/etc/storage/script:/opt/usr/sbin:/opt/usr/bin:/opt/sbin:/opt/bin:/usr/local/sbin:/usr/sbin:/usr/bin:/sbin:/bin'
export LD_LIBRARY_PATH=/lib:/opt/lib
serverchan_enable=`nvram get serverchan_enable`
serverchan_enable=${serverchan_enable:-"0"}
serverchan_sckey=`nvram get serverchan_sckey`
serverchan_notify_1=`nvram get serverchan_notify_1`
serverchan_notify_2=`nvram get serverchan_notify_2`
serverchan_notify_3=`nvram get serverchan_notify_3`
serverchan_notify_4=`nvram get serverchan_notify_4`
mkdir -p /tmp/var
resub=1

# 读取WAN口地址
wanIPAddress() {
    ifconfig ppp0 | awk -F'[: ]+' '{print $4}' | grep "\."
}
# 读取最近外网地址
lastIPAddress() {
    inter="/etc/storage/lastIPAddress"
    cat $inter
}
# WAN流量统计
wanDataFlow() {
    ifconfig ppp0 | awk '{print "WAN接收:" $3$4,"发送:" $7$8}' | grep "iB"
}

while test "$serverchan_enable" = "1" ;
do
    serverchan_enable=`nvram get serverchan_enable`
    serverchan_enable=${serverchan_enable:-"0"}
    serverchan_sckey=`nvram get serverchan_sckey`
    serverchan_notify_1=`nvram get serverchan_notify_1`
    serverchan_notify_2=`nvram get serverchan_notify_2`
    serverchan_notify_3=`nvram get serverchan_notify_3`
    serverchan_notify_4=`nvram get serverchan_notify_4`
    ping_text=`ping -4 114.114.114.114 -c 1 -w 2 -q`
    ping_time=`echo $ping_text | awk -F '/' '{print $4}'| awk -F '.' '{print $1}'`
    ping_loss=`echo $ping_text | awk -F ', ' '{print $3}' | awk '{print $1}'`

    if test ! -z "$ping_time"  ; then
        echo "ping:$ping_time ms 丢包率:$ping_loss"
    else
        echo "ping:失效"
    fi
    if test ! -z "$ping_time"  ; then
        echo "online"

########### IP变动提醒
        if test "$serverchan_notify_1" = "1"  ; then
            hostIP=$(wanIPAddress)
            lastIP=$(lastIPAddress)
                        DataFlow=$(wanDataFlow)
            if test [ "$lastIP" != "$hostIP" ] && [ ! -z "$hostIP" ]; then
                sleep 60
                hostIP=$(wanIPAddress)
                lastIP=$(lastIPAddress)
                DataFlow=$(wanDataFlow)
            fi

            if test "$lastIP" != "$hostIP" && ! -z "$hostIP" ; then
                content="$hostIP,$DataFlow"
                curl -s "http://sc.ftqq.com/$serverchan_sckey.send?text=【"`nvram get computer_name`"】互联网IP变动" -d "&desp=$content" &
                echo -n $hostIP > /etc/storage/lastIPAddress
            fi
        fi

########### 新设备接入提醒  
        if test "$serverchan_notify_2" = "1"  ; then
            # 获取接入设备名称
            cat /tmp/static_ip.inf | grep -v "^$" | awk -F "," '{ if ( $6 == 0 ) print $1"|,|"$2"|,|"$3}' >> /tmp/var/newhostname.txt
            # 读取以往上线设备名称
            test ! -s /etc/storage/hostname_上线.txt  && touch /etc/storage/hostname_上线.txt
            
            # 获取新接入设备名称
            awk 'NR==FNR{a[$0]++} NR > FNR&&a[$0]' /etc/storage/hostname.txt /tmp/var/newhostname.txt > /tmp/var/newhostname相同行.txt
            awk 'NR==FNR{a[$0]++} NR > FNR&&!a[$0]' /tmp/var/newhostname相同行.txt /tmp/var/newhostname.txt > /tmp/var/newhostname不重复.txt

            if test -s "/tmp/var/newhostname不重复.txt"  ; then
                sed -i '1i\|---|---|---|---|---|' /tmp/var/newhostname不重复.txt
                sed -i '1i\|IP|,|MAC|,|HOST|' /tmp/var/newhostname不重复.txt
                cat /tmp/var/newhostname不重复.txt | grep -v "^$" > /tmp/var/content.txt
                cat /tmp/var/newhostname.txt | grep -v "^$" >> /tmp/var/content.txt
                sed -i 's/://g' /tmp/var/content.txt
                content=`cat /tmp/var/content.txt`
                curl -s "http://sc.ftqq.com/$serverchan_sckey.send?text=【"`nvram get computer_name`"】新设备加入" -d "&desp=$content" &
                cat /tmp/var/newhostname不重复.txt | grep -v "^$" >> /etc/storage/hostname.txt
            fi
        fi

########### 设备上、下线提醒        
        if test "$serverchan_notify_4" = "1"  ; then
            # 获取接入设备名称
            cat /tmp/static_ip.inf | grep -v "^$" | awk -F "," '{ if ( $6 == 0 ) print $1"|,|"$2"|,|"$3}' >> /tmp/var/newhostname.txt
            # 读取以往上线设备名称
            test ! -s /etc/storage/hostname_上线.txt  && touch /etc/storage/hostname_上线.txt

            # 上线
            awk 'NR==FNR{a[$0]++} NR > FNR&&a[$0]' /etc/storage/hostname_上线.txt /tmp/var/newhostname.txt > /tmp/var/newhostname相同行_上线.txt
            awk 'NR==FNR{a[$0]++} NR > FNR&&!a[$0]' /tmp/var/newhostname相同行_上线.txt /tmp/var/newhostname.txt > /tmp/var/newhostname不重复_上线.txt
            if test -s "/tmp/var/newhostname不重复_上线.txt"  ; then
                sed -i '1i\|---|---|---|---|---|' /tmp/var/newhostname不重复_上线.txt
                sed -i '1i\|IP|,|MAC|,|HOST|' /tmp/var/newhostname不重复_上线.txt
                cat /tmp/var/newhostname不重复_上线.txt | grep -v "^$" > /tmp/var/content.txt
                cat /tmp/var/newhostname.txt | grep -v "^$" >> /tmp/var/content.txt
                sed -i 's/://g' /tmp/var/content.txt
                content=`cat /tmp/var/content.txt`
                curl -s "http://sc.ftqq.com/$serverchan_sckey.send?text=【"`nvram get computer_name`"】设备【上线】Online" -d "&desp=$content" &
                cat /tmp/var/newhostname不重复_上线.txt | grep -v "^$" >> /etc/storage/hostname_上线.txt
            fi
            
            # 下线
            awk 'NR==FNR{a[$0]++} NR > FNR&&!a[$0]' /tmp/var/newhostname.txt /etc/storage/hostname_上线.txt > /tmp/var/newhostname不重复_下线.txt
            if test -s "/tmp/var/newhostname不重复_下线.txt"  ; then
                sed -i '1i\|---|---|---|---|---|' /tmp/var/newhostname不重复_下线.txt
                sed -i '1i\|IP|,|MAC|,|HOST|' /tmp/var/newhostname不重复_下线.txt
                cat /tmp/var/newhostname不重复_下线.txt | grep -v "^$" > /tmp/var/content.txt
                cat /tmp/var/newhostname.txt | grep -v "^$" >> /tmp/var/content.txt
                sed -i 's/://g' /tmp/var/content.txt
                content=`cat /tmp/var/content.txt`
                curl -s "http://sc.ftqq.com/$serverchan_sckey.send?text=【"`nvram get computer_name`"】设备【下线】offline" -d "&desp=$content" &
                cat /tmp/var/newhostname.txt | grep -v "^$" > /etc/storage/hostname_上线.txt
            fi
        fi

########### 固件更新提醒
        if test "$serverchan_notify_3" = "1" && "$resub" = "1" ; then
            test ! -f /tmp/var/osub  && echo -n `nvram get firmver_sub` > /tmp/var/osub
            rm -f /tmp/var/nsub
            wgetcurl.sh "/tmp/var/nsub" "$hiboyfile/osub" "$hiboyfile2/osub"
            if test $(cat /tmp/var/osub) != $(cat /tmp/var/nsub) &&  -f /tmp/var/nsub ; then
                echo -n `nvram get firmver_sub` > /tmp/var/osub
                content="新的固件: `cat /tmp/var/nsub | grep -v "^$"` ,目前旧固件: `cat /tmp/var/osub | grep -v "^$"` "
                curl -s "http://sc.ftqq.com/$serverchan_sckey.send?text=【"`nvram get computer_name`"】固件更新提醒" -d "&desp=$content" &
                echo -n `cat /tmp/var/nsub | grep -v "^$"` > /tmp/var/osub
            fi
        fi
        resub=`expr $resub + 1`
        test "$resub" -gt 360  && resub=1
    else
        echo "Internet down 互联网断线"
        resub=1
    fi
    sleep 60
    continue
done

本文链接:

https://blog.zhigu34.cn/archives/61.html
1 + 1 =
快来做第一个评论的人吧~