bash - Using sed to edit milter-greylist config file -


virtualmin supports manually configuring domains email rate limiting. create script automatically add domain milter-greylist , define rate limit per hour automatically.

when domain (example.com) set rate-limiting of 500 emails per hour, adds following 3 lines in /etc/milter-greylist/greylist.conf:

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

those 3 lines inserted above racl whitelist default. string of numbers after domain domain id, , can found executing:

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

i’m trying insert 3 lines when server created, , remove 3 lines when server deleted. have pretty rudimentary scripting skills , came with:

#!/bin/bash  # script executed when changes made server  ## add new domain milter-greylist rate limiting if [ "$virtualserver_action" = "create_domain" ]; id=$(virtualmin list-domains --domain $virtualserver_dom --id-only) && sed -i '/racl whitelist default/ a\ratelimit "domain_"$id rcpt 500 / 1h\n racl blacklist /.*@$virtualserver_dom/ ratelimit "domain_"$id msg "message quota exceeded"\n racl whitelist /.*@$virtualserver_dom/\n' /etc/milter-greylist/greylist.conf  fi  ## remove domain milter-greylist on domain deletion if [ "$virtualserver_action" = "delete_domain" ]; 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 

which results in:

racl whitelist default ratelimit "domain_"$id rcpt 500 / 1h  ratelimit "domain_"$id rcpt 500 / 1h  /etc/milter-greylisting/greylisting.conf 

can advise need fix?

you used ' quotes in sed expression , bash expansion didn't happen. following script should work.

## add new domain milter-greylist rate limiting if [ "$virtualserver_action" = "create_domain" ];   id=$(virtualmin list-domains --domain $virtualserver_dom --id-only)   sed -i "/racl whitelist default/i \ratelimit \"domain_$id\" rcpt 500 / 1h\nracl blacklist /.*@$virtualserver_dom/ ratelimit \"domain_$id\" msg \"message quota exceeded\"\nracl whitelist /.*@$virtualserver_dom/\n" /etc/milter-greylist/greylist.conf  fi  ## remove domain milter-greylist on domain deletion if [ "$virtualserver_action" = "delete_domain" ];   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 

tested in gnu sed version 4.2.1. hope helps.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -