1

I currently have a script that runs upon a user logging into a Linux server and at the end it prints

[Terms of use agreement prints here]

read -p "Agree and continue..." -nl -s

However, most users I find are just "entering" through the prompt (spacebar, enter, random keys, etc) since the script isn't looking for specific entry, and now users have written their own scripts or macros to enter past this.

As a solution, I would like the script to print a string of random numbers, and then require the user to enter those numbers back into the prompt before allowing access.

Example:

"Welcome to Server! Please enter the following one-time passphrase to continue: 1234.

Key: "

How can I accomplish this?

0

It is necessary to use read -p? Otherwise you can do the following (this would be a proof of concept script):

#!/bin/bash
RANDOMNUM=$(shuf -i 0000-9999 -n 1)
echo "Introduce password $RANDOMNUM"
read value
if [ $value == $RANDOMNUM ];
then
        echo "Password Match!"
else
        exit
fi

Shuf would create a random number of 4 digits between 0000 and 9999.

New contributor
Dasel is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • Using read -p is not necessary, it was only being used because I found that solution online previously. I'll give this a shot, thanks! – customcables067 Jan 10 at 20:23
  • Do you know how I could enable the script to execute exit if the numbers are incorrect, or if a non-4 digit numeric entry is provided? – customcables067 Jan 10 at 21:13
  • You have to only add an else to the if as any value that does not match with the condition will be threated as "else". I have modified my previous script to include the else and the exit values. – Dasel Jan 10 at 21:19

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.