Free tech support / small logo


Shell script to scrape /etc/passwd data?

I am working on a script where I would like to grab the usernames out of the passwd file and add a emaildomain then ftp it. For example I would like to pull the usernames for all users in /etc/passwd and add @domain.com the ftp it to ftp.domain.com . Can you help?


Dave's Answer:

This is a surprisingly easy scripting task, actually, but there are some nuances worth mentioning: if you just blindly go through the /etc/passwd file, where all accounts are saved, you'll end up with lots of addresses like "postmaster" and "admin" and "ftp" and "uucp" and so on. Probably not what you want.

So the first step isn't just to pull out the account names, but to have some sort of rule-based extraction where you, for example, don't grab anything with a UserID of less than 100. This can be most easily done with the awk command, a highly scriptable and flexible pattern matching utility. Here's how I'd do this first step:

awk -F: '{ if ($3 > 99) { print $1 }}' /etc/passwd

This will pull out all the account names where the ID is greater than 99, which should accomplish the first step of your task. The "-F:" tells awk to use colons as the field separators, then "$1" is the first field (account name) and "$3" is the account ID.

Now you have a stream of account names, guaranteed not to have spaces, so this can easily be produced in a subshell and then fed into a looping structure, thusly:

for acct in $(awk -F: '{ if ($3 > 88) { print $1 }}' /etc/passwd)
do
  echo "$acct@hostname.com"
done

This four line Bourne shell script will do exactly what you seek. Change "hostname.com" to be the desired domain name, and run this script on your Linux or Unix box, and the output will be a stream of email addresses as per your specification.

I hope this helps you out! Also, I have a lot more about shell script programming in my acclaimed book Wicked Cool Shell Scripts, if this whets your appetite!









Subscribe!
Never miss another Q&A article! Click to subscribe: Add to Google Reader Add to My Yahoo! Subscribe in NewsGator RDF XML
Comments

Dave-

I think you have a typo in your full script.

Instead of:

awk -F: '{ if ($3 > 99) { print $1 }}' /etc/passwd

you have

awk -F: '{ if ($3 > 88) { print $1 }}' /etc/passwd

That changes the meaning a bit, so I am pretty sure it is a mistake.

Posted by: Jim at May 4, 2007 11:15 PM

You're right! Fortunately, it's darn easy to fix. :-)

Posted by: Dave Taylor at May 6, 2007 10:04 PM

You do not need the "for acct ..."-loop if you
extend the awk-command with "@hostname.com" to:
awk -F: '{ if ($3 > 99) { print $1 "@hostname.com" }}' /etc/passwd

Posted by: harald eck at December 31, 2007 8:30 AM

I have something to say, now that you mention it, but ...
Starbucks coffee cup I do have a lot to say, and questions of my own for that matter, but first I'd like to say thank you for all your efforts on this Web site by buying you a cup of coffee!

I do have a comment, now that you mention it!











Remember personal info?


Please note that I will never send you any unsolicited email. Ever.

While I'm at it, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site.









Recent Entries


Search
I Need Help!
Need Help? Ask Dave Taylor!


© 2002 - 2012 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site.

[whiteboard marker tray]
"Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.