Go Back   HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials > Linux Forums > Programming/Scripts

Do you like HowtoForge? Please consider supporting us by becoming a subscriber.
Reply
 
Thread Tools Display Modes
  #1  
Old 29th February 2008, 08:42
breakaway breakaway is offline
Junior Member
 
Join Date: Jun 2007
Posts: 21
Thanks: 2
Thanked 1 Time in 1 Post
Exclamation Bash script to boot ssh users who log in more than once

Hello,

I am currently writing a bash script that checks if a user has logged in more than once, by checking the output of 'ps x' The output is processed so only two columns of data are available - the PID and the username. If the script finds that two users (with the same username) are logged in, both the users PIDs should be passed to the 'kill' command so they both get disconnceted. I'm slowly getting there:

Code:
#!/bin/bash

declare users=`ps x | grep priv | tr -s " " | cut -f 1,6 -d ' '`

# Main Loop
# if [ $user1 == $user2 ]
#       then echo "Match found, disconnecting user '$user1'"
# fi

echo $users

# Quit cleanly
exit
Right now, the script just prints out the current users and their PIDs. Unfortunately I don't know how to go any further due to my lack of experience / knowledge. Any help on how to compare every username to every other username, and extract PIDs if/when matches are found would be highly appreciated.

PS I am aware that there is a way to limit concurrent logins, and this is not what I want. I want both users to be D/Cd.

Last edited by breakaway; 29th February 2008 at 08:45.
Reply With Quote
Sponsored Links
  #2  
Old 29th February 2008, 09:13
topdog topdog is offline
Senior Member
 
Join Date: Jan 2008
Location: South Africa
Posts: 1,352
Thanks: 0
Thanked 148 Times in 145 Posts
Default

have you tried
Code:
if [ "$user1" = "$user2" ]; then
But that will match "" as well, you possibly need to check if the $user1 variable is not empty first.
__________________
----
http://www.topdog.za.net - Got Linux problems ? - I can help.
http://www.baruwa.org - Try it.
Reply With Quote
  #3  
Old 29th February 2008, 10:19
breakaway breakaway is offline
Junior Member
 
Join Date: Jun 2007
Posts: 21
Thanks: 2
Thanked 1 Time in 1 Post
Default

Hello,

No I have not tried that, becuase

I don't know how to actually read the users / PIDs into a variable properly to begin with.
Reply With Quote
  #4  
Old 29th February 2008, 10:44
topdog topdog is offline
Senior Member
 
Join Date: Jan 2008
Location: South Africa
Posts: 1,352
Thanks: 0
Thanked 148 Times in 145 Posts
Default

Code:
pids=$(ps ax| grep sshd | grep "@pts/" | awk '{ print $1 }')
users=$(ps ax| grep sshd | grep "@pts/" | awk '{ print $6 }' | sed -r "s/@pts\/[0-9]?//")
YMMV
__________________
----
http://www.topdog.za.net - Got Linux problems ? - I can help.
http://www.baruwa.org - Try it.
Reply With Quote
  #5  
Old 29th February 2008, 11:07
breakaway breakaway is offline
Junior Member
 
Join Date: Jun 2007
Posts: 21
Thanks: 2
Thanked 1 Time in 1 Post
Default

Hello topdog, thanks for the reply. I've already figured out how to actually pull out the usernames and PIDs.

pids=$`ps x | grep priv | tr -s " " | cut -f 1 -d ' `
users=$`ps x | grep priv | tr -s " " | cut -f 6 -d ' '`

My problem is with actually processing them. I am unfamiliar with how to write loops etc.

TIA!
Reply With Quote
  #6  
Old 29th February 2008, 11:53
topdog topdog is offline
Senior Member
 
Join Date: Jan 2008
Location: South Africa
Posts: 1,352
Thanks: 0
Thanked 148 Times in 145 Posts
Default

Code:
#!/bin/bash
declare -a array1
declare -a array2
users=$(ps x | grep priv | tr -s " " | cut -f 6 -d ' ')
pids=$(ps x | grep priv | tr -s " " | cut -f 1 -d ' ')
array1=($users)
array2=($pids)

for i in $(seq 0 $((${#array1[*]} - 1))); do
        testvalue=${array1[$i]}
        for x in $(seq 0 $((${#array1[*]} - 1))); do
                if [ "$x" = "$testvalue" ]; then
                        kill -9 ${#array2[$i]}
                fi
        done
done
__________________
----
http://www.topdog.za.net - Got Linux problems ? - I can help.
http://www.baruwa.org - Try it.
Reply With Quote
  #7  
Old 29th February 2008, 11:58
breakaway breakaway is offline
Junior Member
 
Join Date: Jun 2007
Posts: 21
Thanks: 2
Thanked 1 Time in 1 Post
Default

Thanks topdog! Unfortunately it appears this script is not running properly :\ I have several users logged in like so:

416 breakaway
30459 breakaway
30490 breakaway
30521 vent
30553 devon
32743 xerxes

But it didn't kill the 3 'breakaway' users :\
Reply With Quote
  #8  
Old 29th February 2008, 12:00
topdog topdog is offline
Senior Member
 
Join Date: Jan 2008
Location: South Africa
Posts: 1,352
Thanks: 0
Thanked 148 Times in 145 Posts
Default

Okay i did not test, but i have just checked it seems like your commands to check the users does not work.
__________________
----
http://www.topdog.za.net - Got Linux problems ? - I can help.
http://www.baruwa.org - Try it.
Reply With Quote
  #9  
Old 29th February 2008, 12:06
topdog topdog is offline
Senior Member
 
Join Date: Jan 2008
Location: South Africa
Posts: 1,352
Thanks: 0
Thanked 148 Times in 145 Posts
Default

Code:
#!/bin/bash
declare -a array1
declare -a array2
pids=$(ps ax| grep sshd | grep "@pts/" | awk '{ print $1 }')
users=$(ps ax| grep sshd | grep "@pts/" | awk '{ print $6 }' | sed -r "s/@pts\/[0-9]?//")
array1=($users)
array2=($pids)

for i in $(seq 0 $((${#array1[*]} - 1))); do
        testvalue=${array1[$i]}
        for x in $(seq 0 $((${#array1[*]} - 1))); do
                intestvalue = ${array1[$x]}
                if [ "$intestvalue" = "$testvalue" ]; then
                        kill -9 ${#array2[$i]}
                fi
        done
done
__________________
----
http://www.topdog.za.net - Got Linux problems ? - I can help.
http://www.baruwa.org - Try it.
Reply With Quote
  #10  
Old 29th February 2008, 12:07
topdog topdog is offline
Senior Member
 
Join Date: Jan 2008
Location: South Africa
Posts: 1,352
Thanks: 0
Thanked 148 Times in 145 Posts
 
Default

Sorry that has a bug it could kick out all the users let me perfect it first.
__________________
----
http://www.topdog.za.net - Got Linux problems ? - I can help.
http://www.baruwa.org - Try it.
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Daily mail logrotation?! schmidse Installation/Configuration 4 21st January 2008 13:55
https protocol stops page load tjd General 7 18th November 2007 18:06
Outlook does not Authenticates aberrio Server Operation 30 1st November 2007 19:21
No ftp login for ispconfig-webuser agri Installation/Configuration 12 19th March 2007 10:06
Junk mail and spamassassin... sthompson Installation/Configuration 4 27th December 2006 16:11


All times are GMT +2. The time now is 09:47.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.