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

Wondering if anyone can help with some of the code i am writing. My problem is that i need to reset the variable $oknodes back to zero for each "connection" in the array @connectcommand. This array contains ssh connections into a storage server and and counts the number of "OK" from the output of the command. My problem is that after logging into the second storage server the $oknodes variable is adding. E.g after the first storage server the expected output is "4" but after logging into the next one (which as 4 x OK's) $oknodes is then bumped to 8 and gets higher after each connection. I need to be able to reset the $oknodes variable after each ssh connection in the @connectcommand array. Is someone able to provide some insight into this? PS this is only snippets of the entire script so some things may look odd.

######################################### our $HOST1 = "somenode.domain.local"; our $PARNODE1 = "4"; ######################################### our $HOST2 = "somenode1.domain.local"; our $PARNODE2 = "4"; ######################################### my @CONNECTCOMMANDS; if ( length $HOST1 > 0 ) { my $CONNECTCOMMAND1 = "plink.exe -ssh USERNAME\@$HOST1 + -pw PASSWORD"; push (@CONNECTCOMMANDS, "$CONNECTCOMMAND1"); } if ( length $HOST2 > 0 ) { my $CONNECTCOMMAND1 = "plink.exe -ssh USERNAME\@$HOST2 + -pw PASSWORD"; push (@CONNECTCOMMANDS, "$CONNECTCOMMAND2"); } my $CMD = "shownode -s"; my @TEMPHOST; my $TEMPLOG; my $OUTPUT; foreach (@CONNECTCOMMANDS) { @TEMPHOST = split /[\s@]+/, $_; $TEMPHOST = @TEMPHOST[5]; $TEMPLOG = "C:\\osit\\tmp\\$TEMPHOST.CHECK_NODE.tmp"; $OUTPUT = qx{$_ $CMD > "$TEMPLOG"}; open(file, '<:encoding(UTF-8)', $TEMPLOG) or die "Could not open file '$TEMPLOG' $!"; my $oknodes=0; while (my $row = <file>) { if($row =~ /ok/i) { $oknodes++; } + } printf "$TEMPHOST : $oknodes\n"; }

i have updated the code with something that should work

the expected output should be

somenode.domain.local : 4

somenode1.domain.local : 4

what i am getting is,

somenode.domain.local : 4

somenode1.domain.local : 8

as you are probably aware I am not a regular perl coder

Replies are listed 'Best First'.
Re: help with perl variables
by Laurent_R (Canon) on Sep 03, 2015 at 07:20 UTC
    From trying to decipher your very poorly formatted code, it seems to me that the $oknode variable is reset properly to 0 for each @CONNECTCOMMANDS, and therefore also for each $TEMPHOST, which, to the best of my comprehension, is presumably what you are looking for.

    Please do yourself a favor: learn how to indent code correctly: although the compiler does not care about indentation, humans do a lot, proper indentation is not optional, it is essential to the comprehension of what you're writing.

    As an additional side note, whenever you start to name your variable $var1, $var2, $var3, this must ring an alarm that there is almost certainly something wrong in what you are doing. The 16 variables defined in your NODE sub should probably be an array of hashes, i.e. something like this:

    my @nodes = ( { host => "somenode1.domain.local", username => "user", password => "password", parnode => 4 }, { host => "somenode2.domain.local", username => "user", password => "password", parnode => 4 } # ... );
    This would avoid duplication of code that you have, for example, in the MSWin32 sub, and which could be something like this (untested):
    my @CONNECTCOMMANDS; for my $host (@nodes) { push @CONNECTCOMMANDS, "$SSHCOMMAND -ssh $host->{username}\@$host + -pw $host->{password}"; }
    although you most probably don't need two arrays, since the second one (@CONNECTCOMMANDS) does not contain anything new compared to the original one, so that you could either use directly the first array of hashes in your subsequent code, or decide to put your ssh commands directly in your initial reference data array:
    my @connection_commands = ( 'plink.exe -ssh user1@somenode1.domain.local -pw password1', 'plink.exe -ssh user2@somenode2.domain.local -pw password2', # ... );
    (replacing obviously user1, user2, password1 and password2 by their actual values).

    Update: as I've just posted this answer, I see that you've just now deeply edited your original post, making some of my comments (and other monks' previous comments) look off-topic or unwarranted. When you change one of your posts, please keep the original post and add updates to show the difference between the OP and the updates, as I just did here.

Re: help with perl variables
by davido (Cardinal) on Sep 03, 2015 at 05:21 UTC

    It doesn't appear that the code you posted exhibits the behavior your are describing. But it's an incomplete snippet. I encourage you to post well formatted code that is complete enough that we can run it, that exhibits the behavior you are describing, that passes strict and warnings, and that is under 30 lines in length. If that means you post an example that stands as a proxy for larger code, so be it. In boiling your lengthy code down to a small test case, you may spot the problem yourself. But even if you don't, you'll at least provide us with something that we can run to see what you're talking about.

    And please, don't name variables $FOO1, $FOO2, $FOO3, $FOO4, etc. That's what actual arrays are for.


    Dave

      my $CONNECTCOMMAND1 = "$SSHCOMMAND -ssh $USERNAME1\@$HOST1 -pw $PASSWORD1";
      push (@CONNECTCOMMANDS, "$CONNECTCOMMAND1");

      Furthermore, the idiomatic tic of stringizing something that is already a string, e.g., "$CONNECTCOMMAND1", while it does no harm, also serves no purpose except to be reliably irritating.


      Give a man a fish:  <%-{-{-{-<

        Indeed.

        Just a few days ago I found myself working with code that did just that, and I had to spend a little time tracing through possible reasons why the original coder would have done that.

        If you're going to stringify a scalar that holds a numeric value by wrapping it in quotes, please document that's the reason so that others don't have to wonder if it was intentional. And if that's not the reason, just don't do it. It wastes people's time, wastes a few cycles, clutters the code, clutters its meaning, and drives pedants like me crazy.

        In the case of the code I was looking at, it served no purpose. In the case of this OP, it serves no purpose either.


        Dave

Re: help with perl variables
by AnomalousMonk (Archbishop) on Sep 03, 2015 at 05:20 UTC
    ... some things may look odd.

    Some things do look odd. In fact, most things look almost unreadable. Programming is hard enough without having to deal with code that looks like a filled-in crossword puzzle.

    Maybe try something like perltidy to get the code into a readable format. If code can easily be read, it is more likely to be understood — perhaps even by you! — and to attract useful advice.


    Give a man a fish:  <%-{-{-{-<

Re: help with perl variables
by AnomalousMonk (Archbishop) on Sep 03, 2015 at 15:35 UTC