LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to validate login from Bash script (https://www.linuxquestions.org/questions/programming-9/how-to-validate-login-from-bash-script-4175414599/)

ravisha 07-03-2012 04:00 AM

How to validate login from Bash script
 
Hello,
I'm new to bash scripting, I have a need to write a script which will take user name and password as input validate the inputs and check whether the username/password combination can really login to the system and if authentication is successful then it has to run a set of other commands roughly about 200 different scripts but if authentication fails then it has ask for correct user name and password again and do the above steps again.

If some one can help me with It would be very much helpful.

Thanks,
Ravi

Snark1994 07-03-2012 05:35 AM

Could you explain the "actual" end goal (i.e. what you are actually trying to achieve)? It sounds like you're trying to reinvent the wheel, and you actually want the user to ssh into the system, or something like that.

pan64 07-03-2012 05:36 AM

this will be done by the system automatically, just execute su - <username>. It will ask for pw and based on ~/.profile or ~/.bashrc can execute some programs...

ravisha 07-03-2012 05:44 AM

Hi Snark,
I have a load file which has about 200 commandline entries asking for user name and password, I figured out a way to input user name and password to those script file but instead of asking 200 times I want the user to enter username and password only once.
The catch is we have different servers and user authentication is done via ldap, and I don't have access to do an ldap.
So instead I want to my script to check whether the given combo of username and password works in the given system.
If yes I would take those inputs to run my load file and execute if the authentication fails then I want the script to echo error and ask for valid user name/password combo.
A simple conditional statement is what I'm looking for.
Any way thanks for the response.

Snark1994 07-04-2012 04:39 AM

Right... I'm still not 100% convinced this is the best way to do it, but meh, this should work, and it's not immediately obvious you're doing something silly:

Code:

#!/usr/bin/env bash

success=0
while [[ $success -eq 0 ]]; do
    echo -n "username:"
    read uname
    su $uname -c "/path/to/file_with_commands.bash"
    if [[ $? -eq 0 ]]; then
        success=1
    fi 
done

'su' returns exit code 125 for incorrect password, but su itself pauses for a second or two and prints an error message so it didn't seem necessary to test for that in the script itself.

Hope this helps,

ravisha 07-04-2012 07:23 AM

Hi Snark,
It works. Thanks a ton.


All times are GMT -5. The time now is 09:14 AM.