Username and password


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Username and password
# 1  
Old 05-08-2012
Question 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 corresponding username. Is it possible to do this by using a cat and grep command or is there another way. You can see the progress I have made below. Any help would be massively appreciated.

Thanks

Code:
while :
do
   echo "enter a name"
   read user
   cut -f1 usernames | grep -i -q "$user"
   a=$?
   echo "enter a password"
   read pass
   cut -f2 usernames | grep -q "$pass"
   b=$?

   if [ $a = 0 ] && [ $b = 0 ]
   then
      echo "welcome"
      menu
      exit
   else
      echo "access denied please try again"
   fi
done


The only problem with this is that the password grep does not search for the corresponding username.

Here is the original file i am using:

Username Password

John 123
Dan 345
Matt 678

Smilie


Moderator's Comments:
Mod Comment Please use code tags, thanks!

Last edited by zaxxon; 05-08-2012 at 09:02 AM.. Reason: code tags & indention, see PM
# 2  
Old 05-08-2012
Hi,

The default delimiter for cut is a tab not a space. Use --delimiter=" " to get the second field.

In your code the cut does not return what you think it does.
Code:
$cut -f2 usernames 
John 123
Dan 345
Matt 678

# 3  
Old 05-08-2012
Code:
while :
do
echo "enter a name"
read user
user_record=$(grep "^$user " usernames)
real_pass=$(echo $user_record|cut -d\  -f2 )
echo "enter a password"
read pass
...
done

# 4  
Old 05-08-2012
Code:
#! /bin/bash

while :
do
    echo "enter a name"
    read user
    grep -iq "^$user" usernames
    a=$?
    echo "enter a password"
    read pass
    grep -i "^$user" usernames | grep -q "$pass$"
    b=$?
    
    if [ $a -eq 0 -a $b -eq 0 ]
    then
        echo "Welcome"
        menu # I'm assuming "menu" is a function you haven't pasted here.
        break # "break" is a graceful way of getting out of a loop. Preferred over "exit"
    else
        echo "Access Denied. Please try again"
    fi
done

# 5  
Old 05-08-2012
Thats excellent thank you, and is exactly what im looking for . Just a couple of questions if you dont mind (although im guessing a couple may sound sillly)
1) "^$user" - what does the ^ symbol do?
2)on the part grep -q "$pass$" what does the $ at the end of pass do?

As for menu, you were spot on, it was just a function that i didnt think needed to be pasted here.

Cheers
# 6  
Old 05-08-2012
1) "^$user" - what does the ^ symbol do?
The carat means "line starting with".

2)on the part grep -q what does the $ at the end of pass do?
The dollar means "end of line".


The technique posted is not perfect because you could get multiple matches for similar usernames or similar passwords. e.g. fred and freda . The match strings would be better if they included the delimiter (in this case a single space character):
Code:
"^${user} "
" ${pass}$"


Last edited by methyl; 05-08-2012 at 10:16 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How can i su automaticaly in same server with different username and same password?

Hi I am new to shell scripting, Can you please help me in writing a script that can switch user in same server with different user name and same password. I want to perform some functional task in a particular user and need to switch user and perform same activity in another user and so on ... ... (4 Replies)
Discussion started by: Dew
4 Replies

2. Shell Programming and Scripting

Steps after username and password is entered !

Hi, I know this sounds crazy question.. but I am just curious to know what happens next when I enter username and password and hit enter on a new Unix session (using Putty)? I mean which file gets executed, how the default login shell is determined etc... regards juzz4fun (5 Replies)
Discussion started by: juzz4fun
5 Replies

3. Shell Programming and Scripting

Logging in with Username/Password on one line

Hello, I am currently working on a project that requires me to remote login into another UNIX system using different credentials. The problem is that progamatically I cannot simply feed the password into the UNIX system. Is there a way to feed the password within one command line statement. I... (1 Reply)
Discussion started by: myoung88
1 Replies

4. 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

5. Shell Programming and Scripting

Username password asked during loging

Hi, Whenever I open my unix box,after providing username and password I get the following message. Are you authorised to use this computer as detailed above? (Y)es/(N)o : y Export: Release 10.2.0.2.0 - Production on Mon May 16 16:00:15 2011 Copyright (c) 1982, 2005, Oracle. All rights... (5 Replies)
Discussion started by: emilybose
5 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

automating username / password entry

I have a database that contains a list of server names, and the password for the root user on several servers (100+). I need to verify the passwords for each of the servers in an automated fashion because the database continues to grow. All of the users that I'm going to test are ROOT. I can't... (1 Reply)
Discussion started by: jbeck22
1 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. UNIX for Dummies Questions & Answers

redirecting password as username for rlogin

Is the following even possible ? by echo $3, I mean enter password when prompted for it. My main issue is that it would deal with a prompted password, which is passed from the command line like this: ./processing serverA user password I cannot not use expect here, I heard that was very... (1 Reply)
Discussion started by: seaten
1 Replies
Login or Register to Ask a Question