Perl script stopped working


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl script stopped working
# 1  
Old 06-03-2014
Perl script stopped working

Hi,

I have the following segment of a script which is supposed to prompt a user for password and then capture the password entered by the user.

The function is called in by another script and used to work without issue, the problem is that recently the script is not waiting for the user to input the password it and immediately the calling script is giving an error that null password is provided.

Can someone please help?


Code:
sub prompt_for_password
{
  my $account_name = $_[0];
  my $account_description = $_[1];
  my $display_prompt = $_[2];

  if ($display_prompt)
  {
    print STDERR "\n";
    print STDERR "Please enter password for $account_description '$account_name': ";
  }

  $LINUX = -d "/initrd";
  $SOLARIS = -d "/kernel";

  if ($LINUX)
  {
    system "/bin/stty cbreak -echo </dev/tty >/dev/tty 2>&1";
  }

  if ($SOLARIS)
  {
    system "/bin/stty icanon -echo </dev/tty >/dev/tty 2>&1";
  }

  $password = <STDIN>;

  if ($LINUX)
  {
    system "/bin/stty cbreak echo </dev/tty >/dev/tty 2>&1";
  }

  if ($SOLARIS)
  {
    system "/bin/stty icanon echo </dev/tty >/dev/tty 2>&1";
  }

  if ($display_prompt)
  {
    print STDERR "\n";
  }

  chomp($password);

  return $password;
}

# 2  
Old 06-03-2014
Quote:
Originally Posted by belalr
Hi,

I have the following segment of a script which is supposed to prompt a user for password and then capture the password entered by the user.

The function is called in by another script and used to work without issue, the problem is that recently the script is not waiting for the user to input the password it and immediately the calling script is giving an error that null password is provided.

Can someone please help?
The reason must be that a "\n" or ENTER key is still in the stdin buffer by the time that it gets to $password = <STDIN>; which then it gets removed by chomp($password); returning null
# 3  
Old 06-03-2014
Thanks for your reply.

I don't understand how the script know that password in entered in the first place.

as of now the script doesn't wait for password or anything, it just immediately give the error without allowing user to enter any character.

Please advise.
# 4  
Old 06-03-2014
Quote:
Originally Posted by belalr
Thanks for your reply.

I don't understand how the script know that password in entered in the first place.
[...]
Look what happens depending of how the script is called and what's in the STDIN

This is your subroutine in its own script, with minor modifications to display

Code:
#!/usr/bin/perl

# script: pass.pl

use strict;
use warnings;

sub prompt_for_password
{
  my $account_name = $_[0];
  my $account_description = $_[1];
  my $display_prompt = $_[2];

  if ($display_prompt)
  {
    print STDERR "\n";
    print STDERR "Please enter password for $account_description \'$account_name\': ";
  }

  my $LINUX = -d "/initrd";
  my $SOLARIS = -d "/kernel";

  if ($LINUX)
  {
    system "/bin/stty cbreak -echo </dev/tty >/dev/tty 2>&1";
  }

  if ($SOLARIS)
  {
    system "/bin/stty icanon -echo </dev/tty >/dev/tty 2>&1";
  }

  my $password = <STDIN>;

  if ($LINUX)
  {
    system "/bin/stty cbreak echo </dev/tty >/dev/tty 2>&1";
  }

  if ($SOLARIS)
  {
    system "/bin/stty icanon echo </dev/tty >/dev/tty 2>&1";
  }

  if ($display_prompt)
  {
    print STDERR "\n";
  }

  chomp($password);

  return $password;
}


my $pass = prompt_for_password("My First Account", "no description", "What prompt");
print ">>>$pass<<<\n";

my $pass = prompt_for_password("My Second Account", "no description", "What prompt");
print ">>>$pass<<<\n";

Code:
chmod +x pass.pl

call it normally first and observe the output

Code:
./pass.pl

call it now with something in STDIN
Code:
echo -e "screw the user\n" | ./pass.pl

call it now with the equivalent of ENTER in the STDIN
Code:
echo -e "\n" | ./pass.pl


Last edited by Aia; 06-03-2014 at 01:49 PM.. Reason: Adding some clarifications
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Solaris

Mailx stopped working

after a new patch set on the 14th. I noticed that mailx stopped working, as far a I can tell that is the only thing that changed. solaris 10 OS it seems everything is the same, sendmail seems to be running root@server # ps -ef | grep -i sendmail smmsp 687 1 0 10:42:25 ? ... (0 Replies)
Discussion started by: goya
0 Replies

2. AIX

Printer stopped working

I have a serial printer connected to a 16 port ran. All of a sudden my printer stopped working and not sure how to get it back. Can anyone help pint me in the write direction? lsdev -Cc printer gives me the following. lp0 Defined 0A-08-11-00 Other serial printer lp1 Defined 0A-08-11-03... (3 Replies)
Discussion started by: Gmanx
3 Replies

3. UNIX for Dummies Questions & Answers

Ssh stopped working AIX

I Was able to ssh into the AIX box. now i cannot When I run the command to start it it comes back that is was started, but still does not work. Here is a shot i what i see # server:/> lslpp -l | grep ssh openssh.base.client 4.3.0.5201 COMMITTED Open Secure Shell Commands ... (2 Replies)
Discussion started by: fierfek
2 Replies

4. Ubuntu

Ubuntu 10.10 LAN connection stopped working...

Well this is weird. I restarted my dual boot Win7/Ubuntu 10.10 from ubuntu to windows. Everything was working fine and windows is always connecting properly to my lan. After restarting back into Ubuntu, all of a sudden I can't connect to my network. It looks as if its trying to connect through... (15 Replies)
Discussion started by: zixzix01
15 Replies

5. IP Networking

Wireless stopped working- Fedora 12

I installed F12 around the time it was released and it picked up my wireless card and worked like a charm.....Suddenly last week everything stopped working and I receive what appears to be a driver error when wlan0 tries to load. Error for wireless request "Set Mode" (8B06) : SET failed on... (1 Reply)
Discussion started by: woodson2
1 Replies

6. UNIX for Advanced & Expert Users

suPHP stopped working after dist-upgrade

Hi all, My server was Debian Etch (4) and had a working suPHP module (version 0.6.2-1). After I dist-upgraded it to Lenny (Debian 5), suPHP (version 0.6.2-3) stopped working. I read in the mailing list that I should change the settings of /etc/suphp/suphp.conf to this form: ;Handler... (1 Reply)
Discussion started by: mjdousti
1 Replies

7. Shell Programming and Scripting

cronjobs stopped working

Hello people, I had these cronjobs scheduled in some Unix boxes which were running fine until yesterday.But then the password was changed for that user id and then the jobs stopped working. As far as i know cron jobs run from super user. I am completely lost over here now. Thanks. (2 Replies)
Discussion started by: King Nothing
2 Replies

8. Linux

Cronjobs stopped working

Hi All, I am user of a Linux machine and I have approximatly 15 cronjobs scheduled in my crontab. Yesterday my administrator made LDAP active on my userid and all the things are doing fine after that. But all cronjobs for my user id stored in my crontab have stopped working after that. Could... (1 Reply)
Discussion started by: bisla.yogender
1 Replies

9. Shell Programming and Scripting

#!/bin/bash has stopped working

Hi I'm writing a script and I've put #!/bin/bash as the first line so that I can just type my scripts name 'whodate' at PS1 instead of ./whodate. This has suddenly stopped working for me. It used to be the case that I could start a script with #!/bin/bash and it would work, but for this script... (2 Replies)
Discussion started by: zorrokan
2 Replies
Login or Register to Ask a Question