|
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? 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!
Categorized:
Shell Script Programming
(Article 4264,
Written by Dave Taylor)
Tagged: Previous: How do I figure out my Amazon sales rank? Next: Approvals needed for screenshots? Subscribe!
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 PMYou're right! Fortunately, it's darn easy to fix. :-) Posted by: Dave Taylor at May 6, 2007 10:04 PMYou do not need the "for acct ..."-loop if you I have something to say, now that you mention it, but ...
I do have a comment, now that you mention it!
|
Recommended
Recent Entries
Search
I Need Help!
Apple iPad Help
Articles and Reviews Auctions and Online Shopping Blogs and RSS Feeds Building Web Site Traffic Business and Management CGI Scripts and Web Site Programming Computer and Internet Basics d) None of the Above Facebook Help Google Plus Help HTML and CSS Industry News and Trade Shows iPhone and Cell Phone Help iPod, Sony PSP and MP3 Player Help Mac OS X Help Pay Per Click (PPC) Advertising Search Engine Optimization (SEO) Shell Script Programming Tech Support Video Help The Writing Business Twitter, LinkedIn and Social Network Help Unix and Linux Help Video Game Tips and Help Windows PC Help WordPress Help |