Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^14: expect.pm header

by amagana (Acolyte)
on Apr 08, 2015 at 19:54 UTC ( #1122847=note: print w/replies, xml ) Need Help??


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

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

Replies are listed 'Best First'.
Re^15: expect.pm header
by sn1987a (Chaplain) on Apr 08, 2015 at 20:18 UTC

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

        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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2019-05-22 03:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you enjoy 3D movies?



    Results (138 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!