From 72789c6c5dd91f6f256507d9b4fe76e32479b4ff Mon Sep 17 00:00:00 2001 From: Stephen Hemminger Date: Tue, 22 Dec 2009 14:05:50 -0800 Subject: Use perl to scan directory Better to use perl opendir/readdir to get list of files, rather than parsing output of ls. --- scripts/vyatta-show-dhclient.pl | 58 ++++++++++++++++++----------------------- 1 file changed, 25 insertions(+), 33 deletions(-) diff --git a/scripts/vyatta-show-dhclient.pl b/scripts/vyatta-show-dhclient.pl index 0f3990b..7ae1b79 100755 --- a/scripts/vyatta-show-dhclient.pl +++ b/scripts/vyatta-show-dhclient.pl @@ -1,25 +1,25 @@ #!/usr/bin/perl # # Module: vyatta-show-dhclient.pl -# +# # **** License **** # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License version 2 as # published by the Free Software Foundation. -# +# # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. -# +# # This code was originally developed by Vyatta, Inc. # Portions created by Vyatta are Copyright (C) 2007 Vyatta, Inc. # All Rights Reserved. -# +# # Author: Stig Thormodsrud # Date: January 2008 # Description: Script to display dhcp client lease info -# +# # **** End License **** # @@ -30,51 +30,43 @@ use warnings; my $lease_dir = '/var/lib/dhcp3'; - sub dhclient_get_lease_files { my ($intf) = @_; - - # todo: fix sorting for ethX > 9 my @lease_files; - my $LS; + if ($intf eq "all") { - my $file = "dhclient_"; - open($LS,"ls $lease_dir |grep '^$file.*\_lease\$' | sort |"); + opendir(my $dh, $lease_dir) or die "Can't open $lease_dir: $!"; + @lease_files = grep { /^dhclient_.*_lease$/ } readdir($dh); + closedir $dh; } else { - my $file = "dhclient_$intf"; - open($LS,"ls $lease_dir |grep '^$file\_lease\$' | sort |"); + my $file = 'dhclient_'. $intf . '_lease'; + @lease_files = ( $file ) if -f "$lease_dir/$file"; } - @lease_files = <$LS>; - close($LS); - foreach my $i (0 .. $#lease_files) { - $lease_files[$i] = "$lease_dir/$lease_files[$i]"; - } - chomp @lease_files; + return @lease_files; } sub dhclient_parse_vars { - my ($file) = @_; + my $file = shift; + + open (my $f, '<', "$lease_dir/$file") + or return; my %var_list; - if ( !(-f $file)) { - return %var_list; - } - - open(FD, "<$file"); my $line; - $line = ; + $line = <$f>; chomp $line; $var_list{'last_update'} = $line; - while ($line = ) { + + while ($line = <$f>) { chomp $line; if ($line =~ m/(\w+)=\'([\w\s.]+)\'/) { my $var = $1; my $val = $2; $var_list{$var} = $val; - } + } } - close(FD); + close $f; return %var_list; } @@ -107,7 +99,7 @@ sub dhclient_show_lease { my $new_expiry_str; if (defined $new_expiry) { - $new_expiry_str = strftime("%a %b %d %R:%S %Z %Y", + $new_expiry_str = strftime("%a %b %d %R:%S %Z %Y", localtime($new_expiry)); } @@ -144,9 +136,9 @@ sub dhclient_show_lease { } } print "router : $new_routers\n" if defined $new_routers; - print "name server: $new_domain_name_servers\n" if + print "name server: $new_domain_name_servers\n" if defined $new_domain_name_servers; - print "dhcp server: $new_dhcp_server_identifier\n" if + print "dhcp server: $new_dhcp_server_identifier\n" if defined $new_dhcp_server_identifier; print "lease time : $new_dhcp_lease_time\n" if defined $new_dhcp_lease_time; print "last update: $last_update\n" if defined $last_update; @@ -166,7 +158,7 @@ if ($#ARGV >= 0) { } my @dhclient_files = dhclient_get_lease_files($intf); -foreach my $file (@dhclient_files) { +foreach my $file (sort @dhclient_files) { dhclient_show_lease($file); } -- cgit v1.2.3