Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^8: expect.pm header

by amagana (Acolyte)
on Apr 08, 2015 at 15:42 UTC ( [id://1122814]=note: print w/replies, xml ) Need Help??


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

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?

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

    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.

      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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-04-25 07:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found