linux 拨号+squid监控脚本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux 拨号+squid监控脚本相关的知识,希望对你有一定的参考价值。

客户端

#!/bin/bash

#get_memory-info
a=`free -m|grep Mem|awk {print$2}`                    #total-memory
b=`free -m|grep Mem|awk {print$3}`                    #used-memory
mem_percent=`awk BEGIN{printf "%.2f%\n",($b/$a)*100}`     #used-memory-%
#echo $mem_percent

#get_network_info
ping -W 5  -c 1 114.114.114.114 > /dev/null 2>&1
if [ $? -eq 0 ];then
        net_status="0"
else
        net_status="1"
fi
#echo $net_status


#get_squid_process_status
squid_status=`systemctl status squid|grep Active|awk {print$2}`
if [ "$squid_status" = "active" ];then
        squid_status_code="0"
else
        squid_status_code="1"
fi
#echo $squid_status_code

echo "\"memory\":\""$mem_percent"\",\""net_status\":\""$net_status"\",\""squid_status\":\""$squid_status_code"\""

服务端:

#!/bin/bash

machine_ip=$1
machine_port=$2

 f1=`cat /proc/sys/kernel/random/uuid`
 f2=`cat /proc/sys/kernel/random/uuid`
 f3=`cat /proc/sys/kernel/random/uuid`


rm -rf /tmp/.$f1.txt /tmp/.$f2.txt /tmp/.$f3.txt
touch /tmp/.$f1.txt /tmp/.$f2.txt /tmp/.$f3.txt

{
 #echo "get machine_status"
 machine_status=`ssh -o ConnectTimeout=6 -p $machine_port [email protected]$machine_ip sh /root/get_machine_info.sh`
 
 len=`echo $machine_status|wc -c`
 if [ $len -gt 10  ];then
     echo $machine_status >  /tmp/.$f1.txt
 else
        echo "\"memory\":\""1"\",\""net_status\":\""1"\",\""squid_status\":\""1"\""    > /tmp/.$f1.txt
 fi
}&

{
        #echo "get wan ip"
        host=`ssh -o ConnectTimeout=3 -p $machine_port [email protected]$machine_ip sh /root/get_wan_ip.sh|awk -F: {print$1}`
        #echo $host
        #ssh -o ConnectTimeout=3  [email protected]$host "date" > /dev/null
        #echo "test wan ip for 22"
    
        ping -W 3  -c 1 $host > /dev/null 2>&1
        if [ $? -eq 0  ];then
            #proxy_ip_port_status=`echo "\"proxy_ip\":\"0\",\""proxy_port\":\"1\"`
            echo "\"proxy_ip\":\"0\",\""proxy_port\":\"1\" > /tmp/.$f3.txt
        else
            #proxy_ip_port_status=`echo "\"proxy_ip\":\"1\",\""proxy_port\":\"0\"`
            echo echo "\"proxy_ip\":\"1\",\""proxy_port\":\"1\" > /tmp/.$f3.txt
        fi
}&

{
 #echo "get proxy_ip_port"
 proxy_ip_port=`ssh -o ConnectTimeout=3 -p $machine_port [email protected]$machine_ip sh /root/get_wan_ip.sh`
 
 if [ $? -eq 0  ] && [ "$proxy_ip_port" != "null"  ];then
    #echo "get curl result"
     result=`curl  -I  -m 5 -x $proxy_ip_port -o  /dev/null -s -w %{http_code} map.baidu.com`
    
    
    if [ $result -eq 200 ];then
        proxy_ip_port_status=`echo "\"proxy_ip\":\"0\",\""proxy_port\":\"0\"`    
    else
        #echo "get wan ip"
        #host=`ssh -o ConnectTimeout=3 -p $machine_port [email protected]$machine_ip sh /root/get_wan_ip.sh|awk -F: {print$1}`
        
        #ssh -o ConnectTimeout=3  [email protected]$host "date" > /dev/null
        #echo "test wan ip for 22"
        #if [ $? -eq 0  ];then
        #    proxy_ip_port_status=`echo "\"proxy_ip\":\"0\",\""proxy_port\":\"1\"`
        #else
        #    proxy_ip_port_status=`echo "\"proxy_ip\":\"1\",\""proxy_port\":\"0\"`
        #fi
        f3v=`cat /tmp/.$f3.txt|wc -c`
        while (($f3v < 4))
        do
            f3v=`cat /tmp/.$f3.txt|wc -c`
        done
        proxy_ip_port_status=`cat /tmp/.$f3.txt`
    fi
 else
      proxy_ip_port_status=`echo "\"proxy_ip\":\"1\",\""proxy_port\":\"1\"`  
 fi
 echo $proxy_ip_port_status > /tmp/.$f2.txt
}&

wait

 proxy_ip_port_status=`cat /tmp/.$f2.txt`
 machine_status=`cat /tmp/.$f1.txt`
 rm -rf /tmp/.$f1.txt /tmp/.$f2.txt /tmp/.$f3.txt

#echo $machine_status
#echo $proxy_ip_port_status
echo "{"$machine_status","$proxy_ip_port_status"}"

 

以上是关于linux 拨号+squid监控脚本的主要内容,如果未能解决你的问题,请参考以下文章

自动安装squid+iptables上网代理及上网行为管理脚本。

linux下ppp拨号无线上网

2022 LINUX运维面试题目精选

2022 LINUX运维面试题目精选

PPP 拨号上网步骤

学习Linux第十八节课