Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
PerlMonks header

by amagana (Acolyte)
on Apr 06, 2015 at 20:32 UTC ( #1122604=perlquestion: print w/replies, xml ) Need Help??

amagana has asked for the wisdom of the Perl Monks concerning the following question:

I am needing help with trying to insert a header for each server that gets the password changed in this script. I think I need a log file to identify where the script failed. But I am not able to get a header as recommended. I am getting this error: Global symbol "$system" requires explicit package name at ./tester-a line 7. Execution of ./tester-a aborted due to compilation errors. I am trying to insert a header into a log file for each server but I am just not able to get it to work,
#!/usr/bin/perl use strict; use Expect; my $timeout = 60; my $header = "\n\n======= $system =======\n"; my @servers = qw( Solaris-host Solaris-host Solaris-host Solaris-host ); for my $server (@servers) { # do your thing with $server change_password($server); } sub change_password { my $system = shift; my $filename = "/var/tmp/expect_script.log"; my $ssh = Expect->new('ssh amagana@' . $system); $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->print_log_file($header); $ssh->send("su - root\n"); $ssh->expect(60, 'Password:'); $ssh->send("rootpassword\n"); $ssh->expect(60, '#'); $ssh->send("passwd amagana\n"); $ssh->expect(60, 'New Password:'); $ssh->send("mynewpassword\n"); $ssh->expect(60, 'Re-enter new Password:'); $ssh->send("mynewpassword\n"); $ssh->expect(60, '#'); $ssh->close(); }

Replies are listed 'Best First'.
Re: header
by roboticus (Chancellor) on Apr 06, 2015 at 20:42 UTC


    The error message says "line 7", and on line 7 you have a variable ($system) that has never had a value in it. Delete that line, and you should be able to move further in.


    When your only tool is a hammer, all problems look like your thumb.

      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,

        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
Re: header
by stevieb (Abbot) on Apr 06, 2015 at 20:47 UTC

    $system, used in your header way at the top is never defined at all, let alone before it is used.

    You need to re-evaluate how you are doing things. It looks like you want to create a header down after you define @systems, then iterate over that through each one and use something like



    use warnings;

    ...might have been useful, but I didn't test. Always use it.


      You are right, I am a novice and need help if you could give me an example, I would be greatful.

        Just do what our friend roboticus said in his above post :)


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1122604]
Approved by marinersk
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2019-05-20 21:35 GMT
Find Nodes?
    Voting Booth?
    Do you enjoy 3D movies?

    Results (129 votes). Check out past polls.