Monday, March 21, 2011

Bash Scripting repetitive tasks

An audit on a Linux server made it necessary to get information regarding every user on the machine, and match those users to a mail directory (which may or may not have a different name). Instead of running the 'finger' command three hundred times and copying the output somewhere I used this:

#! /bin/sh
while read username; do
finger ${username}
done < inputfile > outputfile
exit 0

I saved this as finger_script.sh and copied each of the usernames to the file 'inputfile'. The 'inputfile' contained just the usernames that I cut and pasted from the mail directory in question, and looked like this:

bill frank keith user01 user05 user09
carl gary larry user02 user06 user10
dan heather mary user03 user07 user11
denise joe nancy user04 user08 user12

Running finger_script.sh ran the finger command on each user in the 'inputfile' and immediately output a list of users that did not exist. It also created 'outputfile', which contained a list of the information on each user:

Login: bill Name: Bill Person
Directory: /home/b/bill Shell: /bin/nologon
Never logged in.
No mail.
No Plan.

Login: carl Name: Carl Guy
Directory: /users/carl Shell: /bin/nologon
Never logged in.
No mail.
No Plan.

Login: dan Name: Dan Theman
Directory: /users/dan Shell: /bin/nologon
Never logged in.
No mail.
No Plan.

0 comments:

Post a Comment

 
Contact our honeypot department if you are desperate to get blacklisted.