PDA

View Full Version : Bash script to boot ssh users who log in more than once


breakaway
29th February 2008, 09:42
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:


#!/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.

topdog
29th February 2008, 10:13
have you tried
if [ "$user1" = "$user2" ]; then

But that will match "" as well, you possibly need to check if the $user1 variable is not empty first.

breakaway
29th February 2008, 11:19
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.

topdog
29th February 2008, 11:44
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

breakaway
29th February 2008, 12:07
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!

topdog
29th February 2008, 12:53
#!/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

breakaway
29th February 2008, 12:58
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 :\

topdog
29th February 2008, 13:00
Okay i did not test, but i have just checked it seems like your commands to check the users does not work.

topdog
29th February 2008, 13:06
#!/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

topdog
29th February 2008, 13:07
Sorry that has a bug it could kick out all the users let me perfect it first.

topdog
29th February 2008, 13:17
Please test before use in a production environment.

#!/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]}
count=0
for x in $(seq 0 $((${#array1 } - 1))); do
intestvalue = ${array1[$x]}
if [ "$intestvalue" = "$testvalue" ]; then
let count=count+1
#check if the name has appeared more than one and kill
if [ $count -gt 1 ]; then
kill -9 ${#array2[$i]}
fi
fi
done
done