Implementing Password


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Implementing Password
# 1  
Old 05-06-2009
Implementing Password

I am trying to implement a login screen to the following code how would i go about doing so. I have try to place the password in a variable using if statements which would usually work but as i have the system in a while loop i think i need to find another method.

Code:
#!/bin/bash
#Filename: Assigntest Author: Luke Francis
quit=n
while [ "$quit" = "n" ]
do
  clear
  echo "OPERATOR ADMINISTRATIVE TOOL"
  echo
  echo "1. User Information"
  echo "2. Network Connectivity"
  echo "3. Processes"
  echo "4. System Information"
  echo "5. Hardware Utilization"
  echo "Q. Quit"
  echo
  echo "Which option do you require?"
  read menunumber
case $menunumber in
       1)clear
         echo "USER INFORMATION"
         echo
         echo "1. Registered Users"
         echo "2. Disk Usage"
         echo "3. Last Logins"
         echo "4. Users Currently Logged In"
         echo "5. Total number of users"
         echo "B. Back to Main Menu"
         echo "Q. Quit"
         echo
         echo "Which option do you require?"
         read menunumber2
         case $menunumber2 in
              1)clear
                echo "The users registered on the system are:"
                echo
                awk -F: '{print $1}' /etc/passwd
                echo
                echo "Hit the Enter Key to continue"
                 read junk;;
              2)clear
                echo "Disk Usage is as follows:"
                echo
                du
                echo
                echo "Hit Enter Key to continue"
                read junk;;
              3)clear
                echo "Information on last noted login can be found next to      each username."
                echo
                last
                echo
                echo "Hit Enter Key to continue"
                read junk;;
              4)clear
                echo "Users currently logged in are:"
                echo
                w
                echo
                echo "Hit Enter Key to continue"
                read junk;;
              5)clear
                echo "The total number of users are:"
                echo
                who -q
                echo
                echo "Hit Enter Key to continue"
                read junk;;
              Q|q)clear
                  echo "Are you sure you want to quit? Y/N"
                  read choice1
                  case $choice1 in
                       N|n)clear
                           echo "Hit Enter Key to continue"
                           read junk;;
                       Y|y)quit=y;;
                         *)clear
sleep 1;;
                  esac
                  ;;
         esac
         ;;
       2)clear
         echo "NETWORK CONNECTIVITY"
         echo
         echo "1. NIC Status"
         echo "2. Machine Availability"
         echo "B. Back to Main Menu"
         echo "Q. Quit"
         echo
         echo "Which option do you require?"
         read menunumber4
         case $menunumber4 in
              1)clear
                echo "Information reagrding NIC status can be found below:"
                echo
                /sbin/ifconfig
                echo
                echo "Hit the Enter Key to continue"
                read junk;;
              2)clear
                echo "Available hosts and addresses are shown below:"
                echo
                cat /etc/hosts
                echo
                echo "Hit Enter Key to continue"
                read junk;;
       Q|q)clear
           echo "Are you sure you want to quit? Y/N"
           read choice2
           case $choice2 in
                N|n)clear
                    echo "Hit Enter key to continue"
                    read junk;;
                Y|y)quit=y;;
 *)clear
                sleep 2;;
           esac
           ;;
         esac
          ;;
        3)clear
          echo "PROCESSES"
          echo
          echo "1. Running Processes"
          echo "2. Installed Software"
          echo "B. Back to Main Menu"
          echo "Q. Quit"
          echo
          echo "Which option do you require?"
          read menunumber5
          case $menunumber5 in
               1)clear
                 echo "Current running processes are displayed below"
                 echo
                 ps aux
                 echo
                 echo "Hit Enter key to continue"
                 read junk;;
               2)clear
                 echo "The software currently installed on this host is:"
                 echo
                 rpm -qa
                 echo
                 echo "Hit Enter key to continue"
                 read junk;;
        Q|q)clear
           echo "Are you sure you want to quit? Y/N"
           read choice3
           case $choice3 in
                N|n)clear
                    echo "Hit Enter key to continue"
                    read junk;;
 Y|y)quit=y;;
                  *)clear
                sleep 3;;
           esac
            ;;
         esac
          ;;
        4)clear
          echo "SYSTEM INFORMATION"
          echo
          echo "1. Machine Name"
          echo "2. System Date & Time"
          echo "3. Licensing Information"
          echo "4. Last System Reboot"
          echo "B. Back to Main Menu"
          echo "Q. Quit"
          echo
          echo "Which option do you require?"
          read menunumber6
          case $menunumber6 in
               1)clear
                 echo "The name of the machine is:"
                 uname -m
                 echo
                 echo "Hit Enter key to continue"
                 read junk;;
               2)clear
                 echo "The current date and time is:"
                 echo
                 date
                 echo
                 echo "Hit Enter key to continue"
                 read junk;;
               4)clear
                 echo "The last scheduled system reboot was:"
                 echo
                 who -b
                 echo
