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


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

Thank you I am very humbled for your help its very appreciated. I have my script pointing to one remote solaris 10 server for testing and this is what I see first in debug turned on to my screen and after that this is what I see in my log file.

First is my output to my screen from debugging.

# perl tester-a.pl Starting EXPECT pattern matching... at /usr/lib/perl5/site_perl/5.8.8/Expect.pm line 561 Expect::expect('Expect=GLOB(0x1a317160)', 60, 'ARRAY(0x1a31777 +0)', 'ARRAY(0x1a3177c0)') called at tester-a.pl line 32 main::change_password('solarishost') called at tester-a.pl lin +e 20 Password: Starting EXPECT pattern matching... at /usr/lib/perl5/site_perl/5.8.8/Expect.pm line 561 Expect::expect('Expect=GLOB(0x1a317160)', 60, '$') called at t +ester-a.pl line 48 main::change_password('solarishost') called at tester-a.pl lin +e 20 <code> Last login: Wed Apr 8 10:32:54 2015 from mycomputer. $ Starting EXPECT pattern matching... at /usr/lib/perl5/site_perl/5.8.8/Expect.pm line 561 Expect::expect('Expect=GLOB(0x1a317160)', 60, 'Password:') cal +led at tester-a.pl line 50 main::change_password('solarishost') called at tester-a.pl lin +e 20 su - root Password: Starting EXPECT pattern matching... at /usr/lib/perl5/site_perl/5.8.8/Expect.pm line 561 Expect::expect('Expect=GLOB(0x1a317160)', 60, '#') called at t +ester-a.pl line 52 main::change_password('solarishost') called at tester-a.pl lin +e 20 [root@solarishost ~ ] # Starting EXPECT pattern matching... at /usr/lib/perl5/site_perl/5.8.8/Expect.pm line 561 Expect::expect('Expect=GLOB(0x1a317160)', 60, '#') called at t +ester-a.pl line 54 main::change_password('solarishost') called at tester-a.pl lin +e 20 hostname solarishost [root@solarishost ~ ] # Starting EXPECT pattern matching... at /usr/lib/perl5/site_perl/5.8.8/Expect.pm line 561 Expect::expect('Expect=GLOB(0x1a317160)', 60, '#') called at t +ester-a.pl line 56 main::change_password('solarishost') called at tester-a.pl lin +e 20 uptime 1:59pm up 10 day(s), 14:43, 4 users, load average: 5.19, 5.16, 4. +90 [root@solarishost ~ ] # Starting EXPECT pattern matching... at /usr/lib/perl5/site_perl/5.8.8/Expect.pm line 561 Expect::expect('Expect=GLOB(0x1a317160)', 60, 'New Password:') + called at tester-a.pl line 58 main::change_password('solarishost') called at tester-a.pl lin +e 20 passwd amagana New Password: Starting EXPECT pattern matching... at /usr/lib/perl5/site_perl/5.8.8/Expect.pm line 561 Expect::expect('Expect=GLOB(0x1a317160)', 60, 'Re-enter new Pa +ssword:') called at tester-a.pl line 60 main::change_password('solarishost') called at tester-a.pl lin +e 20 Re-enter new Password: Starting EXPECT pattern matching... at /usr/lib/perl5/site_perl/5.8.8/Expect.pm line 561 Expect::expect('Expect=GLOB(0x1a317160)', 60, '#') called at t +ester-a.pl line 62 main::change_password('solarishost') called at tester-a.pl lin +e 20 passwd: password successfully changed for amagana [root@solarishost ~ ] # Starting EXPECT pattern matching... at /usr/lib/perl5/site_perl/5.8.8/Expect.pm line 561 Expect::expect('Expect=GLOB(0x1a317160)', 60, '$') called at t +ester-a.pl line 64 main::change_password('solarishost') called at tester-a.pl lin +e 20 exit logout $ Closing spawn id(3). at /usr/lib/perl5/site_perl/5.8.8/Expect.pm line 1431 Expect::hard_close('Expect=GLOB(0x1a317160)') called at /usr/l +ib/perl5/site_perl/5.8.8/Expect.pm line 1621 Expect::DESTROY('Expect=GLOB(0x1a317160)') called at tester-a. +pl line 20 eval {...} called at tester-a.pl line 20 main::change_password('solarishost') called at tester-a.pl lin +e 20 Pid 4754 of spawn id(3) terminated, Status: 0xFF00

This is my log file results

