From owner-freebsd-questions@FreeBSD.ORG Mon Apr 14 02:16:51 2003 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 6F2C137B401 for ; Mon, 14 Apr 2003 02:16:48 -0700 (PDT) Received: from smtp.infracaninophile.co.uk (smtp.infracaninophile.co.uk [81.2.69.218]) by mx1.FreeBSD.org (Postfix) with ESMTP id 44A3043F3F for ; Mon, 14 Apr 2003 02:16:46 -0700 (PDT) (envelope-from m.seaman@infracaninophile.co.uk) Received: from happy-idiot-talk.infracaninophile.co.uk (localhost [127.0.0.1]) h3E9GFYk033473 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Mon, 14 Apr 2003 10:16:15 +0100 (BST) (envelope-from matthew@happy-idiot-talk.infracaninophile.co.uk) Received: (from matthew@localhost)h3E9GEfc033472; Mon, 14 Apr 2003 10:16:14 +0100 (BST) (envelope-from matthew) Date: Mon, 14 Apr 2003 10:16:14 +0100 From: Matthew Seaman To: Mike Meyer Message-ID: <20030414091614.GA33194@happy-idiot-talk.infracaninophi> Mail-Followup-To: Matthew Seaman , Mike Meyer , Aslak Evang , questions@freebsd.org References: <007a01c301b8$c329c290$3800000a@LAPDANCE> <20030413142842.GB25131@happy-idiot-talk.infracaninophi> <16025.30997.412672.836740@guru.mired.org> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="KsGdsel6WgEHnImy" Content-Disposition: inline In-Reply-To: <16025.30997.412672.836740@guru.mired.org> User-Agent: Mutt/1.5.4i X-Spam-Status: No, hits=-38.8 required=5.0 tests=EMAIL_ATTRIBUTION,IN_REP_TO,PGP_SIGNATURE_2, QUOTED_EMAIL_TEXT,REFERENCES,REPLY_WITH_QUOTES, USER_AGENT_MUTT version=2.53 X-Spam-Checker-Version: SpamAssassin 2.53 (1.174.2.15-2003-03-30-exp) cc: questions@freebsd.org Subject: Re: mail to all users on a system (from root) X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 14 Apr 2003 09:16:52 -0000 --KsGdsel6WgEHnImy Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Sun, Apr 13, 2003 at 09:49:57AM -0500, Mike Meyer wrote: > In <20030413142842.GB25131@happy-idiot-talk.infracaninophi>, Matthew Seam= an 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 o= nly > > > 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 inse= rt > > > 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 actua= lly > > > log-in. Most just read mail. > >=20 > > 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: > >=20 > > everybody: "| /usr/local/sbin/everybody" > >=20 > > 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. >=20 > 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. >=20 > =20 > #!/usr/bin/env python >=20 > "Forward email to everybody on the system." >=20 > import sys, os, pwd, smtplib >=20 > users =3D [pwd[0] for pwd in pwd.getpwall() if pwd[2] > 1000 and pwd[0] != =3D 'nobody'] > mailer =3D smtplib.SMTP('localhost') > mailer.sendmail(os.environ['USER'], users, sys.stdin.read()) >=20 Yeah. Like I said. If you've got a significant number of users (more than about 20 say) and there's quite a bit of use of the 'everybody@' alias, then it's going to be far more efficient just to extract the list of users from the password database, using something like this script but written in whatever your favourite language happens to be: #!/usr/bin/perl -w =20 use User::pwent qw{:FIELDS}; =20 # Take an E-mail message on stdin and forward it to all users on the # system. Assumes that real users have UIDs >=3D 1000 and that system # accounts do not (other than the nobody account, of course). =20 MAIN: { while ( getpwent() ) { next if ( $pw_uid < 1000 || $pw_name eq 'nobody' ); =20 print "$pw_name\n"; } } Run that on a regular basis --- say every few hours during the day via a cron job: 3 9,11,13,15,17 * * * /usr/local/bin/everybody > /etc/mail/everybody= =2Elist and set up an alias that includes that list in /etc/mail/aliases: everybody: :include: /etc/mail/everybody.list Nb. using an external file means that you don't need to run newaliases(1) every time the list of user names changes. The 'everybody' script I wrote yesterday is, I will freely admit, a hack. Cute, in some ways, but not production quality. What can I say? Writing it certainly beat cleaning the kitchen floor, which is what I *should* have been doing... Cheers, Matthew --=20 Dr Matthew J Seaman MA, D.Phil. 26 The Paddocks Savill Way PGP: http://www.infracaninophile.co.uk/pgpkey Marlow Tel: +44 1628 476614 Bucks., SL7 1TH UK --KsGdsel6WgEHnImy Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.1 (FreeBSD) iD8DBQE+mnxedtESqEQa7a0RAv8vAJ0Z7pQnMtTgDxL/3epBhI8ojHOcJACgmuvR JUAIUiZ8bZXeXnvJ7uz/Bfs= =/tKN -----END PGP SIGNATURE----- --KsGdsel6WgEHnImy--