mail to all users on a system (from root)

Mike Meyer mwm-dated-1050677398.69b027 at mired.org
Sun Apr 13 07:50:00 PDT 2003


In <20030413142842.GB25131 at happy-idiot-talk.infracaninophi>, Matthew Seaman <m.seaman at infracaninophile.co.uk> typed:
> On Sun, Apr 13, 2003 at 02:32:35PM +0200, Aslak Evang wrote:
> > I've searched for ways for root to email all users on a system. The only
> > easy solution I found included making a shellscript and setting it as an
> > alias for "everybody". Problem was that in the script you had to insert
> > all your users manually.
> > Does anybody know of other methods for sending announcements to all
> > users on a system? Could have done it with MOTD but few of them actually
> > log-in. Most just read mail.
> 
> Try the attached script which I just slung together.  To use, save the
> script into a defined place on your HD, say /usr/local/sbin/everybody,
> and then set up an alias:
> 
>     everybody:	"| /usr/local/sbin/everybody"
> 
> This script will resend the message to all users with an account on
> the system -- I've assumed that real people have UIDs over 1000 and
> system accounts have UIDs less than that.  Note that this is just a
> quick hack and could be vastly improved --- certainly made much more
> efficient --- with a little effort.  Only minimally tested: use very
> cautiously.

The script is a nice effort, but has a serious efficiency problem -
it's going to fork a sendmail for every user on the system. For small
numbers of users, that's acceptable. The attached everybody script
will connect to the SMTP server on the local system and send just one
copy of the message, allowing the SMTP server to deal with making
multiple copies to send to everyone. The downside of this script is
that it's going to keep the list uf usernames in memory, which will
eventually cause problems - but not before you've reached numbers that
would make spammers notice.

	<mike

#!/usr/bin/env python

"Forward email to everybody on the system."

import sys, os, pwd, smtplib

users = [pwd[0] for pwd in pwd.getpwall() if pwd[2] > 1000 and pwd[0] != 'nobody']
mailer = smtplib.SMTP('localhost')
mailer.sendmail(os.environ['USER'], users, sys.stdin.read())



-- 
Mike Meyer <mwm at mired.org>		http://www.mired.org/consulting.html
Independent Network/Unix/Perforce consultant, email for more information.


More information about the freebsd-questions mailing list