vi /var/tmp/expect_script.log ======= system ======= ^M You are accessing a U.S. Government (USG) Information System (IS) th +at is ^M provided for USG-authorized use only. + ^M + ^M By using this IS (which includes any device attached to this IS), yo +u consent^M to the following conditions: + ^M + ^M -The USG routinely intercepts and monitors communications on this IS +for ^M purposes including, but not limited to, penetration testing, COMSEC + ^M monitoring, network operations and defense, personnel misconduct (PM +), law ^M enforcement (LE), and counterintelligence (CI) investigations. + ^M + ^M -At any time, the USG may inspect and seize data stored on this IS. + ^M + ^M -Communications using, or data stored on, this IS are not private, ar +e ^M subject to routine monitoring, interception, and search, and may be + ^M disclosed or used for any USG authorized purpose. + ^M + ^M -This IS includes security measures (e.g.,authentication and access c +ontrols)^M to protect USG interests--not for your personal benefit or privacy. + ^M + ^M -Notwithstanding the above, using this IS does not constitute consent + to PM, ^M LE or CI investigative searching or monitoring of the content of pri +vileged ^M communications, or work product, related to personal representation +or ^M services by attorneys, psychotherapists, or clergy, and their assist +ants. ^M Such communications and work product are private and confidential.^M Password: ^M Last login: Wed Apr 8 10:32:54 2015 from mycomputer.^M^M +--------------------------------------------------------------------- +--------+^M |You are accessing a U.S. Government (USG) Information System (IS) tha +t is |^M |provided for USG-authorized use only. + |^M | + |^M |By using this IS (which includes any device attached to this IS), you + consent|^M |to the following conditions: + |^M | + |^M |-The USG routinely intercepts and monitors communications on this IS +for |^M | purposes including, but not limited to, penetration testing, COMSEC + |^M | monitoring, network operations and defense, personnel misconduct (PM +), law |^M | enforcement (LE), and counterintelligence (CI) investigations. + |^M | + |^M |-At any time, the USG may inspect and seize data stored on this IS. + |^M | + |^M |-Communications using, or data stored on, this IS are not private, ar +e |^M | subject to routine monitoring, interception, and search, and may be + |^M | disclosed or used for any USG authorized purpose. + |^M | + |^M |-This IS includes security measures (e.g.,authentication and access c +ontrols)|^M | to protect USG interests--not for your personal benefit or privacy. + |^M | + |^M |-Notwithstanding the above, using this IS does not constitute consent + to PM, |^M | LE or CI investigative searching or monitoring of the content of pri +vileged |^M | communications, or work product, related to personal representation +or |^M | services by attorneys, psychotherapists, or clergy, and their assist +ants. |^M | Such communications and work product are private and confidential. + |^M +--------------------------------------------------------------------- +--------+^M $ su - root^M Password: ^M +--------------------------------------------------------------------- +--------+^M |You are accessing a U.S. Government (USG) Information System (IS) tha +t is |^M |provided for USG-authorized use only. + |^M | + |^M |By using this IS (which includes any device attached to this IS), you + consent|^M |to the following conditions: + |^M | + |^M |-The USG routinely intercepts and monitors communications on this IS +for |^M | purposes including, but not limited to, penetration testing, COMSEC + |^M | monitoring, network operations and defense, personnel misconduct (PM +), law |^M | enforcement (LE), and counterintelligence (CI) investigations. + |^M | + |^M |-At any time, the USG may inspect and seize data stored on this IS. + |^M | + |^M |-Communications using, or data stored on, this IS are not private, ar +e |^M | subject to routine monitoring, interception, and search, and may be + |^M | disclosed or used for any USG authorized purpose. + |^M | + |^M |-This IS includes security measures (e.g.,authentication and access c +ontrols)|^M | to protect USG interests--not for your personal benefit or privacy. + |^M | + |^M |-Notwithstanding the above, using this IS does not constitute consent + to PM, |^M | LE or CI investigative searching or monitoring of the content of pri +vileged |^M | communications, or work product, related to personal representation +or |^M | services by attorneys, psychotherapists, or clergy, and their assist +ants. |^M | Such communications and work product are private and confidential. + |^M +--------------------------------------------------------------------- +--------+^M ^[[01m[root@solarishost ~ ]^[[m # hostname^M solarishost^M ^[[01m[root@solarishost ~ ]^[[m # uptime^M 1:59pm up 10 day(s), 14:43, 4 users, load average: 5.19, 5.16, 4. +90^M ^[[01m[root@solarishost ~ ]^[[m # passwd amagana^M New Password: ^M Re-enter new Password: ^M passwd: password successfully changed for amagana^M ^[[01m[root@solarishost ~ ]^[[m # exit^M logout^M $

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

    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.

      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.