Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re^2: header

by amagana (Acolyte)
on Apr 07, 2015 at 14:08 UTC ( #1122706=note: print w/replies, xml ) Need Help??

in reply to Re: header
in thread header

Hello roboticus, I really wanted to have a header but you are right the $system does not have a value assigned to it. Looking at my script how can I get $system to be in my log that corresponds to each server getting the password changed. That is what I am trying to do. Respectfully,

Replies are listed 'Best First'.
Re^3: header
by GotToBTru (Prior) on Apr 07, 2015 at 15:09 UTC

    You need to replace $system with a placeholder - it could be the word 'system' - and then, just before you print the header, use s/// to replace the placeholder with the value you want, which will be defined at that time.

    my $header = "\n\n======= system =======\n"; ... sub change_password { my $system = shift; $header =~ s/system/$system/; ...
    Dum Spiro Spero
      Thank you GotToBTru, I am missing the mark I have my script like this now but I still don't see a header in my log file just other information that was a result of the script in debug. For example:
      ^[[01m[root@remotehost ~ ]^[[m # hostname remotehost ^[[01m[root@remotehost ~ ]^[[m # uptime 1:03pm up 9 day(s), 13:47, 3 users, load average: 5.83, 5.33, 5.1 +3 ^[[01m[root@remotehost ~ ]^[[m # passwd amagana New Password: Re-enter new Password: passwd: password successfully changed for amagana ^[[01m[root@remotehost ~ ]^[[m # exit logout $
      My script is current like this now:
      #!/usr/bin/perl -w use warnings; use strict; use Expect; #my $system = shift; my $filename = "/var/tmp/expect_script.log"; my $header = "\n\n======= system =======\n"; my $timeout = 60; my @servers = qw( remotehost ); for my $server (@servers) { # do your thing with $server change_password($server); } sub change_password { my $system = shift; $header =~ s/system/$system/; #my $filename = "/var/tmp/expect_script.log"; my $ssh = Expect->new('ssh amagana@' . $system); $ssh->log_file($filename); $ssh->debug(1); $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("newpassword\n"); $ssh->expect(60, 'Re-enter new Password:'); $ssh->send("newpassword\n"); $ssh->expect(60, '#'); $ssh->send("exit\n"); $ssh->expect(60, '$'); $ssh->send("exit\n"); $ssh->close(); }

        Try inserting

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2020-03-29 06:25 GMT
Find Nodes?
    Voting Booth?
    To "Disagree to disagree" means to:

    Results (169 votes). Check out past polls.