Shell program with username and password


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Shell program with username and password
# 1  
Old 02-24-2010
Shell program with username and password

Hi I am new to unix and I am trying to figure out how to write a shell script with a login name and password. I want to do something along the lines of if both are correct it echoes "you are logged in" and if the password is wrong it echoes "wrong password" and same with the login name. I've tried to do it with if then statements but i can't seem to get it to work but I think i am going about it the wrong way. Please help!
Thanks
# 2  
Old 02-24-2010
Post what you have written so far...
# 3  
Old 02-25-2010
From where you get the password and how you compared it ,explain clearly or post the script which you tried so for.
# 4  
Old 02-25-2010
As Karthik said,I too don't know where you get the user name and password.But,I wrote the following script to read the user name and password from user and then comparing it.

The script is as follows.

Code:
 echo "Enter The Login Name:"
read username
echo "Enter The Password:"
read password

if [[ $username == "user"  && $password == "password" ]]
then
  echo -e "You're Logged In\n"
elif [ $username != "user" ] 
then
  echo -e "Invalid User Name\n"
else
  echo -e "Invalid Password\n"
fi


Last edited by Scott; 02-25-2010 at 11:58 AM.. Reason: Please use code tags and indent your code
# 5  
Old 02-25-2010
vivekraj, you seem to understand what i need to do. I attempted something similar but i was having trouble comparing the two (username and password). What if i wanted to do this for multiple users? Would i use a CASE statement? such as:

Code:
case $username in
    alex ) pass=alex2;;
    sean ) pass=sean2;;
    ashley ) pass=ashley2;;
    # *) echo "error"
esac

But then i'm thinking this would affect the whole code and you would need the names as such: alex | sean | ashley.

What do you guys think?

---------- Post updated at 11:35 AM ---------- Previous update was at 10:51 AM ----------

also guys, this is what i had so far. i know it's wrong (especially the * part, it doesn't work) but like i said i am very new to this. i was trying to do this for one user first but i actually want to do multiple like i mentioned.

Code:
#!/bin/sh

echo "Enter login name and password"
read trythis

if [ $trythis = "Aarin_dtfc" ]
then
	echo "You are logged in!"
else [ $trythis = "Aarin_*" ]
	echo "Wrong password!"
	exit 1
else [ $trythis = "*_dtfc" ]
	echo "Login name is not recognized!"
	exit 2
fi
exit 0



---------- Post updated at 02:02 PM ---------- Previous update was at 11:35 AM ----------

ok i rewrote the code to this:

Code:
#!/bin/sh
echo "Enter username"
read username
if [ $username = "Aarin" ]
then  
 echo Enter password
 read password
case $username in
 Aarin ) pass=dtfc;;
 Rick ) pass=unpo
esac
while [ "$password" != "$pass" ]; do
 echo "Wrong password!"
 return 1
done
echo "You are logged in!" 
return 0
else
 echo "Login name not recognized"
fi
return 2

the only thing i am having trouble doing is adding multiple usernames to the if statement

Last edited by pludi; 02-25-2010 at 04:17 PM.. Reason: Please use code tags.......
# 6  
Old 02-28-2010
Code:
#!/bin/bash
clear
echo "          		1 = Login

      			2 = Change the Login Password

			3 = Forgot your password?

			4 = Set the recall answer to question
"
	read number
	case $number in
	1)
	      if [ -s /home/alex/Documents/PASS/password_PASS ]; then
			    existpass=`cat /home/alex/Documents/PASS/password_PASS`
			    echo "Type the existing pass:"
			    read givenpassasexisted
		         if [ "$existpass" = "$givenpassasexisted" ]; then
			  	clear; echo "Success!"
		         else    	echo "Bad" 
			          exit
			 fi
		else echo "There is no password."
		
	fi;;
		
	2)
		   if [ -s /home/alex/Documents/PASS/password_PASS ]; then
			    existpass=`cat /home/alex/Documents/PASS/password_PASS`
			    echo "Type the existing pass:"  
				read givenpassasexisted
		         if [ "$existpass" = "$givenpassasexisted" ]; then
				echo "Type new password:"
				read newpass
				echo "Type again to confirm:"
				read newpass2
				if [ "$newpass" = "$newpass2" ]; then
		  		cd /home/alex/Documents/PASS/
				rm password_PASS
				echo $newpass > /home/alex/Documents/PASS/password_PASS
				echo $newpass > /home/alex/Documents/PASS/bringagain_PASS
				else clear; echo "Confirmation failure!"; exit
				fi
			else
				clear; echo "Wrong password!"
			fi
		  else echo "there is no password file.Create one plz."
			fi;;
	3)
		
		echo "Which is your best friend?"
		read maybebestfriend
		  realbestfriend=`cat /home/alex/Documents/PASS/recallquestion_PASS`
		if [ "$maybebestfriend" = "$realbestfriend" ]; then
			recall=`cat /home/alex/Documents/PASS/bringagain_PASS`
			echo "The password which you forgot is $recall"
		else 
			clear; echo "Wrong answer to the question which is your best friend.Try again."
		fi;;
		
	4)
		echo "Which is the current password?"
		read givenpassasexisted
			existpass=`cat /home/alex/Documents/PASS/password_PASS`
		         if [ "$existpass" = "$givenpassasexisted" ]; then
			 echo "Which is your best friend?"
			 read bestfriend
			 echo $bestfriend > /home/alex/Documents/PASS/recallquestion_PASS
		else 
			echo "Wrong password.Try again!"
			fi
		
	esac

