#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0

    How for restarting the script itself


    Hi,

    I writed a script with the Expect perl Module for changing a password. I would like the script restarts ifself if the old password is wrong. Actually, if the old password is wrong the script die, as well :
    Code:
    #!/usr/bin/perl -w  
    use strict;
    use autodie;
    use Expect; 
    
    my $user= getpwuid( $< );
    print "Enter your old password:" ;
    my $oldpassword = <STDIN>;
    chomp($oldpassword);
    
    print "Enter your new password :";
    my $newpassword = <STDIN>;
    chomp($newpassword);
    
    print "Running ' passwd ${user}'\n"; 
    my $exp = Expect->spawn("passwd") or die "Can t acces to passwd \n"; 
    unless ($exp->expect(1, "Enter login\(LDAP\) password:")) {};
    print $exp "${oldpassword}\r" ;
    if ($exp->expect(1,"LDAP Password incorrect\: try again")) {
    die "wrong Old passwd \n"; 
    
    };
    unless ($exp->expect(1, "New password:")) {} ;
    print $exp "${newpassword}\r" ;
    unless ($exp->expect(1, "Re-enter new password:")) {} ;
    print $exp "${newpassword}\r" ;
    $exp->soft_close();
    I don't want to use perl LDAP module because is not openldap
    Thanks for your help.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    4
    Rep Power
    0
    Try something like this instead:

    Code:
    #!/usr/bin/perl
    use strict;
    use warnings;
    use Expect;
    
    my $user = getpwuid($<);
    
    system("stty -echo");
    print "Enter your old Password: ";
    my $oldpassword = <STDIN>;
    chomp($oldpassword);
    print "\n";
    system("stty echo");
    
    my $newpassword = my $confirm = undef;
    do	{
    	system("stty -echo");
    	print "Enter your new Password: ";
    	$newpassword = <STDIN>;
    	chomp($newpassword);
    	print "\nConfirm your new Password: ";
    	$confirm = <STDIN>;
    	chomp($confirm);
    	print "\n";
    	system("stty echo");
    } until ($newpassword eq $confirm);
    
    my $passwd = Expect->spawn("passwd $user")
    	or die "Cannot spawn passwd $user: $!\n";
    
    my $pwsent = 0;
    
    $passwd->expect(15,
    
    	[ /[>\$]\s*$/ => sub {
    		print "prompt seen\n";
    	} ],
    
    	[ "[pP]assword" => sub {
    		$pwsent++;
    		if ($pwsent == 1) {
    			sleep(0.5);
    			$passwd->send($oldpassword."\n");
    			print STDERR "old password sent";
    			exp_continue;
    		} elsif ($pwsent <= 3) {
    			sleep(0.5);
    			$passwd->send($newpassword."\n");
    			print STDERR "new password sent";
    			exp_continue;
    		} else	{
    			sleep(0.5);
    			$passwd->hard_close();
    		}
    	} ],
    
    	[ eof => sub {
    		sleep(0.5);
    		print "passwd command ended\n";
    	} ],
    
    	[ timeout => sub {
    		print "passwd command timed out\n";
    	} ],
    
    );
    $passwd->hard_close();
    
    exit(0);
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0
    Thanks for your answer whistl034,

    Sorry, I'm not familiar with the object structure ....

    In the script could you explain this line :
    Code:
    [ /[>\$]\s*$/ => sub {
    Why didn't you use this syntax :
    Code:
    [qr 'Enter login\(LDAP\) password:' , 
     		sub {

    I launched your script and there is the prompt when the old password is wrong:
    Code:
    :~$ ./chg_ldap_passwd.pl 
    Enter your old Password: 
    Enter your new Password: 
    Confirm your new Password: 
    Use of uninitialized value $_ in pattern match (m//) at ./chg_ldap_passwd.pl line 60, <STDIN> line 3.
    Enter login(LDAP) password: old password sent
    LDAP Password incorrect: try again
    Enter login(LDAP) password: new password sentnew password sent
    New password: user@ldapclient:~$
    user@ldapclient:~$
    user@ldapclient:~$
    user@ldapclient:~$
    When the old password is right there is no problem.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    4
    Rep Power
    0
    Originally Posted by dubis
    Thanks for your answer whistl034,

    Sorry, I'm not familiar with the object structure ....

    In the script could you explain this line :
    Code:
    [ /[>\$]\s*$/ => sub {
    That line searches for a UNIX command prompt, which is normally a line that ends with a dollar sign($) or a greater than sign(>). Actually, that part can be removed, since you're calling the passwd command directly. I tend to use Perl Expect to call SSH to login to remote machine, and that line is useful there, but not here.

    Why didn't you use this syntax :
    Code:
    [qr 'Enter login\(LDAP\) password:' , 
     		sub {
    You could, but you don't need to match the whole string, and some systems don't use LDAP, so they get a different prompt string, which still ends in [Pp]assword. I always try to write as generic code as possible, because I learned Perl in a shop with 23 different flavors of UNIX, and my scripts had to run everywhere.

    You are correct, I didn't catch the password incorrect condition. Let's try again.

    Code:
    #!/usr/bin/perl
    use strict;
    use warnings;
    use Expect;
    
    my $user = getpwuid($<);
    
    system("stty -echo");
    print "Enter your old Password: ";
    my $oldpassword = <STDIN>;
    chomp($oldpassword);
    print "\n";
    system("stty echo");
    
    my $newpassword = my $confirm = undef;
    system("stty -echo");
    do	{
    	print "Enter your new Password: ";
    	$newpassword = <STDIN>;
    	chomp($newpassword);
    	print "\nConfirm your new Password: ";
    	$confirm = <STDIN>;
    	chomp($confirm);
    	print "\n";
    } until ($newpassword eq $confirm);
    system("stty echo");
    
    my $passwd = Expect->spawn("passwd $user")
    	or die "Cannot spawn passwd $user: $!\n";
    
    my $pwsent = 0;
    
    $passwd->expect(15,
    
    	[ /try again/ => sub {
    		print "incorrect password\n";
    	} ],
    
    	[ "[nN]ew password" => sub {
    		$pwsent++;
    		if ($pwsent <= 3) {
    			sleep(0.5);
    			$passwd->send($newpassword."\n");
    			print STDERR "\nnew password sent\n";
    			exp_continue;
    		} else	{
    			$passwd->hard_close();
    		}
    	} ],
    
    	[ "[pP]assword" => sub {
    		$pwsent++;
    		if ($pwsent == 1) {
    			sleep(0.5);
    			$passwd->send($oldpassword."\n");
    			print STDERR "\nold password sent\n";
    			exp_continue;
    		} else	{
    			$passwd->hard_close();
    		}
    	} ],
    
    	[ eof => sub {
                    if ($pwsent == 3) {
                      print "password changed\n";
                    } else {
                      print "passwd command ended\n";
                    }
    	} ],
    
    	[ timeout => sub {
    		print "passwd command timed out\n";
    	} ],
    
    );
    $passwd->hard_close();
    exit(0);
    In this style of Expect, the single call to Expect will run in a loop, processing all of the output of the passwd command, until one of several conditions occurs (the command exits, timeout, incorrect old password). Any section that doesn't end with "exp_continue;" will cause the Expect loop to terminate.

    The sections are compared in order, so New password will match before password, when that prompt comes up.

    Hope this helps some.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0
    Thanks whistl034

    I understand a few more How the Expect Module works
    and I'm able to correct this following line of your last version:
    Code:
    [ /try again/ => sub {
    by this one :
    Code:
     [ "try again" => sub {
    and the last coma of the last following syntax
    Code:
    } ],
    carries out the previous warning message.

    That's helping me a lot because I put the code in a GTK object and I don't have to restart any thing .... so Thanks

IMN logo majestic logo threadwatch logo seochat tools logo