-1

I was using this in the perl script, where it is login to a couple of servers and trying to change the password for the servers through a remote host. But the problem is that the password is not getting changed on both the servers as well as i am not finding a way to check if the new passwords are passed to the servers using expect. i am posting that part of the code where it is checking for the prompt and trying to change the password.

     #!/usr/bin/perl
package Session;
use strict;
use warnings;
use Expect;
use IO::Pty;
use Data::Dumper;
use Time::HiRes qw(usleep);
use Switch;
use YAML;
use feature 'say';

my $host1 = $ARGV[0];
my $host2 = $ARGV[1];
my $host1_adapter_name = $ARGV[2];
my $host2_adapter_name = $ARGV[3];

my $exp = Expect->new;
my ($selfObj) = @_;
my $str = "{$host1}\{root} # ";
my $cmdStr; my $result; my $dev_id; 
my $timeout = 10;
my $min = 192;
my $range = 32;


my $host1_dev_id = _adapter($host1_adapter_name);
my $host2_dev_id = _adapter($host2_adapter_name);
my @hosts = ("$host1", "$host2");
print ("host2 name is=$host2------");
foreach my $n (@hosts)
{
    print ("value of n is $n\n");
    if ( $n eq $host1 )
    {
        _login($n,$host1_dev_id);
    }
        if ( $n eq $host2)
        {
            print ("inside 2nd if-----\n");
                _login($n,$host2_dev_id);
        }

}  

sub _login
{
    my ($host,$host_dev_id) = @_;
    my $exit = 1;
    $exp->raw_pty(1);
    $exp = Expect->spawn("telnet $host") or die "unable to connect , Please check the connection & retry again: $!\n";

    if (!defined($exp))
    {
        print "Please check the connection & retry again\n";
            return -1;
    }

    `sleep 2 `;

    $exp->expect($timeout,
    [
        qr'[lL]ogin:[\s]?[a]?[s]?[\s]?$',
            sub
            {
            $exp->send("root\r");
            `sleep 3 `;
            exp_continue;
        }
        ],
        [
            qr'[pP]assword:[\s]?$',
            sub
            {
            $exp->send("That11NeverWork!\r");
            exp_continue;
            }
        ],

        [
            qr '[#>:][\s]?$',
            sub {

            $cmdStr = "passwd\r";
            $result =_run_cmd($cmdStr);

            qr'root\'s New password:\s*'; 
            $exp->send("raym0nd24");

            qr'Enter the new password again:\s*';
            $exp->send("raym0nd24");
           # $exp->send('passwd:\s*',5);

            $exit = 0;
                exp_continue;

    }
],
[
        eof =>
        sub
        {
            print("FileName : Session.pm , ERROR: premature EOF in command execution.\n");
        }
],
       '-re', qr'[#$>:]\s?$', # wait for shell prompt, then exit expect
);
}



#############################################################
#############################################################################
sub _adapter
{
        my ($adapter_name) = @_;
        print "Adapter name: $adapter_name\n";
        chomp($adapter_name);
        switch($adapter_name){
                case "AUSTIN"             {$dev_id="e414571614102004"}
                case "CX5"                {$dev_id="b315191014103506"}
                case "CX4"                {$dev_id="b31513101410f704"}
                case "CX4_EG10"           {$dev_id="b315151014101f06"}
                case "CX4_EG25"           {$dev_id="b315151014101e06"}
                case "CX3"                {$dev_id="RoCE"}
                case "CX2"                {$dev_id="b315506714106104"}
                case "CX3_PRO"              {$dev_id="RoCE"}
                case "CX3_PRO1"            {$dev_id="b31507101410e704"}
                case "HOUSTON_LR"         {$dev_id="df1020e214104004"}
                case "HOUSTON_SR"         {$dev_id="df1020e214100f04"}
                case "HOUSTON_Cu"         {$dev_id="df1020e214103d04"}
                case "SHINER_S"           {$dev_id="e4148a1614109304"}
                case "SHINER_T"           {$dev_id="e4148e1614109204"}
                case "SLATE_SR"           {$dev_id="df1020e21410e304"}
                case "SLATE_CU"           {$dev_id="df1020e21410e404"}
                case "EVERGLADES"         {$dev_id="b315151014101e06"}
                else              { print "Adapter not in list\n"}
        }
        return $dev_id;
}

#######################################################################################
##########################################################
sub _run_cmd
{
    my $output; my $output1;
    my ($cmdStr) = @_;
    $exp->send($cmdStr ."\r");
    $exp->expect(21, '-re', $str);
    $output = $exp->exp_before();
    $exp->clear_accum();
        my @PdAt_val = split("\r?\n", $output);
        foreach my $line1 (@PdAt_val)
        {
                chomp($line1);
                if ( $line1 =~ /(\(\d+\))(\s*root\s*\@\s*)/)
                {
                        if ( $1 =~ /\((\d+)\)/)
                        {
                                if ($1 != 0)
                                {
                                        print("*************** Command $cmdStr didn't ran sucessfully ***************\n");
                                        exit;
                                }
                        }
                }
       }

    return $output;
}



######################################################################
ray
  • 59
  • 7
  • 1
    This `qr` expression is not assigned to anything: `qr'root\'s New password:\s*';`. So it seems like useless use of `qr` in a void context. Please show more of your code. – Håkon Hægland Sep 03 '19 at 12:07
  • I have added the code – ray Sep 04 '19 at 06:37
  • 1
    I would strongly recommend against using `Switch`. It's a source filter. – melpomene Sep 04 '19 at 06:40
  • You have indeed random `qr` expressions just sitting around in your program. It's like writing `"hello"; "how are you doing today?";`. It's not doing anything. – melpomene Sep 04 '19 at 06:42
  • 1
    You have an anonymous sub with `qr//` expressions that create Regexp objects, but in void context, so they're useless. `qr'root\'s New password:\s*';` doesn't do anything, the regexp object isn't handed off to anything. This happens again a few lines below. – DavidO Sep 04 '19 at 14:45

1 Answers1

0

There are individual solutions for different systems. So some systems got from their god the must have highlevel restrictions. So the regular says, that you cant login as root directly. To step up the long way to the stage you can use sudo or su. I didnt see that mind in your lines.

# The simpliest way is to use what you have!
sub passwd 
{  
   my $user = @_[0];
   my $password = @_[1];
   #
   # as root 
   my $execline = qq~passwd $user:$password~;
   #
   # as root with second password
   my $execline = qq~passwd $user:$password\n$password~;
   #
   # for microsoft certified ubuntu noobs, kidding mint's
   my $execline = qq~sudo $password && passwd $user:$password~;
   #
   # for apple greyed, debian nerds, solaris freaks 
   my $execline = qq~su $password && passwd $user:$password~;
   #
   my $return = system("$execline");
}

print &passwd("root","the magical word");
#
# elseif read this.url([to get the higher experience][1]);
[1]: https://stackoverflow.com/questions/714915/using-the-passwd-command-from-within-a-shell-script