Páginas

quinta-feira, 2 de dezembro de 2010

batch processing zimbra commands « Flylife!

batch processing zimbra commands « Flylife!: "batch processing zimbra commands

One of the most common questions on the Zimbra forums is How do I do _________ for every user? While there is no way to apply a change to multiple users at once through the web admin interface, this is quite easy using the cli tools. Here’s a brief overview of the methods

So lets say you want to force every user on your system to change their password on the next login. The easiest method is to just loop through the results of a zmprov getAllAccounts and run the appropriate command on the results. Of course you don’t want to do this for your admin account or any of the system accounts, but that’s easy enough to exclude. Here is an example script you can run

#!/bin/sh
for i in `zmprov -l getAllAccounts | grep -v '^admin\@\|^wiki\@\|^spam\..*@\|^ham\..*@'`
do
zmprov modifyAccount $i zimbraPasswordMustChange TRUE
done

This is probably the easiest way to accomplish the task. You can change the zmprov line to do do just about anything. However, this is about the slowest method as it starts a separate instance of zmprov each time it’s run. This means a java vm has to be started up and torn down for each command. But if you only have a few dozen users or so it’s really not a huge deal.

But what do you do when you have thousands of users? Here is an example script that prepares all the commnands ahead of time in a temporary file, and then starts one instance of zmprov and pipes the file to that instance.

#!/bin/sh
COMFILE=/tmp/zmbatch.$$
for i in `zmprov -l getAllAccounts | grep -v '^admin\@\|^wiki\@\|^spam\..*@\|^ham\..*@'`
do
echo 'modifyAccount $i zimbraPasswordMustChange TRUE' >> $COMFILE
done
zmprov < $COMFILE
rm $COMFILE

If you want, you can remove the last 2 lines and then you can review the temp file it creates and make sure everything looks like what you want before you run zmprov < /tmp/zmbatch.xxxxx yourself. When you do it this way, you will see a bunch of prov> ‘s print out each time it modifies an account. This is just uncaptured output from zmprov that you can ignore and if you’re that bothered by it modify the script to pipe output to /dev/null or something.

How much faster is this method? Well, on my test system which has 13 non system accounts here is the results of running the time command on the first script

real 0m47.541s
user 0m37.278s
sys 0m5.580s

and here are the results from running time on the second script

real 0m8.575s
user 0m5.312s
sys 0m0.912s

so yeah if you have a lot of users you definately want to go the second route.

– Enviado usando a Barra de Ferramentas Google"