our
$VERSION
=
'1.15.0.2'
;
sub
execute {
my
(
$class
,
$arg1
,
$action
,
$option
) =
@_
;
unless
(
$arg1
) {
die
(
"You have to define the vm name!"
);
}
my
$dom
=
$arg1
;
unless
(
$dom
) {
die
(
"VM $dom not found."
);
}
if
(
$action
eq
"add"
) {
for
my
$rule
(
keys
%{
$option
} ) {
my
$from_port
=
$option
->{
$rule
}->[0];
my
$to_port
=
$option
->{
$rule
}->[1];
i_run
"VBoxManage modifyvm \"$dom\" --natpf1 \"$rule,tcp,,$from_port,,$to_port\""
;
}
}
else
{
if
(
$option
ne
"-all"
) {
i_run
"VBoxManage modifyvm \"$dom\" --natpf1 delete \"$option\""
;
}
else
{
my
$info
= Rex::Virtualization::VBox::info->execute(
$dom
);
my
@keys
=
grep
{ m/^Forwarding/ }
keys
%{
$info
};
for
my
$k
(
@keys
) {
my
@_t
=
split
( /,/,
$info
->{
$k
} );
i_run
"VBoxManage modifyvm \"$dom\" --natpf1 delete \"$_t[0]\""
;
}
}
}
if
( $? != 0 ) {
die
(
"Error setting port forwarding options for vm $dom"
);
}
}
1;