Page 1 of 1

Update and Install Extension Pack via Command Line

Posted: 30. Jul 2017, 07:51
by bluegroper
Regular updating of VirtualBox on too many hosts became irritating.
Here's a small script that I use to automate the process, including the licence acceptance.
Since I'm just a sysadmin (not a real programmer) I'd be delighted for any suggestions or improvement/s.
Also YMMV

Code: Select all

#!/usr/bin/perl -w

#	Update VirtualBox and ExtensionPack programmatically
#	Redhat, CentOS, etc
#	Needs some package/s
#	yum install  perl-WWW-Mechanize  perl-HTML-FormatText-WithLinks

use strict;
use WWW::Mechanize;
use HTML::FormatText::WithLinks;
use LWP::Simple;

#	Must run as root
die "Must run as root\n" if $> != 0;

#	Get the correct VB version, eg 5.x
my $VB_version = `vboxmanage --help | head -n 1`;
$VB_version = (split '\ ',$VB_version)[-1];
my ($VB_major, $VB_minor, $VB_dot) = split (/\./, $VB_version);
$VB_version = "$VB_major"."."."$VB_minor";

#	Update VirtualBox package
print "\n...Updating VirtualBox $VB_version\n";
system ("yum update -y VirtualBox-$VB_version");

#	Get the correct VBox ExtensionPack
print "\n...Get the VirtualBox ExtensionPack for $VB_version\n";
my $url="https://www.virtualbox.org/wiki/Downloads";
my $mech=WWW::Mechanize->new();
$mech->get($url);
my $html=$mech->content(base_href => undef);
my $page = HTML::FormatText::WithLinks->new();
my $text = $page->parse($html);
my @lines = split /\n/, $text;
foreach my $line (@lines) {
	next unless $line =~ m/VirtualBox_Extension_Pack-$VB_version/;
	my ($http_filename) = ($line =~ m/(https?:\/\/[\w\.\-\/\_]+)?$/);
	print "http filename : $http_filename\n" ;
	my ($FileName) = ($http_filename =~ m/([\w\.\-\_]+)$/);
	print "Local filename : /tmp/$FileName\n";
	my $status = getstore("$http_filename", "/tmp/$FileName");

	#	Install the ExtensionPack
	if (is_success($status)) {
		print "...File downloaded correctly\n";
		print "...Installing ExtensionPack for $VB_version\n";
		system ("echo y > /tmp/yes");
		system ("VBoxManage extpack install --replace /tmp/$FileName < /tmp/yes");
		print "\n*** Don't forget to install updated vbox GuestAdditions in VBox guest/s ***\n\n";
		exit ();
	} else {
		print "error downloading file: $status\n";
		exit ();
	}
}
exit ();