echo "Hit Enter Key to continue"
                 read junk;;
               B)read junk;;
               Q|q)clear
                   echo "Are you sure you want to quit? Y/N"
                   read choice4
                   case $choice4 in
                   N|n)clear
                       echo "Hit Enter key to continue"
                       read junk;;
                   Y|y)quit=y;;
                     *)clear
                   sleep 4;;
               esac
             ;;
         esac
         ;;
       5)clear
         echo "HARDWARE UTILISATION"
         echo
         echo "1. Unix Filesystem Usage"
         echo "2. RAM Availability"
         echo "3. Swap Space"
         echo "4. CPU Utility"
         echo "Q. Quit"
         echo
         echo "Which option do you require?"
         read menunumber6
         case $menunumber6 in
              1)clear
                echo "Information regarding usage of the Unix filesystem as requested can be found below:"
                echo
                df -h
                echo
                echo "Hit Enter Key to continue"
                read junk;;
              2)clear
                echo "RAM usage information can be found below:"
echo
                cat /proc/meminfo
                echo
                echo "Hit Enter Key to continue"
                read junk;;
              3)clear
                echo "The amount of free and used Swap space is:"
                echo
                cat /proc/meminfo
                echo
                echo "Hit Enter Key to continue"
                read junk;;
              4)clear
                echo "The first column indicates size of CPU run queue"
                echo
                vmstat
                echo
                echo "Hit Enter Key to continue"
                read junk;;
        esac
        ;;
       Q|q)clear
            echo "Are you sure you want to quit? Y/N"
                   read choice5
                   case $choice5 in
                   N|n)clear
                       echo "Hit Enter key to continue"
                       read junk;;
                   Y|y)quit=y;;
                     *)clear
                   sleep 5;;
              esac
              ;;
  esac
done
clear
echo "Thank you for using the Operator Administrative Tool"

I would like the login screen to appear first, and if the password 12345 was not enetered then a message saying incorrect password would be displayed. I have tried to implement this but am not having much luck. How would this be implemented to the code above? All help is appreciated. Thanks
# 2  
Old 05-07-2009
Quote:
Originally Posted by warlock129
I am trying to implement a login screen to the following code how would i go about doing so. I have try to place the password in a variable using if statements which would usually work but as i have the system in a while loop i think i need to find another method.
...
...
I would like the login screen to appear first, and if the password 12345 was not enetered then a message saying incorrect password would be displayed. I have tried to implement this but am not having much luck. How would this be implemented to the code above?
You can add some authentication code right at the top of your script. Read the username/password and either exit the script or go ahead with the rest of your script.

A short demo follows:

Code:
#!/bin/bash

clear
# Read the username. The trailing newline is
# suppressed due to the "-n" option.
echo -n "Enter username : "
read usr
if [ -z $usr ]; then
  echo "Username must be entered."
  exit
elif [ $usr = "ronald" ]; then
  pswd="reagan"
elif [ $usr = "mikhail" ]; then
  pswd="gorbachev"
elif [ $usr = "anwar" ]; then
  pswd="sadat"
fi

# Now read the password entered by user
# It will not be echoed on the terminal due
# to the "-s" option.
read -s -p "Enter password : "  pswd
echo
if [ -z $pswd ]; then
  echo "Password must be entered."
  exit
elif [[ $usr = "ronald" && $pswd = "reagan" ||
        $usr = "mikhail" && $pswd = "gorbachev" ||
        $usr = "anwar" && $pswd = "sadat" ]]; then
  echo "Successful login!"
else
  echo "Sorry, incorrect username/password"
  exit
fi

################################################################### 
# Now add your script from this point onwards. If we've reached here, the user
# entered a correct username/password combination. You may want to tweak
# the code above to accept just *one* username/password combination.
################################################################### 

# Your script follows...

Of course, this is a rather trivial and insecure authentication code - the password is seen in cleartext. Anyone who reads this script knows all passwords. You may want to use "hashes" instead of cleartext passwords.

Execution:

Code:
$ cat read_pswd.sh
#!/bin/bash       
clear
# Read the username. The trailing newline is
# suppressed due to the "-n" option.
echo -n "Enter username : "
read usr
if [ -z $usr ]; then
  echo "Username must be entered."
  exit
elif [ $usr = "ronald" ]; then
  pswd="reagan"
elif [ $usr = "mikhail" ]; then
  pswd="gorbachev"
elif [ $usr = "anwar" ]; then
  pswd="sadat"
fi

# Now read the password entered by user
# It will not be echoed on the terminal due
# to the "-s" option.
read -s -p "Enter password : "  pswd
echo
if [ -z $pswd ]; then
  echo "Password must be entered."
  exit
