Showing results for 
Search instead for 
Did you mean: 

Prompting a user for a password within a script

Go to solution
Andrew Kaplan
Super Advisor

Prompting a user for a password within a script

Hi there --

I am writing a script that will provide a telnet connection to a remote server. What I need to do is have the user prompted for his/her password during the execution of the script.

I checked the system in question for expect, and it is not installed. I could go through the motions of installing it, but before I do, I would like to see if read would work in this scenario. The shell that I am using is bash, and the OS is Fedora Core 2. What syntax would I use for read in order to have the user prompted for a username and password once the telnet session is initiated? Thanks.

A Journey In The Quest Of Knowledge
Ivan Ferreira
Honored Contributor

Re: Prompting a user for a password within a script

I suggest you to use SUDO, with sudo, you can force a user to specify it's password to run a script.

But this if you need authenticate the user to run the script, is not related to the remote password.

Now, if you want to prompt for the remote password, then you do need expect or something similar, anyway, telnet is something that you should stop using and you should use SSH instead.
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Andrew Kaplan
Super Advisor

Re: Prompting a user for a password within a script

Hi there --

Thanks for your reply. Using SSH is not an option because the server in question does not have it installed at this time. Eventually it will, but until then, telnet is the only available method of terminal connectivity.

I've included the text of the script in this reply. The user does not need a sudo account to execute the script, the username and password prompt will occur when the telnet session is initiated. The syntax within the script I need is how to have the user prompted for the username and password. The script should pause until this information is entered, and then proceed accordingly. Thanks.


# This script will provide access to the tcs # application which is located on the
# mcrs1 server.

# First disable access control so that clients can connect from any host.
xhost +

# Establish a telnet session with mcrs1. NOTE: The address in question is
# the address that mcrs1 has on the Partners network, and not the internal
# tcs network address. The syntax shown below is designed to prompt the
# operator for the appropriate username and password.

# Configure the display environmental variable on mcrs1 so that X applications
# can run on the workstation that has made the connection to mcrs1 setenv DISPLAY :0.0

# Execute the tcs application so that it appears on the workstation.
# Change to the directory where the executable is located, and then run it.
# NOTE: Do NOT include the '&' symbol at the end of the executable. The
# terminal window needs to be running in the background in order for TPR
# to work properly.

A Journey In The Quest Of Knowledge
Ivan Krastev
Honored Contributor

Re: Prompting a user for a password within a script

You can try another approach: use default logging facility in Linux and after user connect via telnet (he/she must enter correct username and password) and set default shell to desired script. After script completitions user will be logged off from the system.

dirk dierickx
Honored Contributor

Re: Prompting a user for a password within a script

if you are using FC2, why not just use SSH and passwordless logins using public keys and such. saves you a bunch of hassle for sure!
Alexander Chuzhoy
Honored Contributor

Re: Prompting a user for a password within a script

If you write your script in perl, then you could use the crypt function. Her's an example of implementation:
use strict;
my $pass=(getpwuid($<))[1]; # the $pass variable will hold the crypted password of the user executed this script
my $again=0;
my $passwd;
do {

if ($again == 1 ) {
print "Sorry! Wrong password. Try again.\n";
print "Please enter your password:\n";
system "stty -echo";
chomp $passwd;
system "stty echo";
} while (crypt($passwd,$pass) ne $pass);
print "You've got it!\n";