Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^7: expect.pm header

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


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

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:~$

Replies are listed 'Best First'.
Re^8: expect.pm header
by amagana (Acolyte) on Apr 08, 2015 at 15:42 UTC
    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.

        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\)?/] );

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2019-04-18 16:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I am most likely to install a new module from CPAN if:
















    Results (103 votes). Check out past polls.

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