Tuesday, November 18, 2008

Bash One-Liner Script To Produce Somewhat-Fancy Output Of Who's On Your Linux Or Unix Box

Hey there,

Today we're going to shoot out a quick one liner script that can come in handy from time to time. It doesn't fall under the "necessary" category (or, maybe not even the "useful" one ;), but it's nice to have for those times when you just don't feel like doing any extra typing or expending the effort required to separate the wheat from the chaff on a screen full of garbage output. That is, as I've always understood, one of the main reasons to script things out in the first place. The other reasons, like automation, efficiency, etc, are pretty much ignored here, even though we will be automating a repetitive action and that, by definition, will make our waste of company time more efficient. No matter how you look at them, automation and efficiency always seem to be a good thing ;)

This "script" is really just a simple pipe-slam so that we can figure out who's on the machine with us (my preference is for a script, but - as you'll soon see - this could just as easily be an alias, etc). Sure, we could just type "w" or "who," (and "w" does form the base of this bash one-liner script), but we'd like to have this whole thing look more personal. And by personal, I mean, we'd like to know who else is on other than us by UID or login name. The functionality (or relative merit) of this script depends heavily on the way in which users were created on your machine in the first place. If no "comment" (or "gecos") field is populated for users on your system, this command-chain is completely worthless.

For instance, assuming we had a user, with a login name of user1, logged in at the same time as us, with an entry that looked like this in /etc/passwd:

user2:x:32678:99::/home/user2:/bin/bash

and our /etc/passwd entry looked like this:

user1:x:32677:99:Mike The SysAdmin:/home/user1:/bin/bash

the command would pull no useful information for user2. So, if the output from w looked like this:

host # w
13:49:12 up 8 days, 3:41, 2 users, load average: 0.00, 0.14, 0.43
USER TTY LOGIN@ IDLE JCPU PCPU WHAT
user2 pts/0 08:44 5:03m 0.01s 0.01s -bash
user1 pts/1 09:05 0.00s 0.04s 0.00s w


our command would only produce output like this:

host # ./wdo.sh
Mike The SysAdmin


Whereas, if we added a comment field (perhaps via usermod -c "Bob The Guy Who Used To Be Anonymous" user2 or the chfn command), we would get nice output like the following:

host # ./wdo.sh
Mike The SysAdmin
Bob The Guy Who Used To Be Anonymous


and so forth, with users who had multiple sessions open being reported more than once. This script was written to give us a warm, fuzzy feeling, so seeing Bob's name five times is no big deal. If you want to use it for profiling purposes on a more heavily trafficed machine, there's always "sort."

Note, of course, that your version of Unix or Linux may produce slightly different output for the "w" command and xargs' -i flag may be "-I" (also, if, for some bizarre reason, your login names have spaces in them, just be sure to double quote the variable you define via xargs with the -i or -I flag. This should never happen). Basically, what I'm saying is, this script probably won't run correctly, exactly as it comes, on every single distribution of Linux and Unix out there :)

Enjoy the neighborhood watch :)

Cheers,

#!/bin/bash

#
# wdo.sh - More comments than code :)
#

w|awk '{print $1}' |xargs -ibing grep bing /etc/passwd|awk -F":" '{print $5}'



, Mike




Please note that this blog accepts comments via email only. See our Mission And Policy Statement for further details.

Excellent Web Hosting At An Affordable Price!
www.authorswebhosting.com
These Are The Guys I Use To Host My Online Presence!