i think it is pretty good! It has option to change your password, answer to the recall question in order to take it back and change the question to the recall question !
# 7  
Old 02-28-2010
thanks for the help. i actually rewrote to this:
Code:
#!/bin/sh
echo "Enter username" 
while : 
do
 read username
 case $username in 
  Aarin | Rick | Zach | Ashley | Alex | Nik | Wilbur ) 
   echo "Enter password" 
   break ;; 
  * ) echo Username fail. Try again. ;;
 esac
done
case $username in
 Aarin ) pass=dtfc;;
 Rick ) pass=up;;
 Zach ) pass=btiom;;
 Ashley ) pass=cayh;;
 Alex ) pass=quixotic;;
 Nik ) pass=stbm;;
 Wilbur) pass=xmomx;;
esac
read password
while [ "$password" != "$pass" ]; do
 echo "Password fail. Try again."
 read password
done
echo "You Are Logged In!"
return 0

its simpler and it works. thanks again!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing Username & password through shell script to java code

Hi, I have a shell script (script.sh) in which we are calling java code which asks for Username: Password: for authentication purpose currently we are passing the credential manually and run the script. but I am trying echo -e "user_id\npassword" | script.sh but its not... (1 Reply)
Discussion started by: rakeshtomar82
1 Replies

2. Programming

Getting username from an informix 4gl program in UNIX

How to get the user name of the Operating system from an Informix 4gl program. eg:- -rw-r----- 1 gkuser srth1 292 Jul 27 19:28 u1.txt i need to get gkuser as the result? (0 Replies)
Discussion started by: enriquegm82
0 Replies

3. UNIX for Dummies Questions & Answers

How do you reset username/password

Picked up a 3b2 running System V. Works fine, but it requires a username and password. Is the username "root" or "sysadm"? How do I find out and how to I reset it or bypass it? Thanks. (2 Replies)
Discussion started by: TanRuNomad
2 Replies

4. Shell Programming and Scripting

Username and password

Hi I am new to using unix and am struggling with a script i am writing. What i am trying to do is get a user to enter a username, check the original file i created with username and pin to see if their is a corresponding entry. Next ask the user to enter the pin and see if this matches... (5 Replies)
Discussion started by: somersetdan
5 Replies

5. Shell Programming and Scripting

Asking username and password in the middle of the Shell/perl script

Can any body help me to find out the logic I have a script chkcomponent.pl Which give some output Like component1 userid: u1 component2 userid: u2 component3 userid: u1 . . #The no of components are different in different times run Now I want this chkcomponent.pl script... (1 Reply)
Discussion started by: pareshpatra
1 Replies

6. UNIX for Advanced & Expert Users

Login through SFTP using username and password

Hi All, I want to login to a server through SFTP by giving username and password, in an automated script. I know that this can be done through public key authentication, but my requirement is to login ONLY through username and password. I am using GNU/Linux server. Please advise me !!!... (4 Replies)
Discussion started by: sparks
4 Replies

7. UNIX for Dummies Questions & Answers

How can i hide username/password

hi all, i run sqlplus command on unix(HP-UX) like "sqlplus username/password@serverA @deneme.sql" but when someone run "ps -ef | grep sqlplus", it can see my username and password :( How can i hide username and password. thanx. (1 Reply)
Discussion started by: temhem
1 Replies

8. Shell Programming and Scripting

Read Oracle Username password SID from single file and pass it to shell

Dear All I am trying to write one shell which will be running through Cron which contain one SQL query. But I want to draw/fetch the Username password and Instance name (required to loging to the database) from one single file to run that SQL query . Also this file contain details of multiple... (2 Replies)
Discussion started by: jhon
2 Replies

9. Shell Programming and Scripting

username password in script

Can we write a script to telnet to a unix server from unix with the username and password hardcoded in the script?? something like ssh a@b -p password ??? (5 Replies)
Discussion started by: roshanjain2
5 Replies

10. Programming

getting username from a c program in unix

How to get the user name of the Operating system from a c program. eg:- -rw-r----- 1 gkuser srth1 292 Jul 27 19:28 u1.txt i need to get gkuser as the result? (3 Replies)
Discussion started by: Rajeshsu
3 Replies
Login or Register to Ask a Question