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

I tried to use the $ssh->print_log_file($header); and got a bunch of ^M characters in my log.

M ^[[01m[root@host ~ ]^[[m # hostname^M prdoradb03^M ^[[01m[root@host ~ ]^[[m # uptime^M 3:15pm up 9 day(s), 15:59, 3 users, load average: 5.04, 5.59, 5.3 +2^M ^[[01m[root@host ~ ]^[[m # passwd amagana^M New Password: ^M Re-enter new Password: ^M passwd: password successfully changed for amagana^M ^[[01m[root@host ~ ]^[[m # exit^M logout^M $
$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\)?/] );

Replies are listed 'Best First'.
Re^7: expect.pm header
by sn1987a (Curate) on Apr 08, 2015 at 04:02 UTC

    It worked for my. I have modified you program to work in my environment, but the core is still the same. Note how I made a local copy of the header before doing the substitution so the original is available for the other servers

    Progam:
    #!/usr/bin/env perl use strict; use Expect; my $header_template = "\n\n======== system ========\n"; do_date($_) for qw( vftp vwww2 ); sub do_date { my ($system) = @_; my $header = $header_template; $header =~ s/system/$system/; my $timeout = 10; my $ssh = Expect->new("ssh $system"); $ssh->log_file("$system.log"); $ssh->debug(1); $ssh->print_log_file($header); $ssh->expect( $timeout, [qr/password/] ); if ( $ssh->match() =~ m/password/ ) { $ssh->send("youcantseeme\n"); } $ssh->expect( 60, [qr/\$\s*/] ); $ssh->send("hostname\n"); $ssh->expect( 60, [qr/\$\s*/] ); $ssh->send("date\n"); $ssh->expect( 60, [qr/\$\s*/] ); $ssh->send("exit\n"); $ssh->close(); }


    vftp.log:

    ======== vftp ======== jwk@vftp's password: Welcome to Ubuntu 12.04.5 LTS (GNU/Linux 3.13.0-48-generic x86_64) Last login: Tue Apr 7 22:48:33 2015 from 10.1.1.134 jwk@vftp:~$ hostname vftp jwk@vftp:~$ date Tue Apr 7 22:53:09 CDT 2015 jwk@vftp:~$


    vwww2.log:

    ======== vwww2 ======== jwk@vwww2's password: Welcome to Ubuntu 14.04.2 LTS (GNU/Linux 3.13.0-48-generic x86_64) Last login: Tue Apr 7 22:48:35 2015 from 10.1.1.134 jwk@vwww2:~$ hostname vwww2 jwk@vwww2:~$ date Tue Apr 7 22:53:10 CDT 2015 jwk@vwww2:~$

      Thank you for your example I am still trying to avoid getting the ^M characters in my log also I have not figured out where to place the
      $header =~ s/system/$system/;
      inside my script. I am learning as I am blowing up my script. Could you give a illustration with my script on how to get my header to work inside my log file?

        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.