Shell Script for User AUTH Help

#!/usr/bin/ksh
/bin/clear

LIST_USERS="user1|user2|user"

echo "Please enter the PASSWORD:"
stty -echo
read PASSWORD
stty echo

if [ "$PASSWORD" = = "$LIST_USERS" ]; then
        echo "You have access!"
else
        echo "ACCESS DENIED!"
exit
fi

This above script is not working when I auth more then one user w/in script - any help here appreciated - I shall summarize.

I could be wrong but I believe you need to have each user quoted separately. I'm just looking at it and it looks like LIST_USERS should be

LIST_USERS="user1"|"user2"|"user"

Chances are I'm wrong tho...Just looks like quoting it your way quotes that entire string as one string of user1|user2|user and not user1, user2,user etc.

Also I believe you need only one "=" to test strings. So:

 if [ "$PASSWORD" = "$LIST_USERS" ]; then

Nope I tried that - that doesn't work too.

Question, why would the username (I'm assuming user1,user2,and user are usernames) equal the password?

/bin/clear
echo "Please enter the PASSWORD:"
stty -echo
read PASSWORD
stty echo
LIST_USERS="user1"|"user2"|"user"
for i in $(echo "$List" | sed "s/|/ /g");do
 if [[ "$PASSWORD" == "$i" ]];then
echo "Fine!"
else
echo "Not Good!"
exit
fi
done

Even this w/o any good results.

Your for loop is looking for $List, where's $List? Don't you mean $LIST_USERS?

but again i'm curious why your password variable is equal to your user variable. Do your users have the same pass as their username? also, string comparisons are done with one equals sign [ $PASSWORD = $i ]

---------- Post updated at 11:33 PM ---------- Previous update was at 11:19 PM ----------

 #!/bin/sh
/bin/clear
echo "What's your user name?"
 read usrName
usrPass=""
case $usrName in
         user1) usrPass="pass1"
         break
;;
         user2) usrPass="pass2"
         break
;;
         user3) usrPass="pass3"
         break
;;
         *) echo "Invalid username"
         exit 1
;;
esac
echo "What's your password?"
read usrPwdin
[ $usrPass != $usrPwdin ] && echo "Incorrect password" && exit 1
echo "Password accepted"
# do stuff here...

I'm still learning myself, but I whipped this up for you. Maybe it can help...

This is alomost done my requirement but, still not 100% - first of all thanks so much.

my requirement is to supply set os usernames who can excute the script against the passwd file or texting in the script itself. What do you say?

I'm sorry I don't quite understand what you're asking. Does your script need to ask for a user name? Please clarify...

and how do i do it in real time usernames and user's password? please advise.

---------- Post updated at 01:58 AM ---------- Previous update was at 01:56 AM ----------

yes please it should ask me for either usernames and type is their password which is the OS/UNIX password to type in and proceed.

So you're wanting to authenticate with the users login and pass that's on the machine they're running it from? Why? Why not just make sure they are a certain user like:

[`whoami` = "root" ] && echo "I am root"
[`whoami` = "shiv2001in" ] && echo "I am unix.com user shiv2001in"

In other words, your script can test to see who the user is. You can say:

[ ! `whoami` = "user1" -o "user2" -o "user" ] && echo "You are not authorized to run this script"

because I want only certain users in the admin group to run - once they supply their real/won password the script should continue else should exit out.

Yea see I personally wouldn't want to be "logging in" to your script with my username and password. Leaves a lot to be desired as far as security. If you allow the user to login to the shell, and then test to make sure the user is in fact "user1" "user2" "user" etc, you can make the script exit if they are not on the list.

correct - but, in my case we have lots of admins and I want to allow certain admins only - $who -m|awk '{print $1}' <- if they are these admins only I will allow - becase whoami says root and many admins can sudo to root and they can become root and I do not want those admins or all admins to run this script and want only those who are in my admin group to run this. Hope I'm not making too complicate.

[ ! `whoami` = "user1" -o "user2" -o "user" ] && echo "You are not authorized to run this script"

This says if you're NOT user1 or user2 or user to echo "You are not authorized to run this script" which can be followed by && exit 1 to make it exit the script immediately if they are not one of those users

[ ! `whoami` = "user1" -o "user2" -o "user" ] && echo "You are not authorized to run this script" && exit 1

If your setup has `whoami` use it instead of going through `who -m | awk '{print $1}'` does the same thing. Save yourself some typing...

[ ! "who -m|awk '{print $1}'" = "user1" -o "user2" -o "user3" ] && echo "You are not authorized to run this script" && exit 1

is this correct ?

You have to use backticks ` to surround the who -m command. Backticks are right next to the number 1 on my laptop's keyboard. Should be for your computer too. Looks like a ' but its not. So:

 [ `who -m | awk '{print $1}'` != "user1" -o "user2" -o "user3" ] && echo "you are not..." && exit 1 

Forgive me, the ! goes right before the = sign so !=. Meaning NOT EQUAL ( ! means NOT ). Please note that after the single slash ' after {print $1}' there's another backtick `. Might be hard to see.

Do you not have "whoami"? Type whoami at your shell prompt and it should tell you the current logged in user. Use that instead of the who command, save some time...

in my case user1 user2 user3 and so on will be authorized users to run this script - I should allow them and rest others who are NOt "who -m | awk '{print $1}'" the out put should not use it and exit out.

That's what that will do. It will only allow someone who is logged in with a name you've authorized to run the script, to execute the script. If the person logged in is anyone else, it will exit. So, only user1, user2, and user3 will be able to run that script without it exiting with the error.

there is a deference in whoami and who -m | awk 'Print $1}'
whoami shows me "root" and who -m shoes "user1"

ah ok. I guess I learned something. :slight_smile: So did that work for you?