Build your program step by step using Shell Scripting - Part 5

In previous publications of this round called «Build your program step by step using Shell Scripting» We have already covered how to implement the following modules:

Shell scripting

Now in this new publication we will see how to implement a:

USER AUTHORIZATION MODULE WITH PASSWORD

Which should be the module (section) of your program that allows you to ensure that only the user or users previously defined to use it can execute the operations designated for your program. It may or may not include a password for said authorized users. In this case, we will explore one with username and password validation.

I personally add the following lines to create this module:

Below the code:


###############################################################################
# INICIO DEL MODULO DE AUTORIZACIÓN DE USUARIO CON CONTRASEÑA
###############################################################################

setterm -background blue

espeak -v es-la+m1 -a 100 -p 50 -s 150 -b UTF-8 --stdout "ESTIMADO USUARIO: RECUERDA QUE EL LINUX POST INSTALL SCRIPT BICENTENARIO SOLICITA TU AUTENTICACION Y VALIDACION PARA EVITAR USOS INDEBIDOS POR PARTE DE PERSONAL NO AUTORIZADO. EN CASO DE NO SABER USUARIO Y CONTRASEÑA, ESCRIBA SALIR EN MAYUSCULA." | aplay -q

echo "ESTIMADO USUARIO: RECUERDA QUE EL LINUX POST INSTALL SCRIPT BICENTENARIO SOLICITA TU AUTENTICACION Y VALIDACION PARA EVITAR USOS INDEBIDOS POR PARTE DE PERSONAL NO AUTORIZADO. EN CASO DE NO SABER USUARIO Y CONTRASEÑA ESCRIBA, SALIR EN MAYUSCULA." | clear

dialog --infobox "\n \n \nESTIMADO USUARIO: RECUERDA QUE EL LINUX POST INSTALL SCRIPT BICENTENARIO SOLICITA TU AUTENTICACION Y VALIDACION PARA EVITAR USOS INDEBIDOS POR PARTE DE PERSONAL NO AUTORIZADO. EN CASO DE NO SABER USUARIO Y CONTRASEÑA, ESCRIBA SALIR EN MAYUSCULA." 10 80

################################################################################

USUARIO=0

until [ "$USUARIO" = "sysadmin" ];

do

espeak -v es-la+m1 -a 100 -p 50 -s 150 -b UTF-8 --stdout "ESTIMADO USUARIO: INTRODUZCA UN NOMBRE DE USUARIO VALIDO." | aplay -q

read -p "INTRODUZCA UN NOMBRE DE USUARIO VALIDO / PLEASE ENTER A VALID USER: " USUARIO
echo ""

if [ "$USUARIO" = "SALIR" ]; then

   setterm -background green
   play /usr/share/sounds/warning.wav 2> /dev/null

   echo -e "\nUsted ha decidido salir del Programa, hasta luego.\nYou have decided to leave the program, later.\n\n\nPuede intentarlo mas tarde.\nYou can try later."

   exit 0

fi


if [ "$USUARIO" != "$USR1_VALID" ]; then

   setterm -background red
   play /usr/share/sounds/error.wav 2> /dev/null

   echo -e "\nError al introducir un Usuario valido.\nFailed to enter a valid user.\n\n\nVuelva a intentarlo de nuevo.\nPlease try again."

fi

done

if [ "$USUARIO" = "$USR1_VALID" ]; then

   setterm -background blue
   play /usr/share/sounds/info.wav 2> /dev/null

   echo -e "\nAuntenticación de Usuario valido exitosa.\nValid user authentication successful.\n\n\nEspere unos segundos para continuar.\nWait a few seconds before continuing"

   sleep 3

fi

################################################################################

CLAVE=0

until [ "$CLAVE" = "lpi*sb8" ];

do

espeak -v es-la+m1 -a 100 -p 50 -s 150 -b UTF-8 --stdout "ESTIMADO USUARIO: INTRODUZCA UNA CONTRASEÑA VALIDA." | aplay -q

read -p "INTRODUZCA UNA CONTRASEÑA VALIDA / ENTER A VALID PASSWORD: " CLAVE
echo ""

if [ "$CLAVE" = "SALIR" ]; then

   setterm -background green
   play /usr/share/sounds/warning.wav 2> /dev/null

   echo -e "\nUsted ha decidido salir del Programa, hasta luego.\nYou have decided to leave the program, later.\n\n\nPuede intentarlo mas tarde.\nYou can try later."

   exit 0

fi


if [ "$CLAVE" != "$PASSWD_VALID" ]; then

   setterm -background red
   play /usr/share/sounds/error.wav 2> /dev/null

   echo -e "\nError al introducir una Contraseña valida.\nFailed to enter a valid password.\n\n\nVuelva a intentarlo de nuevo.\nPlease try again."

fi

done

################################################################################

if [ "$CLAVE" = "$PASSWD_VALID" ]; then

   setterm -background blue
   play /usr/share/sounds/info.wav 2> /dev/null

   echo -e "\nAuntenticación de Contraseña valida exitosa.\nValid password authentication successful.\n\n\nEspere unos segundos para continuar.\nWait a few seconds before continuing"

   sleep 3

fi

espeak -v es-la+m1 -a 100 -p 50 -s 150 -b UTF-8 --stdout "ESTIMADO USUARIO: ACCESO GARANTIZADO - USUARIO Y CONTRASEÑA CORRECTA" | aplay -q

echo "ESTIMADO USUARIO: ACCESO GARANTIZADO - USUARIO Y CONTRASEÑA CORRECTA." | sleep 5 | clear

dialog --infobox "\n \n \nESTIMADO USUARIO: ACCESO GARANTIZADO - USUARIO Y CONTRASEÑA CORRECTA." 10 80 ; sleep 5

