http://www.perlmonks.org?node_id=1122845


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

Thank you for the output. That makes things much clearer.

I suspect the ^M were always there, but since all of the lines had them, vi assumed DOS line endings and suppressed them. But, once you added the header with only the line feed, it thought it had unix line endings and showed all of the ^M's. If I am correct, the solution is to replace \n in the header with \r\n like this

my $header = "\r\n\r\n======= system =======\r\n";

To get the word "system" change to the actual system name in the log file, replace the line

$ssh->print_log_file($header);
with
my $my_header = $header; $my_header =~ s/system/$system/; $ssh->print_log_file($my_header);

I hope this helps. Let me know what happens.

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

    I put in my script as you suggested but I got an error, I am not sure where to place your suggestions in my script

    This is my error on my screen

    perl tester-a.pl syntax error at tester-a.pl line 12, near "$header my " Global symbol "@servers" requires explicit package name at tester-a.pl + line 12. Global symbol "@servers" requires explicit package name at tester-a.pl + line 18. Execution of tester-a.pl aborted due to compilation errors.

    For reference this how I placed it in my script your suggestions

    #!/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 $my_header = $header 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); $ssh->debug(1); $ssh->log_file("$filename"); $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(); }

      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.

        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(); }