Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^9: expect.pm header

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


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

I am not sure where the '^M' characters are coming from. They are a representation of the carriage return.

Are you showing the entire contents of the log file. What you have shown is only from the hostname on down. The header should be the first thing in the log. (If you are concerned about revealing to much information, just change any hostname/addresses/passwords you need to)

You have the replacement of "system" in the correct place. The problem occurs when you have more than one system. After the first call $header will no longer contain the string "system", and so the previous system name will remain in the header. The solution is to work with a local copy of the header.

my $my_header = $header; $my_header =~ s/system/$system/; my $ssh = Expect->new('ssh amagana@' . $system); $ssh->log_file($filename); $ssh->debug(1); $ssh->print_log_file($my_header); $ssh->expect ( $timeout, ...

Also note that by default Expect opens the log file for appending, so if you don't delete it and only look at the top of the file you will not see the new entries.

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

    I am still trying to understand how to put system and have header in the right place. Ideally what I am trying to achieve is this :

    ============hostname01============ bla bla bla ============hostname02============ bla bla bla ============hostname03============ bla bla bla
    #!/usr/bin/perl -w use warnings; use strict; use Expect; my $filename = "/var/tmp/expect_script.log"; my $header = "\n\n======= system =======\n"; my $timeout = 60; my @servers = qw( hostname01 hostname02 hostname03 hostname04 ); 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->log_file("$filename"); $ssh->debug(1); $ssh->print_log_file($header); $ssh->expect ( $timeout, [ qr/Password:/], [ qr/Are you sure you want to continue connecting \(yes\/no\)?/] );

      I believe I know what you are looking for. I am not sure what you are currently seeing. Once I know that, I can make a suggestion for how to move forward. With the code that you showed, I would expect that the log file contains:

      ============ system ============ bla bla bla ============ system ============ bla bla bla ============ system ============ bla bla bla

      Is that what you are currently seeing? If so, then I can definitely tell you how to correct the headers. If not, then we need to figure out why no headers are showing up in your log file.

        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 $

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2019-06-17 18:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Is there a future for codeless software?



    Results (80 votes). Check out past polls.

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