LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-04-2005, 07:49 AM   #1
rubby1
LQ Newbie
 
Registered: Oct 2005
Posts: 5

Rep: Reputation: 0
executing a script when logging via ssh


hello,

i'm new here =)

ok, i woold like execute a script (sh or php) when a user log in with ssh (the goal is to send an email.

this script wolld be executed just after the validation of login / password.
i searched inside the ssfd conf, but nothing looks like that.

what's the way please ?

regards
and thx in advance
Rubby
 
Old 10-04-2005, 09:20 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
this script wolld be executed just after the validation of login / password.
If your distro uses PAM (got dir and files in /etc/pam.d) you prolly can use something like the pam_script module.
 
Old 10-04-2005, 09:44 AM   #3
rubby1
LQ Newbie
 
Registered: Oct 2005
Posts: 5

Original Poster
Rep: Reputation: 0
yep there is pam, and there is also a config for sshd, so what can i do ? :!

here is the content :
auth required /lib/security/pam_stack.so service=system-auth
auth required /lib/security/pam_nologin.so
account required /lib/security/pam_stack.so service=system-auth
password required /lib/security/pam_stack.so service=system-auth
session required /lib/security/pam_stack.so service=system-auth
session required /lib/security/pam_limits.so
session optional /lib/security/pam_console.so


do you think i can add this line ???
session required /home/myscript.sh (or .php) service=system-auth

???

rubby
 
Old 10-04-2005, 03:59 PM   #4
clb
Member
 
Registered: Sep 2004
Location: UK
Distribution: Ubuntu
Posts: 117

Rep: Reputation: 16
If they are using the BASH shell, add the following line to their /home/user/.bash_profile file:

/path/to/executable/script.php

or

sh /path/to/script

Both shell scripts and php scripts can be made executable - and PHP could be easier to send mail from. If you are planning to do it for all users, you will have to edit each users .bash_profile aswell as the/etc/skel/.bash_profile

/etc/skel is a directory that is used to create all new user dirs from - if its in the /etc/skel directory then it will be in the new users directory. This applies to the contents of files in /etc/skell
 
Old 10-04-2005, 07:11 PM   #5
rubby1
LQ Newbie
 
Registered: Oct 2005
Posts: 5

Original Poster
Rep: Reputation: 0
hi,

ok thx for your answer =)

do you think i can do the same with pam ?

cr
 
Old 10-05-2005, 02:31 PM   #6
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
do you think i can do the same with pam ?
Should go something like this.
1. Install pam_script.so if you haven't got it in /lib/security/
2. Add this line in /etc/pam.d/sshd under the other "session" statements:
Code:
session  required pam_script.so onsessionopen="/etc/security/ssh_mail.sh"
3. Now edit your script "/etc/security/ssh_mail.sh" and make executable:
Code:
#!/bin/sh
# 1st arg by pam_script: username, 2nd arg: service
_user="$1"; _svc="$2"
getent passwd "${_user}" 2>&1>/dev/null; case "$?" in 0) echo -en "Hello ${_user},\n\nWelcome to ${HOSTNAME}\
\nHave phun\!\nThe owner\n"|mail -s "Welcome ${_user}" "${_user}@${HOSTNAME};; *) logger "pam_script [FATAL]\
 running script for ${_user}@${_svc}";; esac; exit 0
* Test...
 
Old 10-05-2005, 05:29 PM   #7
rubby1
LQ Newbie
 
Registered: Oct 2005
Posts: 5

Original Poster
Rep: Reputation: 0
hehe ok thx !

if somethink is wrong, can it crash sshd ???? (it is a distant server =) so if no more sshd, no more server =)

CR
 
Old 10-06-2005, 09:51 AM   #8
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
I tested pam_script (v0.4 I think) and ...

OK, using mailx package is nice, but you really want to forge either a no-return@ address or a general address like "info@". To do this I pipe the message (Subject: and body) tru sendmail using -F and -f. Side effect from this is you will need a privileged (non-root account) user to run the script as (runas=) , else you'll have an X-Auth warning header saying the luser faked the From: address. However I set the permissions on the runnable script and whatever user I use, it ain't working. Besides that logging out of a ssh session seems to halt at "logout", and the /etc/login.defs fix (CLOSE_SESSION) doesn't work as advertised either.

Long story short, pam_script doesn't work as I want it to work, so forget it.
 
Old 10-06-2005, 03:12 PM   #9
rubby1
LQ Newbie
 
Registered: Oct 2005
Posts: 5

Original Poster
Rep: Reputation: 0
ok =)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SSH] Issue logging in [SSH & Permissions] MD3 Linux - Networking 11 12-10-2006 09:25 AM
logging on without executing bashprofile aru_04 Programming 2 06-11-2005 05:34 AM
.bash_profile (hence .bashrc) not executing with ssh arobinson74 Linux - General 1 10-31-2004 09:55 AM
executing multiple commands by ssh jpan Linux - General 1 10-22-2004 02:12 PM
Ssh or logging from shell script Alek Linux - General 5 09-24-2003 10:30 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 02:29 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration