0

I have an assignment where i am supposed to create a perl script in linux that will give an output similar to that of the linux command "ifconfig". I have started writing the script but I repeatedly get errors. I would like some assistance in correcting my code so it becomes fully operational. What the program needs to do specificaly is require a perimeter. for example when i run the script it will ask for an interface name, if I typed in "eth0" which will be the interface name, and then it should output as if i typed in the command “ifconfig eth0”. The assignment requires that I use these two packages Net::Int::Stats and Net::Ifconfig::Wrapper. The code i have so far is below. I would really appreciate any help

 #!/usr/bin/perl
use strict;
use warnings;
use Net::Int::Stats;
use Net::Ifconfig::Wrapper;

my $int            = 'eth0';
my $stat           = 'rx_packets';
my $Iface          = $ARGV[0];
my $get_Iface_data = Net::Int::Stats->new();
my $Iface_Info     = Net::Ifconfig::Wrapper::Ifconfig('list', '', '', '');

my $rx_packets  = $get_Iface_data->value($Iface, 'rx_packets');
while( my ( $Addr, $Mask ) = each %{ $Info->{$int}{'inet'} } ) { 
    print "\tinet $Mask $Addr"; 
}
kjprice
  • 1,316
  • 10
  • 26
user218001
  • 35
  • 1
  • 8
  • user218001, are you passing in eth 0, "eth 0", or eth0? Probably, only eth0 is the proper one. – kjprice Apr 16 '13 at 19:50
  • Can you please provide specific errors you get that you can't figure out? – DVK Apr 16 '13 at 19:51
  • Also, I'm betting you're going to want a newline on that print statement : `print "\tinet $Mask $Addr\n";` – kjprice Apr 16 '13 at 19:51
  • I am trying to pass eth0, sorry for the confusion there. Errors i encounter = Global symbol "$get_Iface_data" requires explicit package name at ./ass3.pl line 8. Global symbol "$Info" requires explicit package name at ./ass3.pl line 10. Global symbol "$int" requires explicit package name at ./ass3.pl line 10. Execution of ./ass3.pl aborted due to compilation errors. – user218001 Apr 16 '13 at 19:54
  • That's correct - you don't assign anything to "$get_Iface_data" or "$Info" variables, and try to use them. (the actual error is because they aren't declared, but that's just a syntactic symptom of the bigger logical problem - you'd declare them when assigning via "my" if you assigned them). `$get_Iface_data` should probably be an object of Ifconfig class? – DVK Apr 16 '13 at 20:00
  • can you please put your suggestion in code format, so I may know how to implement it. thanks – user218001 Apr 16 '13 at 20:14
  • Why do you hardcode `eth0`? Aren't you supposed to be passing that in to `$Iface`? – kjprice Apr 16 '13 at 20:41
  • thts what im saying...im not sure how to do that, can you please explain how i would pass it to $iface. – user218001 Apr 16 '13 at 20:50
  • But you do. `$ARGV[0]` is the first argument passed to your script. – kjprice Apr 16 '13 at 21:15
  • OK..well is the rest of the code fine, it still doesn't work, in your opinion what else needs to be fixed – user218001 Apr 16 '13 at 23:16
  • You don't do anything with $rx_packets. I haven't read the perldoc for either module, so I can't say exactly how you should be getting the info the mimic `ifconfig`. But, just by looking at this, I'm not sure that it will. – kjprice Apr 17 '13 at 00:24
  • Why don't you just copy the whole script provided in the perldoc? That looks like what you want... – kjprice Apr 17 '13 at 00:28

1 Answers1

0

Here is the working example:

#!/usr/bin/perl
use strict;
use warnings;
use Net::Int::Stats;
use Net::Ifconfig::Wrapper;

my $Iface   = $ARGV[0];
my $get_Iface_data = Net::Int::Stats->new();
my $rx_packets  = $get_Iface_data->value($Iface, 'rx_packets');
my $Iface_Info = Net::Ifconfig::Wrapper::Ifconfig('list', '', '', '');
while( my ( $Addr, $Mask ) = each %{ $Iface_Info->{$Iface}{'inet'} } ) {
    print "\tinet $Mask $Addr\n";
}
Dmitry Mina
  • 3,842
  • 1
  • 14
  • 10