Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need a script

Status
Not open for further replies.

vti

Technical User
Feb 26, 2001
189
TR


Hi
I would like to have a script to deny second of users sessions.I want to users to have only one telnet session to my system.
I ll write down step by step what i want;
- when a user attempt to logon ,write user name to a user list file.
- when user wants to get exit ,remove that username from the user list file.
--- But if a user has already login and wants to login as second session script will searching for the user name in user list file,if user name in the user list file he/she gonna have message and get exit.

I am thinking to put this script to users profiles .

Thanks for any helps.
 
unixadmin:

Myself, I see no reason to use a log file. You can do something like this in the users profile or /etc/profile:

#!/bin/ksh

# tested under Solaris 7
uname=$(who am i| awk ' { print $1 } ')
if who |grep "^$uname" > /dev/null
then
echo "found $uname"
exit 1
fi
# end script

If you decide to use a log file, you'll have to decide how to handle abnormal terminations.

Also, the 'who am i' command is notoriously non-portable. On some systems it's whoami.

Regards,

Ed

 
hi olded
I think it's a perfect way to do it without log file.
uname=$(who am i| awk ' { print $1 } ') *didn't work on Tru64 and i have deleted this line and changed the script as ;

if who |grep "^$USER" > /dev/null
then
echo "found $USER"
exit 1
fi

and it work well.But only the problem is it catch tje user at first login .I want to terminate second session ,not first one.Do you have any more idea about it.?Maybe we can add counter to find username count,and if it's more than one go exit.But i have to also kill second session because user ,not first one.

Thanks so much for your help.
 
unixadmin:

Sorry about that. How about using -c to get a count of the number of time USER is logged in:

if [ $(who |grep -c "^$USER") -gt 1 ]
then
echo "found more than 1 $USER"
exit 1
fi

I've never used tru64, but the above should work. If it doesn't you might try setting a variable before the if test:

xcnt=$(who |grep -c "^$USER)

if [ xcnt -gt 1 ]
then
echo "found more than 1 $USER"
exit 1
fi

Regards,


Ed
 
hi olded ,
Thanks so much for your help it works perfect with ksh.
There is only problem is when i grep for a user this scripts finds all of users who beginig with sam name, for example

Name which i grep for john;

But on system there is 4 users start with john or has "john" in their name .

john
johnb
johnc
johnde

How can i grep for exact word i am looking for.( "john" ) ,i think i am gonna need a parameter of grep,i have tried with manual pages but couldn't find.

thanks.
 
unixadmin:

Good Point: How about using a regular expression stating a field needs to begin and end with the exact username:

if [ $(who |grep -c &quot;^$USER$&quot;) -gt 1 ] # <- not the $USERS$

If you want to, you can do something like this:

for name in `who`
do
if [ `echo $name|awk ' { print $1 } '` = &quot;$USER&quot; ]
then
echo &quot;found more than 1 $USER&quot;
exit 1
fi
done

You're stripping off the first field and looking just at that. No need for regular expressions. I think this is more inefficient.

 
olded
I am sorry but &quot;&quot;if [ $(who |grep -c &quot;^$USER$&quot;) -gt 1 ]&quot;&quot; doesn't work.The other scripts works which has do-while but i prefere to work with grep command,because on my system 1500 users get login at same time and the system is works on low performance if i put do-while or ps commands in profiles.Therefore i gotta find out grep fuctions.
I will try to write forum as new question ,thanks for your help.

Best regards.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top