diff options
Diffstat (limited to 'scripts')
-rw-r--r-- | scripts/dhcpv6-client-show-leases.pl | 211 | ||||
-rw-r--r-- | scripts/rename-image.pl | 108 | ||||
-rw-r--r-- | scripts/show-image-storage.pl | 66 | ||||
-rwxr-xr-x | scripts/vyatta-boot-image.pl | 73 | ||||
-rwxr-xr-x | scripts/vyatta-show-interfaces.pl | 4 |
5 files changed, 450 insertions, 12 deletions
diff --git a/scripts/dhcpv6-client-show-leases.pl b/scripts/dhcpv6-client-show-leases.pl new file mode 100644 index 0000000..546668c --- /dev/null +++ b/scripts/dhcpv6-client-show-leases.pl @@ -0,0 +1,211 @@ +#!/usr/bin/perl + +# Module: dhcpv6-client-show-leases.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. +# +# A copy of the GNU General Public License is available as +# `/usr/share/common-licenses/GPL' in the Debian GNU/Linux distribution +# or on the World Wide Web at `http://www.gnu.org/copyleft/gpl.html'. +# You can also obtain it by writing to the Free Software Foundation, +# Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, +# MA 02110-1301, USA. +# +# This code was originally developed by Vyatta, Inc. +# Portions created by Vyatta are Copyright (C) 2010 Vyatta, Inc. +# All Rights Reserved. +# +# Author: Bob Gilligan +# Date: April 2010 +# Description: Script to display DHCPv6 client leases in a user-friendly form +# +# **** End License **** + +use strict; +use lib "/opt/vyatta/share/perl5/"; + +use Getopt::Long; +use Vyatta::Config; + + +# Globals +my $debug_flag = 0; + +GetOptions( + "debug" => \$debug_flag, + ); + + +sub log_msg { + my $message = shift; + + print "DEBUG: $message" if $debug_flag; +} + + +# +# Main section. +# + +opendir (my $dir, "/var/lib/dhcp3"); +my @lease_files; +while (my $f = readdir $dir) { + if ($f =~ /^dhclient_v6_(\w+).leases$/) { + push (@lease_files, $f); + } +} +closedir $dir; + +if ($debug_flag) { + print "lease files:", join(' ',@lease_files), "\n"; +} + +# Holds the most recent (last) entry for each interface +my %ghash = (); + +foreach my $lease_filename (@lease_files) { + my @lines=(); + + if (!open(LEASE_FILE, "</var/lib/dhcp3/$lease_filename")) { + printf("Can't open lease file for reading: $lease_filename\n"); + exit 1; + } + + @lines = <LEASE_FILE>; + close(LEASE_FILE); + chomp @lines; + + my $level = 0; + my $s1; + my $s2; + my $ia_na; + my $iaaddr; + my $ends_day; + my $ends_time; + my $ifname; + my $starts; + my $pref_life; + my $max_life; + my $binding_state; + + # Parse the leases file into a hash keyed by IPv6 addr. + foreach my $line (@lines) { + log_msg("Line: $line\n"); + if ($line =~ /^lease6 \{/) { + if ($level != 0) { + printf("Found lease6 at level $level\n"); + exit 1; + } + $level++; + } elsif ($line =~ /^.*ia-na .*\{/) { + if ($level != 1) { + printf("Found ia-na at level $level\n"); + exit 1; + } + log_msg("setting ia_na\n"); + ($s1, $ia_na, $s2) = split(' ', $line); + $level++; + } elsif ($line =~ /^.*interface /) { + if ($level != 1) { + printf("Found interface at level $level\n"); + exit 1; + } + ($s1, $ifname) = split(' ', $line); + $ifname =~ s/;//; + $ifname =~ s/\"//g; + log_msg("Setting ifname to $ifname\n"); + } elsif ($line =~ /^.*iaaddr .*\{/) { + if ($level != 2) { + printf("Found iaaddr at level $level\n"); + exit 1; + } + ($s1, $iaaddr, $s2) = split(' ', $line); + log_msg("Setting iaaddr to $iaaddr.\n"); + log_msg("s1 $s1 s2 $s2\n"); + $level++; + } elsif ($line =~ /^.*starts /) { + ($s1, $starts) = split(' ', $line); + $starts =~ s/;//; + } elsif ($line =~ /^.*preferred-life /) { + ($s1, $pref_life) = split(' ', $line); + $pref_life =~ s/;//; + } elsif ($line =~ /^.*max-life /) { + ($s1, $max_life) = split(' ', $line); + $max_life =~ s/;//; + } elsif ($line =~ /^.*ends /) { + if ($level != 2) { + printf("Found ends at level $level\n"); + exit 1; + } + log_msg("Setting ends_day ends_time\n"); + ($s1, $s2, $ends_day, $ends_time) = split(' ', $line); + $ends_time =~ s/;//; + } elsif ($line =~ /^.*binding state /) { + if ($level != 2) { + printf("Found binding state at level $level\n"); + exit 1; + } + log_msg("Setting binding state\n"); + ($s1, $s2, $binding_state) = split(' ', $line); + $binding_state =~ s/;//; + } elsif ($line =~ /^.*\{/) { + log_msg("Unknown clause: $line\n"); + $level++; + } elsif ($line =~ /\}$/) { + $level--; + if ($level == 0) { + if (!defined($ia_na)) { + printf("ia_na not defined\n"); + exit 1; + } + + if (!defined($iaaddr)) { + printf("iaaddr not defined\n"); + exit 1; + } + } + } else { + log_msg("Unknown parameter: $line\n"); + } + } + + my @array = ($ia_na, $iaaddr, $starts, $max_life, $pref_life); + $ghash{$ifname} = \@array; +} + +# Display the leases... + +my $num_entries = scalar(keys %ghash); +if ($num_entries == 0) { + printf("There are no DHCPv6 leases.\n"); + exit 0; +} else { + printf("DHCPv6 client leases:\n"); +} + +printf("\n"); +printf("Interface IPv6 Address Expires\n"); +printf("--------- --------------------------------------- ------------------------\n"); +foreach my $key (keys %ghash) { + my $entry = $ghash{$key}; + my ($ia_na, $iaaddr, $starts, $max_life, $pref_life) = @$entry; + my $ts; + if (defined ($starts) && defined ($max_life)) { + my $exp_time = $starts + $max_life; + $ts = localtime($exp_time); + } else { + $ts = "Unknown"; + } + printf ("%-9s %-39s %s\n", $key, $iaaddr, $ts); +} + + + diff --git a/scripts/rename-image.pl b/scripts/rename-image.pl new file mode 100644 index 0000000..e2e9745 --- /dev/null +++ b/scripts/rename-image.pl @@ -0,0 +1,108 @@ +#!/usr/bin/perl + +# **** 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) 2010 Vyatta, Inc. +# All Rights Reserved. +# +# Author: Bob Gilligan +# Date: April 30, 2010 +# Description: Script to re-name a system image. +# +# **** End License **** + +use strict; +use warnings; +use Getopt::Long; +use File::Temp qw/ tempfile tempdir /; + +my $old_name; +my $new_name; + +GetOptions( + 'old_name:s' => \$old_name, + 'new_name:s' => \$new_name, + ); + +if (!defined($old_name) || !defined($new_name)) { + printf("Must specify both old ane new name.\n"); + exit 1; +} + +my $image_path = "/live/image/boot"; + +if (! -e "$image_path") { + # must be running on old non-image installed system + $image_path = ""; +} + +if (! -e "$image_path/$old_name") { + printf("Old name $old_name does not exist.\n"); + exit 1; +} + +if (("$new_name" eq "Old-non-image-installation") || + ("$new_name" eq "grub") || + ("$new_name" =~ /^initrd/) || + ("$new_name" =~ /^vmlinuz/) || + ("$new_name" =~ /^System\.map/) || + ("$new_name" =~ /^config-/)) { + printf("Can't use reserved image name.\n"); + exit 1; +} + +my $cmdline=`cat /proc/cmdline`; +my $cur_name; +($cur_name, undef) = split(' ', $cmdline); +$cur_name =~ s/BOOT_IMAGE=\/boot\///; +$cur_name =~ s/\/vmlinuz.*//; + +if ($old_name eq $cur_name) { + printf("Can't re-name the running image.\n"); + exit 1; +} + +if (-e "$image_path/$new_name") { + printf("New name $new_name already exists.\n"); + exit 1; +} + +printf("Renaming image $old_name to $new_name.\n"); + +my $tmpfh; +my $tmpfilename; +($tmpfh, $tmpfilename) = tempfile(); + +if (!open (GRUBFH, "<${image_path}/grub/grub.cfg")) { + printf("Can't open grub file.\n"); + exit 1; +} + +# This is sensitive to the format of menu entries and boot paths +# in the grub config file. +# +my $line; +while ($line = <GRUBFH>) { + $line =~ s/\/boot\/$old_name/\/boot\/$new_name/g; + $line =~ s/Vyatta $old_name/Vyatta $new_name/; + $line =~ s/Lost password change $old_name/Lost password change $new_name/; + printf($tmpfh $line); +} + +close($tmpfh); +close(GRUBFH); + +system("mv $image_path/$old_name $image_path/$new_name"); +system("cp $tmpfilename $image_path/grub/grub.cfg"); + +printf("Done.\n"); + diff --git a/scripts/show-image-storage.pl b/scripts/show-image-storage.pl new file mode 100644 index 0000000..00a5112 --- /dev/null +++ b/scripts/show-image-storage.pl @@ -0,0 +1,66 @@ +#!/usr/bin/perl + +# **** 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) 2010 Vyatta, Inc. +# All Rights Reserved. +# +# Author: Bob Gilligan +# Date: April 30, 2010 +# Description: Script to display disk storage used by images +# +# **** End License **** + +use strict; +use warnings; +use Getopt::Long; + + +# +# Main section +# + +# Figure out where the images live... +my $imagedir = "/live/image/boot"; +if (! -e $imagedir) { + # Must be running on Old non-image system. + $imagedir = "/boot"; + if (! -e $imagedir) { + printf("Can't locate system image directory!\n"); + exit 1; + } +} + +my $bootlist=`/opt/vyatta/bin/vyatta-boot-image.pl --list`; + +my @bootlist_arr = split(/\n/, $bootlist); + +printf("Image name Read-Only Read-Write Total\n"); +printf("------------------------------ ------------ ------------ ------------\n"); + +foreach my $image (@bootlist_arr) { + my $total; + my $read_only; + my $read_write; + my $string; + my $garbage; + + if ( -e "$imagedir/$image") { + $string = `du -s $imagedir/$image`; + ($total, $garbage) = split(' ', $string); + $string = `du -s $imagedir/$image/*.squashfs`; + ($read_only, $garbage) = split(' ', $string); + $read_write = $total - $read_only; + printf("%-30s %12d %12d %12d\n", $image, $read_only, $read_write, + $total); + } +} diff --git a/scripts/vyatta-boot-image.pl b/scripts/vyatta-boot-image.pl index 276d704..9431711 100755 --- a/scripts/vyatta-boot-image.pl +++ b/scripts/vyatta-boot-image.pl @@ -57,6 +57,8 @@ sub parseGrubCfg { my @entries = (); my $in_entry = 0; my $idx = 0; + my $running_boot_cmd=`cat /proc/cmdline`; + $running_boot_cmd =~ s/BOOT_IMAGE=//; while (<$fd>) { if ($in_entry) { if (/^}/) { @@ -87,6 +89,11 @@ sub parseGrubCfg { } else { $ehash{'reset'} = 0; } + if (/$running_boot_cmd/) { + $ehash{'running_vers'} = 1; + } else { + $ehash{'running_vers'} = 0; + } push @entries, \%ehash; } } elsif (/^set default=(\d+)$/) { @@ -179,23 +186,70 @@ sub getBootList { } +# Get the Vyatta version of a particular image, given its name. +# +sub image_vyatta_version { + my ($image_name) = @_; + + my $vers; + my $dpkg_path = "var/lib/dpkg"; + + my $image_path; + if ($image_name eq $OLD_IMG_VER_STR) { + $image_path = ""; + } else { + $image_path = "/live/image/boot/$image_name/live-rw"; + } + + $image_path .= "/var/lib/dpkg"; + + if ( -e $image_path ) { + $vers = `dpkg-query --admindir=$image_path --showformat='\${Version}' --show vyatta-version`; + return $vers; + } else { + if ($image_name eq $OLD_IMG_VER_STR) { + return "unknown"; + } + + my @squash_files = </live/image/boot/$image_name/*.squashfs>; + foreach my $squash_file (@squash_files) { + if (-e $squash_file) { + system("sudo mkdir /tmp/squash_mount"); + system("sudo mount -o loop,ro -t squashfs $squash_file /tmp/squash_mount"); + $image_path = "/tmp/squash_mount/var/lib/dpkg"; + my $vers = `dpkg-query --admindir=$image_path --showformat='\${Version}' --show vyatta-version`; + system("sudo umount /tmp/squash_mount"); + system("sudo rmdir /tmp/squash_mount"); + return $vers; + } + } + # None found + return "unknown2" + } +} + # Prints the boot list generated by getBootList(). If the argument # $brief is set, display the entries for machine instead of human # consumption: One entry per line showing the the version string only. # sub displayBootList { - my ($didx, $entries, $brief) = @_; - my $running_ver = curVer(); + my ($didx, $entries, $brief, $show_version) = @_; for my $i (0 .. $#{$entries}) { my $di = $i + 1; my $ver = $ {$entries}[$i]->{'ver'}; my $m = ''; + + if (defined $show_version) { + my $vyatta_vers = image_vyatta_version($ver); + $m .= " [$vyatta_vers]"; + } + if ($didx == $ {$entries}[$i]->{'idx'}) { - $m = ' (default boot)'; + $m .= ' (default boot)'; } - if ($ver eq $running_ver) { - $m .= ' (running version)'; + if ($ {$entries}[$i]->{'running_vers'} == 1) { + $m .= ' (running image)'; } if (defined($brief)) { @@ -416,13 +470,14 @@ sub doDelete { # Main section # -my ($show, $del, $sel, $list) = (undef, undef, undef, undef); +my ($show, $del, $sel, $list, $show_vers) = (undef, undef, undef, undef); GetOptions( 'show' => \$show, 'delete:s' => \$del, 'select:s' => \$sel, - 'list' => \$list + 'list' => \$list, + 'show_vers' => \$show_vers, ); if (-e $UNION_GRUB_CFG) { @@ -481,7 +536,7 @@ if (defined($del)) { $msg = 'The following image(s) can be deleted:'; } print "$msg\n\n"; -displayBootList($def_idx, $bentries); +displayBootList($def_idx, $bentries, undef, $show_vers); print "\n"; exit 0 if (defined($show) || (!defined($sel) && !defined($del))); # show-only @@ -489,7 +544,7 @@ exit 0 if (defined($show) || (!defined($sel) && !defined($del))); # show-only # for doing select my $prompt_msg = 'Select the default boot image: '; my $error_msg = 'Invalid selection. Default is not changed.'; -if ($del) { +if (defined ($del)) { # doing delete $prompt_msg = 'Select the image to delete: '; $error_msg = 'Invalid selection. Nothing is deleted.'; diff --git a/scripts/vyatta-show-interfaces.pl b/scripts/vyatta-show-interfaces.pl index 1b662c8..6e071e2 100755 --- a/scripts/vyatta-show-interfaces.pl +++ b/scripts/vyatta-show-interfaces.pl @@ -57,10 +57,8 @@ my @tx_stat_vars = sub get_intf_description { my $name = shift; - my $intf = new Vyatta::Interface($name); - return "" unless $intf; + my $description = interface_description($name); - my $description = $intf->description(); return "" unless $description; return $description; } |