-3

I am trying to fetch my dns server ip address from command prompt but I am unable to fetch it.

my @ip1 = `ipconfig/all`;
open(my $fh, '>file.txt') or die "Couldn't open file file.txt, $!";
print $fh @ip1;
close($fh); 

my $row;
foreach (@ip1)
{

    if($_ =~ m/DNS Servers/)
     {  
        # print "$_";    
        $row = split(/:/,$_,1);
        print "$row\n";

     }

}

output:
1
1

shark1992
  • 11
  • 4
  • 2
    In this program, 'ipconfig' is a string literal - which means you have to quote it like I did earlier in this sentence. You are expected to make some minimum effort to learn and understand what you are doing before posting here. This does not meet that minimum standard. – Marty May 12 '16 at 04:23

1 Answers1

0

First ipconfiq is a string literal .

You want to execute the command and store the output in an array. So you should know the difference about system, Backtick / qx and exec

system will execute the command and the result is display. The result cann't store into any variable.

backtick or qx will execute the command and their result is able to store into the variable.

so your script should as

my @ip1 = `ipconfig`;
print "ip information is @ip1"; 
mkHun
  • 5,891
  • 8
  • 38
  • 85
  • More details [see this question](http://stackoverflow.com/questions/799968/whats-the-difference-between-perls-backticks-system-and-exec) – mkHun May 12 '16 at 04:49