linux bash - 动态更新 squid 服务的配置文件

linux bash - dynamically update config file for squid service

我创建了一个 bash 脚本来检查我想在 squid 中用作转发代理的一些代理服务器:

#!/bin/sh

PROXY_LIST="1.1.1.1:3128 1.2.2.2:3128"
CHECK_URL="https://google.com"
SQUID_CFG="/etc/squid/squid.conf"

for proxy in $PROXY_LIST; do
curl -s -k -x http://$proxy -I $CHECK_URL > /dev/null

if [ $? == 0 ]; then
  echo "Proxy $proxy is working!"
  echo $proxy > proxy-good.txt
else
  echo "Proxy $proxy is bad!"
  echo $proxy > proxy-bad.txt
fi
done

#update config
#service squid reload

squid 中的静态配置如下所示:

cache_peer 1.1.1.1 parent 3128 0 no-query default
cache_peer 1.2.2.2 parent 3128 0 no-query default

当代理是坏的或好的时,从我的 bash 脚本更新配置文件的最佳方法是什么?我如何在 bash 或其他编程语言中做到这一点?

#!/bin/sh

PROXY_LIST="1.1.1.1:3128 1.2.2.2:3128"
CHECK_URL="https://google.com"
SQUID_CFG="/etc/squid/squid.conf"

for proxy in $PROXY_LIST; do
curl -s -k -x http://$proxy -I $CHECK_URL > /dev/null

if [ $? == 0 ]; then
  echo "Proxy $proxy is working!"
  echo $proxy > proxy-good.txt
  echo "cache_peer ${proxy%%:*} parent ${proxy##*:} 0 no-query default" >> "$SQUID_CONFIG"
  # ${proxy%%:*} - represents the IP address
  # ${proxy##*:} - represents the port
  # Add the cache peer line to the end of the squid config file
else
  echo "Proxy $proxy is bad!"
  echo $proxy > proxy-bad.txt
  sed -i "/^cache_peer ${proxy%%:*} parent ${proxy##*:} 0 no-query default/d" "$SQUID_CONFIG"
  # Use the port and IP with sed to search for the cache peer line and then delete.
fi
done