Log Out SSH User in Bash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Log Out SSH User in Bash
# 1  
Old 05-30-2013
Wrench Log Out SSH User in Bash

So, I've been writing a system to allow users temporary access onto a system.

Essentially, there's a web server with a PHP script, the PHP script takes a Username & Password from a webform, and passes it to a script, createusr.sh.

The script looks something like this:

Code:
pass=$(perl -e 'print crypt($ARGV[0], "password")' $2)
sudo /usr/sbin/useradd $1 -s /bin/bash -p $pass -d /home/onlineusers/$1 -m
sudo /bin/chmod 700 /home/onlineusers/$1
sleep 1800
sudo /usr/sbin/userdel -f $1
sudo /bin/rm -rf /home/onlineusers/$1

Basically it creates a user, using the passed parameters, and force creates their home directory. It then makes that directory only accessible to said user, before sleeping for half an hour.

After that time it deletes the user and their home directory. But I have one problem. If the user is still logged on at that point, then the user deletion has no effect, as the user can still work.

At the moment, I am thinking of using:

Code:
pgrep -t <user's tty>

and then killing the bash, but I'm not sure how I can get the TTY of a user logged in through SSH?

Maybe somehow "grep" with "w" might do it, but not if the username contains something like "load" which is featured elsewhere in the output from "w".

Besides this, is there any way that I can send a message to a logged in user, saying something like "5 minutes left"?

Code:
write

seems like a possibility, but I'm not sure exactly how this would be implemented.

Last edited by FreddoT; 05-30-2013 at 09:27 AM..
# 2  
Old 05-30-2013
Not sure I understand the problem in it's entirety. Try
Code:
USERTTY=$(who | awk '$0 ~ user {print $2}' user=$1)
write $1 $USERTTY

# 3  
Old 05-30-2013
Thanks!

Thankyou so much! Helped loads!

I ended up with:

Code:
pass=$(perl -e 'print crypt($ARGV[0], "password")' $2)
sudo /usr/sbin/useradd $1 -s /bin/bash -p $pass -d /home/onlineusers/$1 -m -g onlineusers
sudo /bin/chmod 700 /home/onlineusers/$1
echo -e "\n"
sleep 1800
USERTTY=$(who | awk '$0 ~ user {print $2}' user=$1)
BASHPROC=$(pgrep -t $USERTTY | sed -n 1p)
kill -HUP $BASHPROC
sudo /usr/sbin/userdel -f $1
sudo /bin/rm -rf /home/onlineusers/$1

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash scripts - CGI and ssh

Hi Everyone, I started looking at the possibility of making some of our bash scripts available through a web server using CGI and the simple ones works just fine. Now I need to execute remote commands using ssh but can't really get it to work. I got private keys all sorted. Must be ssh... (1 Reply)
Discussion started by: arizah
1 Replies

2. Shell Programming and Scripting

Help needed on ssh in bash scripting

HI I have the following requirement I have a script a.sh which will deploy files in multiple servers .The argument for the a.sh is abc.gz host1.conf where abc.gz is a zip file and one.conf will contain all the database connection string . Now I have to write a b.sh which will... (7 Replies)
Discussion started by: harry00514
7 Replies

3. Shell Programming and Scripting

Need some help in bash scripting with ssh

Hi @ all I have the following scenario: As Admin of a cupple of servers I tried to write the following script to figure out, if the machine is up and available and if some directory´s were available. But my script is having some probs, while running. Maybe some of you have a better way to... (9 Replies)
Discussion started by: muogli
9 Replies

4. AIX

passwordless entry using ssh from one user to a different user on the same server

Hi, We have a requirement to do passwordless entry from one user to a different user on the same AIX server using ssh keys. Can some one help me with this? Thanks in advance, Panditt (3 Replies)
Discussion started by: deshaipet
3 Replies

5. Red Hat

Cannot ssh for a user

This is the entry when I tail /var/log/secure when I ssh for user "nightly"... Aug 4 03:19:48 itanium2 sshd: Illegal user nightly from ::ffff:10.91.220.35 Aug 4 03:20:10 itanium2 sshd: Failed password for illegal user nightly from ::ffff:10.91.220.35 port 32862 ssh2 What could be... (3 Replies)
Discussion started by: kirtikjr
3 Replies

6. Shell Programming and Scripting

Help on an ssh bash script...

Hey Guys, I want to have a bash script on my computer (Mac OS X 10.6.8) that can ssh into my iPod and respring. I know how do this by typing in "ssh root@10.0.1.10" and then typing in the password "alpine". From there i simply type "respring". I want to possibly put this into a shell script so it... (0 Replies)
Discussion started by: jetstream131
0 Replies

7. Shell Programming and Scripting

Bash commands to an 'ssh' within an ssh'

I've struggled to find a solution to this problem from searching so I thought I'd write a post to see what can be done. I'm attempting to connect and run commands on 'server2' but because of security limitations I cannot access it directly. I can however ssh into 'server1' and then into... (7 Replies)
Discussion started by: mcintosh.jamie
7 Replies

8. UNIX for Dummies Questions & Answers

change user> to user@host> ssh prompt

Hi, I was wondering how to change the prompt for my ssh login. At the moment it is like user> while I'd like it to be as user@host> It is in the .bash_profile or .ssh ??? Thanks (2 Replies)
Discussion started by: pmasterkim
2 Replies

9. Shell Programming and Scripting

BASH ssh login

Ok, there's been a good number of posts about this, but here goes. I want a script to log in to a system via ssh without using keys. This will be used to log in to Cisco IOS devices. I have tried the following, but could not get it to work: SSH login expect shell script to supply username and... (1 Reply)
Discussion started by: mike909
1 Replies

10. Shell Programming and Scripting

bash script for ssh login-

hi. I need a bash script which can login to an other mashin via SSH and then run some commands and then return the result to my mashine. I dont know where to begin, I think first I will need a ssh connection, dont know how to make it, then , do I need a ftp connection between the 2 mashins to... (5 Replies)
Discussion started by: big_pil
5 Replies
Login or Register to Ask a Question