Operating System - Linux
1756161 Members
3514 Online
108842 Solutions
New Discussion юеВ

Problem with deleting utmp entries in script

 
SOLVED
Go to solution
Douglas James Cameron
Occasional Advisor

Problem with deleting utmp entries in script

Hi,

I have a script which deletes PIDs of a specified user, but it is not deleting the entries in the utmp file ... any ideas?
Here's the script:

#!/bin/ksh
PATH=/usr/local/bin:/usr/bin:/bin

kill_sessions()
{
ps -ef | grep "$username" | sort | awk '{ print $2 }' >> killfile
for i in `cat killfile`
do
kill "$i"
done
}

erase_entries()
{
/usr/sbin/acct/fwtmp < /etc/utmp > /tmp/utmp.out
ls -l /tmp/utmp.out
cat /tmp/utmp.out |
sed -e 's/"$username"//g' > /tmp/new_utmp.out
/usr/sbin/acct/fwtmp -ic < /tmp/new_utmp.out > /etc/utmp
}

while (true) do
echo ""
echo ""
echo "Do you wish to unhang a user session ( Please type Yes or No) ? \c"
read choice
case "$choice" in
"Yes") echo "Okay"
echo "Server? \c"
read server
echo "Username? \c"
read username
kill_sessions
erase_entries
;;
"No") echo "Exiting program."
PATH=$HOME
exit 1
;;
*) echo "Please type Yes or No at the prompt"
;;
esac
done
PATH=$HOME

Thanks in advance.
5 REPLIES 5
A. Clay Stephenson
Acclaimed Contributor

Re: Problem with deleting utmp entries in script

Hi,

I am intentionally not going to fix your problem (because that is really not doing you a favor) but I will point out your fundamental error:

's/"$username"//g'

NOTE: You are expecting variable instantiation to take place inside single quotes --- and that ain't gonna happen.

Now, a better question is why you are doing this? At best, who and utmp/wtmp represent a "best guess" of who is on the system. A far better and more trustworthy resource is the process table (ps -e).
If it ain't broke, I can fix that.
Douglas James Cameron
Occasional Advisor

Re: Problem with deleting utmp entries in script

Clay - you replied:
Hi,

I am intentionally not going to fix your problem (because that is really not doing you a favor) but I will point out your fundamental error:

's/"$username"//g'

NOTE: You are expecting variable instantiation to take place inside single quotes --- and that ain't gonna happen.

Now, a better question is why you are doing this? At best, who and utmp/wtmp represent a "best guess" of who is on the system. A far better and more trustworthy resource is the process table (ps -e).


Hey Clay,
The reason I am doing this is to 'unhang' user sessions. I am also making another error, the uer with multiple sessions is to have all but the first removed from the process table and all but the first entry removed from utmp. I can't go into more detail than that, but it is on the up and up.
Now, one approach I could take is to use awk and manipulate the killfile with the getline function to be able to skip the first process. The only reason I used sed was to try and 'null' the username entries from the utmp file, in effect logging them out of the system.
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Problem with deleting utmp entries in script

I wasn't questioning the ethics of your task but rather the wisdom of ever depending upon the contents of utmp for anything. Again, it represents a "best guess" and this applies to all flavors of UNIX. In any event, you had a logical error and I thought I had left you with a big enough clue to fix it (although even if it was working I'll bet it wouldn't do what you want it to do).

Note this line:
's/"$username"//g'
when the shell hands off this line to sed
"$username" is literally just that: "$username" --- when you probably meant for $username to be "joe".

The fix is to do away with the single quotes:

username="Joe"

sed "s/${username}//g" < infile > outfile

This will replace every occurence of "Joe" with a null string. What you probably intended was deleting the line when the username was found.

sed "/${username}/d" < infile > outfile
If it ain't broke, I can fix that.
Douglas James Cameron
Occasional Advisor

Re: Problem with deleting utmp entries in script

This may not appear as a reply to you Clay, but I'll eventually get the hang of the forums here.

Thanks Clay .. I was missing a few things on the sed statement there.

You actually helped me quite a bit (even though I know you're right, it would have helped more if I figured it out myself, from your clue) .. anyway thanks.

Now all I have to do is figure out how to preserve the first entry in the process table and the utmp (don't ask me why) file..

You should've received some points on that .. let me know if they didn't get added on.
Douglas James Cameron
Occasional Advisor

Re: Problem with deleting utmp entries in script

Thanks again Clay, for your advice on the script.