elif [[ $usr = "ronald" && $pswd = "reagan" ||
        $usr = "mikhail" && $pswd = "gorbachev" ||
        $usr = "anwar" && $pswd = "sadat" ]]; then
  echo "Successful login!"
else
  echo "Sorry, incorrect username/password"
  exit
fi
$
$ ./read_pswd.sh
<screen_gets_cleared>
Enter username : mikhail
Enter password :
Successful login!
$
$ ./read_pswd.sh
<screen_gets_cleared>
Enter username : yasser
Enter password :
Sorry, incorrect username/password
$

Hope that helps,
tyler_durden

_________________________________________________
"Without pain, without sacrifice, we would have nothing."
# 3  
Old 05-07-2009
Ok i have read your response which seems to make alot of sense, i only want one user on the system with a username of "admin" and a password of "0600519" if possible could you please insert this into my code pasted earlier because i am having trouble putting it in. Sorry to disturb you but i really do appreciate your help.
# 4  
Old 05-07-2009
We limit access to a script to a small group of users by adding those users in a secondary group (in this case "helpdesk") and setting tight permissions on the script.

Code:
-rwxr-x---   1 root       helpdesk

# 5  
Old 05-07-2009
Thanks for all the help i got it working
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Forum Support Area for Unregistered Users & Account Problems

Password sent via reset password email is 'weak' and won't allow me to change my password

I was unable to login and so used the "Forgotten Password' process. I was sent a NEWLY-PROVIDED password and a link through which my password could be changed. The NEWLY-PROVIDED password allowed me to login. Following the provided link I attempted to update my password to one of my own... (1 Reply)
Discussion started by: Rich Marton
1 Replies

2. Shell Programming and Scripting

Need help in implementing expect

Hello All, I am trying a shell script for automatically login to test servers and pulling the output of top command from all using expect. ----snippet of code --- #!/usr/bin/expect -f #!/bin/bash server1=10.251.222.51 server=("$server1") i=1 for exp_server in ${server}; do expect -c... (3 Replies)
Discussion started by: Renjesh
3 Replies

3. Shell Programming and Scripting

Need help in implementing logic

i have following input file... 00290002STDR000000000000000000000000000EOD END TRANSACTION ^@^@^@^@^@^@^@^@^@^@^@^@^ 00299998STDR070000000007000000000000000STANDING DEBITS ^@^@^@^@^@^@^@^@^@^@^@^@^... (1 Reply)
Discussion started by: sagarrd
1 Replies

4. Shell Programming and Scripting

Help with implementing logging

I'm trying to add logging to an existing script which echos a number of lines to the screen. I've added a switch to the script that is going to suppress much of this output and put it in a file instead. The way I envisioned it was like this: $log would be set to either "" or the log files... (8 Replies)
Discussion started by: cheetobandito
8 Replies

5. Programming

implementing AVL tree

how is an AVL tree implemented and insertions are made to itwenever a new socket is created and deletions are made when the socket is closed or connection is disconnected.how to read and print the names of all the current sockets. Please send the source code. (1 Reply)
Discussion started by: arjunjag
1 Replies

6. Shell Programming and Scripting

is there anyway of implementing password aging in NIS?

Hi , is there anyway of implementing password aging in NIS? I would say thanks in advance. Thanks and regards, HAA (1 Reply)
Discussion started by: HAA
1 Replies

7. IP Networking

implementing ftp

i have a client server connection steady and running... but the problem here is that the file transfer is very crude and succeptible to risks... so i want to implement ftp.. can anybody suggest a way to implement it or any book to read? (4 Replies)
Discussion started by: damn_bkb
4 Replies

8. Programming

Implementing the redirection

Hi all I am facing a problem with redirection. Its somewhat related to parsing. I am following the following steps. 1. take the command and tokenize it. 2. if redirection is there then give it to redirection unit 3. if pipe is there give it to piping unit. 4. do until the command ends ... (0 Replies)
Discussion started by: mobile01
0 Replies

9. Programming

Implementing a shell in C

Hi, I am implementing a shell in C, with the following problem... Suppose the shell is invoked from the command line as >> myshell < test.in > test.out 2>&1 I have to execute the commands in test.in and redirect them to test.out How does one detect in the main function that the shell... (1 Reply)
Discussion started by: jacques83
1 Replies

10. Programming

Implementing a shell

I'm implementing a shell in C that supports piping, output redirection, and background processing, and a few other commands. I was wondering how I'd go about implementing the output redirection. So, I'd open a file and I'd fork and execute the command. But how would I get stdout into the file? Any... (10 Replies)
Discussion started by: ununium
10 Replies
Login or Register to Ask a Question