LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   user list (https://www.linuxquestions.org/questions/linux-newbie-8/user-list-448495/)

RagedPenguin 05-25-2006 01:28 PM

user list
 
How do you send out an email in 'reverse order' of all the users currently logged into a Linux server, while storing the command in a background process?

I'm using standard telnet + putty, and I'm definately a linux newb lol

Thanks for any help. I've been reading my guide to linux book, but I'm still confused.

jeelliso 05-25-2006 02:11 PM

I thought there was a command you could use to send a message to all current users, but I can't seem to find it.

I would probably just write a small shell script with a for loop. For each line of
Code:

who
(which shows users logged on) it could use the mail command to send an email to them. Just start from the back of the list in your for loop to do it in reverse order.

If you've ever written a shell script before, I would suggest looking over a small tutorial. It wouldn't do you any good if I just told you the answer, as it would be a pretty simple script.

Good Luck,

~Justin

MensaWater 05-25-2006 02:17 PM

What do you mean by "reverse order". Alphabetically? Time of login?

Short and simple:

for USER in `who |awk '{print $1}' |sort -u`
do echo mailtextfile | mail -s "Mail from me" $USER
done

In the above USER is a variable name you picked (you could call it MUD or anything else that appeals to you). $USER is a reference back to the same variable (so if you use MUD you need to use $MUD).

The who command shows all logged in users.

The awk command says to only print the first field of the who output (this is the user name).

The sort -u tells it to list each user only once (in case they have multiple logins).

mailtextfile is abitrary - it is any filename you want (mudtext for example). In that file you put the text you wish to email.

The item in quotes after mail -s is the subject you want.

Finally $USER (or $MUD etc...) is the user name to send the mail to.

Note that the above will send the mail to the user's Linux/Unix mail box. If you've set up forwarding and aliases in sendmail for all users it will be handled by that. If you haven't and your users don't typically read their Linux/Unix mail they're unlikely to see the mail.

If you have user accounts on your Linux server that are the same as those used in your external mail then you modify the $USER to be ${USER}.domain.top. (You need the brackets so it will know that USER is the variable rather than USER.domain.top.) The domain.top is whatever your user email accounts use as top level and local domain (example yahoo.com, apache.org etc...).

MensaWater 05-25-2006 02:20 PM

By the way if you're just trying to send a message to everyone currently logged in you can use the "wall" command. Just type "wall". Input the message you want to send then hit Ctrl-D to end the message. It will appear on the screen of everyone logged in. (Note that some people get very annoyed by that however - usually it is done when you need to take the system down.)

Note that "wall" does NOT send an email but rather writes directly to the users' terminals.

theNbomr 05-25-2006 02:22 PM

What do you mean 'reverse order'?

Logins seem to be an unordered list. I guess you could sort by UID, Username, login time/date, color of socks, process ID's....

But there's more. Sending e-mails has no guarantee that they will ultimatley be sent or received in any particular order, unless you actually wait to see that a sent e-mail was received before sending the next one.

Maybe you should explain what you really want to do.

--- rod.

RagedPenguin 05-25-2006 03:19 PM

thank you for very much for the help so far guys. Yes sorry for not elaborating, reversed order is supposed to be by alphabetical order of usernames on the network and to send that list in a standard email.

RagedPenguin 05-25-2006 05:09 PM

yeah and it isn't supposed to be in a script, just a basic command.
I managed to do the command that I needed for the process, thanks so much for all your help. I thought it was a lot more complicated then it turned out to be lol.
Next time I'll try to be more clear, sorry for the confusion.


All times are GMT -5. The time now is 03:55 AM.