Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I’m trying to send user login logs to my email with the users name and location. After making the script I found that environment variables can’t run in this script, I’m hoping that someone out there can help me get this working even if it’s a security problem. Thanks for your time

/etc/update-motd.d/99-footer

ssmtp -oi me@gmail.com << EOF
To: me@gmail.com
Subject: Login

$SUDO_USER" from "$SSH_CLIENT

EOF

email output is

Subject: Login  
 from
share|improve this question
    
$SUDO_USER" from "$SSH_CLIENT is somewhat strange syntax. try to use "$SUDO_USER from $SSH_CLIENT" or even better ${SUDO_USER} etc. But this woh't help, I guess - where are the environment variables SUDO_USER and SSH_CLIENT defined? –  ohno May 27 '12 at 18:25
    
idk where the environment variables are defined –  Alexander May 27 '12 at 19:37
    
Type "set" and you will get a list of all the current variables. If you want something to persist put it in ~/.bashrc or ~/.bash_profile –  virpara May 28 '12 at 5:37
    
i give it a try but no luck. thanks for trying virpara –  Alexander May 29 '12 at 6:11

2 Answers 2

As an experiment, I added an env command (equivalent to printenv) to the end of /etc/update-motd.d/99-footer on my Ubuntu 12.04 system.

When I logged in again, the following lines were added to the end of my /etc/motd:

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PWD=/

So $PATH and $PWD are the only environment variables that are set when 99-footer is running. That's why $SUDO_USER and $SSH_CLIENT aren't being expanded.

You need to send that e-mail from somewhere else, not from the process that updates /etc/motd.

share|improve this answer

If you change the script to:

echo "To: me@gmail.com
Subject: Login

$SUDO_USER from $SSH_CLIENT

" | ssmtp -oi me@gmail.com

Note use of double quotes. Variables should then be substituted.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.