View previous topic :: View next topic |
Author |
Message |
JloR n00b
Joined: 29 Jun 2004 Posts: 43 Location: Denmark
|
Posted: Fri Mar 11, 2005 5:04 pm Post subject: MOTD idea - need some help on it |
|
|
Well, I have this idea for a Message Of The Day :
I am running a server machine somewhere on the internet, I am not the only one on it but there are several other users that sometimes change username (su - <user>) on the shells..
I would like the MOTD appearing on login and on user-change and have the MOTD include some various information set up like this:
----------
Welcome back <user>, last time you logged in was: <time>
You logged in from: <host>
Your 10 last commands:
<10 last commands>
----------
I don't know if it can be done, but I think it would be a nice project to try to realize. Both for security and userfriendlyness.
Have anyone done something like this before? Can it be done?
Thanks in advance.
[EDIT]
Oh yes, and every user uses /bin/bash , so the last ten commands could be a tail of the .bash_history in their home dir.
And please only reply with questions of clarification and/or solutions.
[/EDIT] _________________ -- Jakob L. O. Rosenlund |
|
Back to top |
|
|
Nard` Apprentice
Joined: 23 Jan 2005 Posts: 250
|
Posted: Fri Mar 11, 2005 5:12 pm Post subject: |
|
|
I think what you need is /etc/bashrc
Perhaps something like this:
Code: |
cat /etc/motd # Static information, you may need to reconfigure/change filename so you don't get repeats of the motd when your not su'ing
source /etc/motd-interactive # A normal, bash script file
|
in /etc/motd you could have for instance "Welcome to <servername>"
in /etc/motd-interactive
Code: |
echo "Welcome back `whoami`, last time you logged in was: not implemented"
echo "You logged in from: not implemented"
echo "Your last 10 commands:"
tail -n 10 ~/.bash_history # Note the -n 10 can be ommitted as that's what tail defaults to
|
Hope that helps No idea how you get the other information, i'd think that would also depend on whether you use PAM or just plain ol shadow, but maybye not...
EDIT: changed to ~/.bash_history, my mistake _________________ Programming is the process of putting bugs in.
Debugging is the process of attempting to take bugs out, doing an ugly hack, hitting your computer, then claiming insurance.
Last edited by Nard` on Fri Mar 11, 2005 5:23 pm; edited 1 time in total |
|
Back to top |
|
|
JloR n00b
Joined: 29 Jun 2004 Posts: 43 Location: Denmark
|
Posted: Fri Mar 11, 2005 5:17 pm Post subject: |
|
|
That works exactly like it should - with the exception of the "logged in from <host>" and "last time you logged in was: <time>" parts :)
And the ~/.bashrc in the last line of last example should probably be ~/.bash_history (Just to clear up for future readers..)
Thanks a lot Nard, this looks like it might work :) _________________ -- Jakob L. O. Rosenlund |
|
Back to top |
|
|
Nard` Apprentice
Joined: 23 Jan 2005 Posts: 250
|
Posted: Fri Mar 11, 2005 5:33 pm Post subject: |
|
|
Figured out where it gets the information from, /var/log/wtmp. I'll see if I can figure out a way to format it, but that will proboably be up to you
EDIT: last command is useful. You still need to do extra formatting. grep and awk might be useful
EDIT: worth noting that i believe su does not change wtmp, so... you might have to give up on that
EDIT: *yet again* would just like to clarify when i said last command i meant the command called 'last' _________________ Programming is the process of putting bugs in.
Debugging is the process of attempting to take bugs out, doing an ugly hack, hitting your computer, then claiming insurance.
Last edited by Nard` on Fri Mar 11, 2005 5:53 pm; edited 1 time in total |
|
Back to top |
|
|
JloR n00b
Joined: 29 Jun 2004 Posts: 43 Location: Denmark
|
Posted: Fri Mar 11, 2005 5:40 pm Post subject: |
|
|
This is the output I have so far - using your method:
Code: |
Angel root # su dark
##########################################################
Welcome back dark, last time you logged in was: NOT IMPLEMENTED
You logged in from: NOT IMPLEMENTED
Your last 10 commands:
texi2pdf uge6.tex
texi2pdf uge7.tex
texi2pdf uge8.tex
texi2pdf uge9.tex
mv uge9.pdf ../public_html/
su -
su
ssh -l root *******dk
texi2pdf uge10.tex
mv uge10.pdf ../public_html/
##########################################################
Angel root $
|
Using this code in my ~/.bashrc file:
Code: |
# /etc/skel/.bashrc:
# $Header: /home/cvsroot/gentoo-src/rc-scripts/etc/skel/.bashrc,v 1.8 2003/02/28
15:45:35 azarah Exp $
# This file is sourced by all *interactive* bash shells on startup. This
# file *should generate no output* or it will break the scp and rcp commands.
# colors for ls, etc.
eval `dircolors -b /etc/DIR_COLORS`
alias d="ls --color"
alias ls="ls --color=auto"
alias ll="ls --color -l"
# Change the window title of X terminals
case $TERM in
xterm*|rxvt|Eterm|eterm)
PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/$
HOME/~}\007"'
;;
screen)
PROMPT_COMMAND='echo -ne "\033_${USER}@${HOSTNAME%%.*}:${PWD/$HO
ME/~}\033\\"'
;;
esac
##uncomment the following to activate bash-completion:
#[ -f /etc/profile.d/bash-completion ] && source /etc/profile.d/bash-completion
echo ""
echo ""
echo " ##########################################################"
echo "Welcome back `whoami`, last time you logged in was: NOT IMPLEMENTED"
echo "You logged in from: NOT IMPLEMENTED"
echo "Your last 10 commands:"
tail -n 10 ~/.bash_history
echo " ##########################################################"
echo ""
echo ""
|
Now all that is needed is to get the last logon time, and host from where they logged on... It must be here somewhere, I mean it is shown during login normally :) _________________ -- Jakob L. O. Rosenlund |
|
Back to top |
|
|
Nard` Apprentice
Joined: 23 Jan 2005 Posts: 250
|
Posted: Fri Mar 11, 2005 5:47 pm Post subject: |
|
|
erm, read my second post _________________ Programming is the process of putting bugs in.
Debugging is the process of attempting to take bugs out, doing an ugly hack, hitting your computer, then claiming insurance. |
|
Back to top |
|
|
BlackEdder Advocate
Joined: 26 Apr 2004 Posts: 2588 Location: Dutch enclave in Egham, UK
|
Posted: Fri Mar 11, 2005 5:50 pm Post subject: |
|
|
When you login with SSH you get the $SSH_CONNECTION which you can echo (ip from where you are connecting) if $SSH_CONNECTION is empty (which it will if you stay on one machine) $HOSTNAME gives the machinename ofcourse, so check if SSH_CONNECTION exists and otherwise show the HOSTNAME |
|
Back to top |
|
|
JloR n00b
Joined: 29 Jun 2004 Posts: 43 Location: Denmark
|
Posted: Fri Mar 11, 2005 5:50 pm Post subject: |
|
|
Nard` wrote: | erm, read my second post :P |
Oh, the edits :)
Well... For what it's worth - I guess I can take the info from the login then.. Normal login.
Just to know how to do it, now my curiousity has been sparked :)
[EDIT]
BlackEdder has a solution. Good idea actually.
[/EDIT] _________________ -- Jakob L. O. Rosenlund |
|
Back to top |
|
|
BlackEdder Advocate
Joined: 26 Apr 2004 Posts: 2588 Location: Dutch enclave in Egham, UK
|
Posted: Fri Mar 11, 2005 5:54 pm Post subject: |
|
|
Did some more testing and while $SSH_CONNECTION is kept when doing su <user> it is lost when doing su - <user> (logical actually ), so then you will need to use the command last (or do your own logdelving) |
|
Back to top |
|
|
JloR n00b
Joined: 29 Jun 2004 Posts: 43 Location: Denmark
|
Posted: Fri Mar 11, 2005 5:58 pm Post subject: |
|
|
Can you try to show it in bash code ? I'm not very good at that.. _________________ -- Jakob L. O. Rosenlund |
|
Back to top |
|
|
BlackEdder Advocate
Joined: 26 Apr 2004 Posts: 2588 Location: Dutch enclave in Egham, UK
|
Posted: Fri Mar 11, 2005 6:11 pm Post subject: |
|
|
my bash script skills are also limited and the problem with last is that you will still need to no which of the logged in ones is the current user.
Code: | last -n 2 -a
edwin pts/0 Fri Mar 11 18:01 still logged in blpc237.rhul.ac.uk
edwin pts/2 Fri Mar 11 10:48 still logged in blpc237.rhul.ac.uk
|
You could match pts/0 against the current user. you can get that info by running
This will still fail though if the number of users now logged in are bigger than the number you ask from last (with the -n variable).
But some awking and sed-ing should make this all doable, Just search some awk/sed examples on the forum |
|
Back to top |
|
|
JloR n00b
Joined: 29 Jun 2004 Posts: 43 Location: Denmark
|
Posted: Fri Mar 11, 2005 7:29 pm Post subject: |
|
|
I decided that if a user su'ed it be the same as saying he logged on from local machine.
So it's basically just:
Code: |
IF the $SSH_CONNECTION is set past first word
IF NOT then paste $HOSTNAME
|
And a friendly soul at #gentoo at EFnet constructed some code for me to do just that:
Code: |
if [ -n "$SSH_CONNECTION" ]; then a=`echo $SSH_CONNECTION|cut -d" " -f1|cut -d: -f4`; else a=$HOSTNAME; fi; echo $a
|
So I used it like this (code in /etc/motd-interactive ):
Code: |
echo ""
echo ""
echo " ##########################################################"
echo "Welcome back `whoami`, last time you logged in was: NOT IMPLEMENTED"
if [ -n "$SSH_CONNECTION" ]; then a=`echo $SSH_CONNECTION|cut -d" " -f1|cut -d: -f4`; else a=$HOSTNAME; fi;
echo "You are logged in from: $a"
echo "Your last 10 commands:"
tail -n 10 ~/.bash_history
echo " ##########################################################"
echo ""
echo ""
|
_________________ -- Jakob L. O. Rosenlund |
|
Back to top |
|
|
|