Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^15: expect.pm header

by sn1987a (Chaplain)
on Apr 08, 2015 at 20:18 UTC ( #1122850=note: print w/replies, xml ) Need Help??


in reply to Re^14: expect.pm header
in thread expect.pm header

You are getting the error because there is no semicolon at the end of the line

my $my_header = $header

That line should be in the change_password subroutine with the other ones I gave you. That subroutine should be:

sub change_password { my $system = shift; my $ssh = Expect->new('ssh amagana@' . $system); $ssh->debug(1); $ssh->log_file("$filename"); my $my_header = $header; ###### This line added <<<< $my_header =~ s/system/$system/; $ssh->print_log_file($header); $ssh->expect ( $timeout, [ qr/Password:/], [ qr/Are you sure you want to continue connecting \(yes\/no\)?/] ); if ($ssh->match() =~ m/Are you sure you want to continue connecting \( +yes\/no\)? +/ ) { $ssh->send("yes\r"); } elsif ($ssh->match() =~ m/Password:/ ) { $ssh->send("mycurrentpassword\n"); } $ssh->expect(60, '$'); $ssh->send("su - root\n"); $ssh->expect(60, 'Password:'); $ssh->send("rootpassword\n"); $ssh->expect(60, '#'); $ssh->send("hostname\n"); $ssh->expect(60, '#'); $ssh->send("uptime\n"); $ssh->expect(60, '#'); $ssh->send("passwd amagana\n"); $ssh->expect(60, 'New Password:'); $ssh->send("mynewpassword\n"); $ssh->expect(60, 'Re-enter new Password:'); $ssh->send("mynewpassword\n"); $ssh->expect(60, '#'); $ssh->send("exit\n"); $ssh->expect(60, '$'); $ssh->send("exit\n"); $ssh->close(); }

The idea is to make a local copy of the header in the subroutine, then change "system" to the actual system name. This way the original header text is unchanged and will be available for subsequent calls.

Replies are listed 'Best First'.
Re^16: expect.pm header
by amagana (Acolyte) on Apr 08, 2015 at 21:12 UTC

    My script is going but I do not see that the header changed to the hostname

    This is how I inserted the changes into my script

    #!/usr/bin/perl -w use warnings; use strict; use Expect; my $filename = "/var/tmp/expect_script.log"; my $timeout = 60; my @servers = qw( remotehost ); for my $server (@servers) { # do your thing with $server change_password($server); } sub change_password { my $system = shift; my $ssh = Expect->new('ssh amagana@' . $system); my $header = "\r\n\r\n======= system =======\r\n"; $ssh->debug(1); $ssh->log_file("$filename"); my $my_header = $header; $ssh->print_log_file($header); $my_header =~ s/system/$system/; $ssh->expect ( $timeout, [ qr/Password:/], [ qr/Are you sure you want to continue connecting \(yes\/no\)?/] ); if ($ssh->match() =~ m/Are you sure you want to continue connecting \( +yes\/no\)?/ ) { $ssh->send("yes\r"); } elsif ($ssh->match() =~ m/Password:/ ) { $ssh->send("mycurrentpassword^\n"); } $ssh->expect(60, '$'); $ssh->send("su - root\n"); $ssh->expect(60, 'Password:'); $ssh->send("rootpassword\n"); $ssh->expect(60, '#'); $ssh->send("hostname\n"); $ssh->expect(60, '#'); $ssh->send("uptime\n"); $ssh->expect(60, '#'); $ssh->send("passwd amagana\n"); $ssh->expect(60, 'New Password:'); $ssh->send("mynewpassword\n"); $ssh->expect(60, 'Re-enter new Password:'); $ssh->send("mynewpassword\n"); $ssh->expect(60, '#'); $ssh->send("exit\n"); $ssh->expect(60, '$'); $ssh->send("exit\n"); $ssh->close(); }

      There is an error in the code I provided, and you inserted the lines in a different order. The three lines for the headers in change_password should be:

      my $my_header = $header; $my_header =~ s/system/$system/; $ssh->print_log_file($my_header); # <<<<< this corrects the error in m +y previous post

      Use these three lines, in the order provided here

        Do I place the code you recommended inside  sub change_password ?

        Currently I am getting this error now

        perl tester-a.pl Global symbol "$system" requires explicit package name at tester-a.pl +line 27. Global symbol "$ssh" requires explicit package name at tester-a.pl lin +e 28. Global symbol "$system" requires explicit package name at tester-a.pl +line 30. Execution of tester-a.pl aborted due to compilation errors.
        #!/usr/bin/perl -w use warnings; use strict; use Expect; my $filename = "/var/tmp/expect_script.log"; my $header = "\r\n\r\n======= system =======\r\n"; my $timeout = 60; my @servers = qw( remotehost ); for my $server (@servers) { # do your thing with $server change_password($server); } sub change_password { my $my_header = $header; $my_header =~ s/system/$system/; $ssh->print_log_file($header); my $ssh = Expect->new('ssh amagana@' . $system); $ssh->debug(1); $ssh->log_file("$filename"); $ssh->expect ( $timeout, [ qr/Password:/], [ qr/Are you sure you want to continue connecting \(yes\/no\)?/] );

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1122850]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (7)
As of 2019-10-22 08:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?