DIALOG=${DIALOG=dialog}

$DIALOG --title "LINUX POST INSTALL - SCRIPT BICENTENARIO" --clear \
        --yesno "\n \n \nBIENVENIDO USUARIO: $(whoami), PRESIONE "SI" PARA CONTINUAR O "NO" PARA FINALIZAR." 10 80

case $? in
  0)
    echo ""
    echo ""
    echo "Continuando el proceso..."
    echo ""
    echo "."
    sleep 1
    echo ".."
    sleep 1
    echo "..."
    sleep 1
    clear;;
  1)
    exit 0;;
  255)
    exit 0;;
esac

dialog --infobox "\n \n \n ESPERE UNOS SEGUNDOS MIENTRAS EL PROGRAMA SE INICIALIZA..." 10 80 ; sleep 3

###############################################################################
# FINAL DEL MODULO DE AUTORIZACIÓN DE USUARIO CON CONTRASEÑA
###############################################################################

As you can see, this module first configures your terminal screen to blue background to a better visual appreciation of the user, then emit auditory messages with speak y Visual with threw out y Dialogue. Then through a loop using the command until validates that the value entered for the Username be compared to the valid user value (s) previously defined, giving the user the possibility of closing the program correctly by typing the word LOGOUT. If the user validation is successful, proceed to carry out the same procedure with the password. To finish ending in case of being successful the validation of the user and Password with a simple menu made in Dialogue asking if User X wishes to continue continuing the program process or not. If so, issue a simple progressive bar waiting done with the command CASE​.

=============

Recommendation:

Schedule or think! What is first? o What is more important?

Without a doubt, when we start in programming, the first thing we should learn is algorithms or flow diagrams as theoretical bases to learn logic and mathematics to increase our logical-mathematical reasoning, however, this is sufficient or adequate basis to start programming . Understand that Learn to Program, it is not enough, it is necessary Learn to Think.

IN WHICH PROGRAMMING LANGUAGE SHOULD I BEGIN TO ACHIEVE RAPIDLY LEARNING TO PROGRAM?

This is surely a mandatory question that we all ask ourselves when we start in the wonderful world of programming. But it is not the first correct question that we should ask ourselves, but:

DO I HAVE THE ANALYTICAL CAPACITY NEEDED TO ANALYZE A PROBLEM, EXTRACT ITS ELEMENTS, AND VIEW A RESPONSE?

And certainly learning to program in any of the most common or powerful languages ​​has many undeniable benefits. Since it provides us with the necessary tools to develop our own applications, it helps us to locate problems in technological devices, and it opens doors for us in the job market, but programming is only a small part of computer science.

While Program or write code consists of teaching a computer (through an application, software or system) to perform a certain operation, after a Option X performed by an operator or a Event X by or within the same Hardware or Software that supports it; The principles of Computer Science (Informatics / Systems) they teach us reasoning and logic skills that are useful in areas other than technology. Computer science, in essence, teaches how to process and represent information.

Therefore, I consider this to be the most appropriate way of Learn to program.

I explain, taking as an example, this other post called Don't learn to code. Learn to think » de Yevgeny Brikman, where this Engineer briefly exposes us the difference between knowing how to program, and knowing how to think like a computer scientist with this analogy:

As ubiquitous as a technology is, this does not make us learn it when we study it in a teaching center. As an example, we could take that almost anyone at some point in life has ever flown in an airplane, but from there to achieving a pilot's license, there is a very long way to go, which is generally not part of the formal studies of a person, but very particular studies on a specific area.

Explained in more detail, this means that the knowledge that anyone can have about how airplanes work basically many of us have from secondary and university education or in any technical career (engineering), such as physics and mathematics that help us to understand how the forces present in flight, and external elements such as gravity, atmospheric pressure, speed, friction or lift. And just as biology helps us understand how a human being behaves at X height, or how it is affected by a lack of oxygen and extreme cold. Or the story that allows us to know the process that gave rise to how airplanes were created, and what role they play in transportation and commerce. All these knowledge together acquired in a teaching center, from various areas of knowledge, provide us with a general idea of ​​what an airplane is and how it works. General subjects help us understand a wide variety of problems, including how airplanes fly. Something very different from a specialized subject that only teaches you to operate one of those machines, a type of airplane. Therefore, for the same reason, we should focus on studying Computer Science, and not just programming. Since while Computer Science teaches us globally to think in a general (systemic) sense, programming is only a tool that allows us to translate an idea or problem Y through a code X, previously analyzed (correctly or incorrectly).

This and many other points of view contributed by Yevgeny Brikman in his post about what Schedule it is only part of a broader knowledge embedded within the Computer's science, which are essential for every programmer.

Finally, remember to progress in a self-taught way with learning about the most important commands of GNU / Linux to continue advancing knowledge that they will use later, and that everything presented here about the development of some module, function, variable, command or action in general it can be carried out in different ways or alternative methods, different from those described here.

Below a Screencast made by me, where you can see how this module is executed, previous or later modules to be taught, so that you can see the potential of Shell Scripting at a medium level (Not advanced):

LPI-SB8 Test ScreenCast

(LINUX POST INSTALL - BICENTENNIAL SCRIPT 8.0.0) - Part 2


Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   Another who was passing said

    To have if I have understood it correctly. You ask the user to enter their username ... fine. And after you enter your password ... and it turns out that the password is "lpi * sb8" ... and that it is "hardcoded" in the script.

    If the user has read access to the script… bingo !!… they already know which username and password they have to enter !!!

    I don't know if I've made a mistake about something ... but if so, that's very unsafe ...

  2.   Jose Albert said

    True! I gave a method now, each one with that innate curiosity that characterizes the human race must seek its perfection or strengthening in the security lines.