in reply to Extracting network information
Hi Kasemodder, welcome to the Monastery.
First, put use strict; use warnings; at the top of your script and declare your variables with my. It is stupid to try to program Perl without that.
If you do that you'll get:
This tells you that the $2 and $4 are being interpolated by Perl (and they are not initialized in your program). So now you see the problem, thanks to use warnings;. If you escape the dollar signs that you want to use in your awk statement so that they are not interpolated by Perl, you should get what you want.$ perl 1140691.pl Use of uninitialized value $2 in concatenation (.) or string at 114069 +1.pl line 8. Use of uninitialized value $4 in concatenation (.) or string at 114069 +1.pl line 9. hostname : localhost IP : inet 10.5.50.27 netmask 0xffffff00 broadcast 10.5.50.25 +5 Netmask : inet 10.5.50.27 netmask 0xffffff00 broadcast 10.5.50.25 +5
Output:#!/usr/bin/perl use strict; use warnings; chomp(my $syshost = `hostname`); chomp(my $sysip = `ifconfig | grep -i inet | egrep -v "inet6|127.0.0.1 +" | head -n 1 | awk '{print \$2}'`); chomp(my $sysnm = `ifconfig | grep -i inet | egrep -v "inet6|127.0.0.1 +" | head -n 1 | awk '{print \$4}'`); print "hostname : $syshost\n"; print "IP : $sysip\n"; print "Netmask : $sysnm\n"; __END__
There are of course better ways to do this with Perl, without shelling out, but if this does what you want, then 👍$ perl 1140691.pl hostname : localhost IP : 10.5.50.27 Netmask : 0xffffff00 $
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Extracting network information
by Kasemodder (Initiate) on Sep 01, 2015 at 18:41 UTC |