how to expand a perl variable within a unix command

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mnew
    New Member
    • Feb 2010
    • 4

    how to expand a perl variable within a unix command

    Hi, i have just started using Perl and I'm trying to expand a perl variable within a Unix command in the perl script, for example: -
    Code:
    foreach $host (@lines)
    
    {
    
            print "$host\n";
            $sshcmd = "ssh";
            $get = `$sshcmd $host "uptime"`;
            print "$get\n";
    
    }
    I want to use the contents of $host for the hostname but when i run the script it just hangs. if anyone could let me know how to expand the variable within the Unix command that would be great.

    Thanks
    Last edited by numberwhun; Feb 18 '10, 06:04 PM. Reason: Please Use CODE Tags!
  • RonB
    Recognized Expert Contributor
    • Jun 2009
    • 589

    #2
    What makes you think it's not expanding/interpolating the variable?

    It's most likely waiting for user input for the username/password.

    Comment

    • mnew
      New Member
      • Feb 2010
      • 4

      #3
      Hi RonB

      If i explicitly specify a hostname instead of $host it works, but nothing happens when I replace the real hostname with $host variable. Also, no username and password is required as I am using ssh keys to connect to all of the servers in my array.

      Comment

      • RonB
        Recognized Expert Contributor
        • Jun 2009
        • 589

        #4
        $host does not hold the value you think it holds.

        Add this just before the foreach loop and post its output.
        Code:
        use Data::Dumper;
        print Dumper \@lines;
        Also, always post your code within the code tags, as I did. The code tag button is not available in the "Quick Reply" window. You need to click on the "Go Advanced" button to get it and the other features.

        Comment

        • mnew
          New Member
          • Feb 2010
          • 4

          #5
          Sure, below are my hostnames: -

          Code:
          bash-3.00# ./check.pl
          $VAR1 = [
                    'lonprodd1
          ',
                    'cradevd1
          ',
                    'lonprodgz1
          '
                  ];

          Comment

          • RonB
            Recognized Expert Contributor
            • Jun 2009
            • 589

            #6
            That shows that your hostnames need to have the "\n" stripped off.

            Code:
            foreach my $host (@lines)
            {
                [B]chomp $host;[/B]
                print "$host\n";
                $sshcmd = "ssh";
                $get = `$sshcmd $host uptime`;
                print "$get\n";
            }

            Comment

            • mnew
              New Member
              • Feb 2010
              • 4

              #7
              Oh man, can't believe I missed that. Thanks for your help Ron, it working fine now.

              Comment

              Working...