LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-03-2012, 04:00 AM   #1
ravisha
LQ Newbie
 
Registered: Jul 2012
Posts: 3

Rep: Reputation: Disabled
Question 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
 
Old 07-03-2012, 05:35 AM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
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.
 
Old 07-03-2012, 05:36 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
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...
 
Old 07-03-2012, 05:44 AM   #4
ravisha
LQ Newbie
 
Registered: Jul 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
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.
 
Old 07-04-2012, 04:39 AM   #5
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
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,
 
1 members found this post helpful.
Old 07-04-2012, 07:23 AM   #6
ravisha
LQ Newbie
 
Registered: Jul 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
Hi Snark,
It works. Thanks a ton.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Script creation(How to validate SID?) pinga123 Linux - Newbie 4 08-16-2010 11:06 PM
Validate and sort input in script stuaz Programming 5 08-26-2009 04:18 PM
URGENT: Please validate this script PKrishna Linux - Newbie 1 04-01-2008 11:03 AM
bash login script bunnyknight13 Linux - Newbie 4 09-10-2004 08:02 PM
bash login script seanatis Programming 6 09-06-2004 08:00 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration