从squid.conf文件动态获取端口并使用bash将它们放到json文件中

时间:2016-12-22 09:43:07

标签: json bash

我有一个任务,我需要从多个配置squid文件中取出端口并将它们放到json文件中。

到目前为止,我能够按照下一种方式做到这一点:

echo -n "{ \"data\":" > test.json
echo -n "  [" >> test.json

sqcur=0
sqtotal=`ls /etc/squid?.conf | wc -l`
tcp_ports=$(grep "port" /etc/squid?.conf |awk '{print $2}' |sed 's|.*[0-9]*\:||g'|sort|uniq|xargs)

for squidconf in `ls /etc/squid?.conf`; do
  let "sqcur+=1" > /dev/null
  squidident=`echo $squidconf | sed 's/.*\///' | awk -F '.' '{print $1}'`  
  if [ $sqcur -lt $sqtotal ]; then
    echo -n "    { \"{#SQPROC}\": \"/usr/local/squid/bin/squid\", \"{#SQPROC_IDENT}\": \"${squidident}\", \"{#SQPROC_ARGS}\": \"-D -C -F -f ${squidconf}\", \"{#SQPROC_PORT}\": \"${tcp_ports}\", \"{#SQPROC_CONF}\": \"${squidconf}\" }," >> test.json
  else
    echo -n "    { \"{#SQPROC}\": \"/usr/local/squid/bin/squid\", \"{#SQPROC_IDENT}\": \"${squidident}\", \"{#SQPROC_ARGS}\": \"-D -C -F -f ${squidconf}\", \"{#SQPROC_PORT}\": \"${tcp_ports}\", \"{#SQPROC_CONF}\": \"${squidconf}\" }" >> test.json  
  fi
done

echo -n "  ]" >> test.json
echo "}" >> test.json

输出将是下一个:

{
    "data": [
        {
            "{#SQPROC}": "/usr/local/squid/bin/squid",
            "{#SQPROC_IDENT}": "squid1",
            "{#SQPROC_ARGS}": "-D -C -F -f /etc/squid1.conf",
            "{#SQPROC_PORT}": "1111 2222 3333 4444",
            "{#SQPROC_CONF}": "/etc/squid1.conf"
        },
        {
            "{#SQPROC}": "/usr/local/squid/bin/squid",
            "{#SQPROC_IDENT}": "squid2",
            "{#SQPROC_ARGS}": "-D -C -F -f /etc/squid2.conf",
            "{#SQPROC_PORT}": "1111 2222 3333 4444",
            "{#SQPROC_CONF}": "/etc/squid2.conf"
        },
        {
            "{#SQPROC}": "/usr/local/squid/bin/squid",
            "{#SQPROC_IDENT}": "squid3",
            "{#SQPROC_ARGS}": "-D -C -F -f /etc/squid3.conf",
            "{#SQPROC_PORT}": "1111 2222 3333 4444",
            "{#SQPROC_CONF}": "/etc/squid3.conf"
        }
    ]
}

但我需要在每个配置文件中定义的端口只在json文件中与定义它们的配置文件相关联。

例如1111和2222在squid1.conf中定义,因此json的输出必须是下一个:

{
        "data": [
            {
                "{#SQPROC}": "/usr/local/squid/bin/squid",
                "{#SQPROC_IDENT}": "squid1",
                "{#SQPROC_ARGS}": "-D -C -F -f /etc/squid1.conf",
                "{#SQPROC_PORT}": "1111 2222",
                "{#SQPROC_CONF}": "/etc/squid1.conf"
            },

依此类推其他配置文件。

你能帮忙吗?

1 个答案:

答案 0 :(得分:1)

您可以在循环内移动tcp_ports的定义, 并仅在当前配置文件中运行它:

for squidconf in `ls /etc/squid?.conf`; do
  let "sqcur+=1" > /dev/null
  squidident=`echo $squidconf | sed 's/.*\///' | awk -F '.' '{print $1}'`  
  tcp_ports=$(grep "port" $squidconf |awk '{print $2}' |sed 's|.*[0-9]*\:||g'|sort|uniq|xargs)
  if [ $sqcur -lt $sqtotal ]; then
    echo -n "    { \"{#SQPROC}\": \"/usr/local/squid/bin/squid\", \"{#SQPROC_IDENT}\": \"${squidident}\", \"{#SQPROC_ARGS}\": \"-D -C -F -f ${squidconf}\", \"{#SQPROC_PORT}\": \"${tcp_ports}\", \"{#SQPROC_CONF}\": \"${squidconf}\" }," >> test.json
  else
    echo -n "    { \"{#SQPROC}\": \"/usr/local/squid/bin/squid\", \"{#SQPROC_IDENT}\": \"${squidident}\", \"{#SQPROC_ARGS}\": \"-D -C -F -f ${squidconf}\", \"{#SQPROC_PORT}\": \"${tcp_ports}\", \"{#SQPROC_CONF}\": \"${squidconf}\" }" >> test.json  
  fi
done