需要 shell 脚本将 CSV 转换为 Apache httpd 格式

需要 shell 脚本将 CSV 转换为 Apache httpd 格式

需要在脚本上指向正确的方向来获取和正则表达式或 sed。

Site24x7 提供了一个 URL,其中包含用于监控的源 IP 的 CSV 列表。 (他们还提供其他格式,CSV 似乎是最不混乱的,因为它们的结构还有很多不足之处。https://www.site24x7.com/multi-location-web-site-monitoring.html

就像这样:

Country,City,IP Address External
Australia,Sydney,"101.0.67.53"
Australia,Melbourne,"125.214.65.59"
Belgium,Brussels,"87.238.165.164"
Brazil,São Paulo,"200.170.83.170"
Brazil,Rio de Janeiro,"201.20.20.237"
Canada,Toronto,"208.69.56.166,
208.69.56.171,
208.69.56.172 "
Canada,Montreal,"199.204.45.153,
199.204.45.154,
199.204.45.155,
199.204.45.156"

我需要将其保存为 apache 中的允许包含文件。就像这样:

Allow from \
72.5.230.111 \
72.5.230.65 \
72.5.230.84

答案1

#!/bin/bash

webpage="https://www.site24x7.com/multi-location-web-site-monitoring.html"
csv=$(curl -s "$webpage" | grep 'title="CSV"' | \
    sed 's/^.*href="\(http[^ ]*\)".*$/\1/')

echo -e "Allow from \\"
curl -s "$csv" | egrep -o '[0-9.]{7,15}' | paste -s | sed 's/\t/ \\\n/g'

答案2

又快又脏,而且不是很坚固的sed(1)单行:

echo 'Country,City,IP Address External
Australia,Sydney,"101.0.67.53"
Australia,Melbourne,"125.214.65.59"
Belgium,Brussels,"87.238.165.164"
Brazil,São Paulo,"200.170.83.170"
Brazil,Rio de Janeiro,"201.20.20.237"
Canada,Toronto,"208.69.56.166,
208.69.56.171,
208.69.56.172 "
Canada,Montreal,"199.204.45.153,
199.204.45.154,
199.204.45.155,
199.204.45.156"' | 
    sed -e 1d -e 's/"$//' -e 's/^[^"]*"/Allow from /' -e 's/,$/ \\/'

输出:

Allow from 101.0.67.53
Allow from 125.214.65.59
Allow from 87.238.165.164
Allow from 200.170.83.170
Allow from 201.20.20.237
Allow from 208.69.56.166 \
208.69.56.171 \
208.69.56.172 
Allow from 199.204.45.153 \
199.204.45.154 \
199.204.45.155 \
199.204.45.156

假设:

  • 每个 IP 地址组都用双引号 ( ") 字符括起来
  • "除了分隔 IP 组之外,该字符不能出现在其他任何地方。
  • ,行尾的逗号 ( ) 表示下一行继续有多个 IP 地址。

答案3

我的解决方案使用 dns 代替:

echo -n 'Allow from ';
host -4 -T -t A site24x7.enduserexp.com |
cut -d ' ' -f 4 |
tr '\n' ' '

相关内容