使用sed编辑milter-greylist配置文件

时间:2014-06-03 10:59:56

标签: bash sed

Virtualmin支持手动配置域以限制电子邮件速率。我想创建一个脚本来自动将域添加到milter-greylist并自动定义每小时的速率限制。

当域(example.com)设置为每小时500封电子邮件的速率限制时,它会在/etc/milter-greylist/greylist.conf中添加以下三行:

ratelimit "domain_14014450697382" rcpt 500 / 1h
racl blacklist from /.*@example.com/ ratelimit "domain_14014450697382" msg "Message quota exceeded"
racl whitelist from /.*@example.com`

这三行插在racl whitelist default之上。域之后的数字字符串是域ID,可以通过执行以下命令找到:

virtualmin list-domains --domain $VIRTUALSERVER_DOM --id-only

我正在尝试在创建服务器时插入这三行,并在删除服务器时删除这三行。我有非常基本的脚本编写技巧,并提出:

#!/bin/bash

# script is executed when changes are made to server

## Add new domain to milter-greylist for rate limiting
if [ "$VIRTUALSERVER_ACTION" = "CREATE_DOMAIN" ]; then
ID=$(virtualmin list-domains --domain $VIRTUALSERVER_DOM --id-only) &&
sed -i '/racl whitelist default/ a\ratelimit "domain_"$ID rcpt 500 / 1h\n
racl blacklist from /.*@$VIRTUALSERVER_DOM/ ratelimit "domain_"$ID msg "Message quota exceeded"\n
racl whitelist from /.*@$VIRTUALSERVER_DOM/\n' /etc/milter-greylist/greylist.conf 
fi

## Remove domain from milter-greylist on domain deletion
if [ "$VIRTUALSERVER_ACTION" = "DELETE_DOMAIN" ]; then
ID=$(virtualmin list-domains --domain $VIRTUALSERVER_DOM --id-only) &&
sed -i '/$ID/d' /etc/milter-greylist/greylist.conf &&
sed -i '/$VIRTUALSERVER_DOM/d' /etc/milter-greylist/greylist.conf
fi

导致:

racl whitelist default
ratelimit "domain_"$ID rcpt 500 / 1h

ratelimit "domain_"$ID rcpt 500 / 1h

/etc/milter-greylisting/greylisting.conf

有人可以建议我需要解决的问题吗?

1 个答案:

答案 0 :(得分:2)

您在'表达式中使用了sed引号,因此没有发生bash扩展。以下脚本应该有效。

## Add new domain to milter-greylist for rate limiting
if [ "$VIRTUALSERVER_ACTION" = "CREATE_DOMAIN" ]; then
  ID=$(virtualmin list-domains --domain $VIRTUALSERVER_DOM --id-only)
  sed -i "/racl whitelist default/i \ratelimit \"domain_$ID\" rcpt 500 / 1h\nracl blacklist from /.*@$VIRTUALSERVER_DOM/ ratelimit \"domain_$ID\" msg \"Message quota exceeded\"\nracl whitelist from /.*@$VIRTUALSERVER_DOM/\n" /etc/milter-greylist/greylist.conf 
fi

## Remove domain from milter-greylist on domain deletion
if [ "$VIRTUALSERVER_ACTION" = "DELETE_DOMAIN" ]; then
  ID=$(virtualmin list-domains --domain $VIRTUALSERVER_DOM --id-only)
  sed -i "/$ID/d" /etc/milter-greylist/greylist.conf
  sed -i "/$VIRTUALSERVER_DOM/d" /etc/milter-greylist/greylist.conf
fi

GNU sed version 4.2.1中测试。希望有所帮助。