From f215e02bf85f68d3a6106c2a1f4f7f063f819064 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Thu, 11 Apr 2024 10:17:27 +0200 Subject: Adding upstream version 7.0.14-dfsg. Signed-off-by: Daniel Baumann --- .../Devices/Network/scripts/VBoxConvertNATStats.sh | 123 +++++++++++++++++ .../Devices/Network/scripts/VBoxPortForwarding.py | 149 +++++++++++++++++++++ 2 files changed, 272 insertions(+) create mode 100755 src/VBox/Devices/Network/scripts/VBoxConvertNATStats.sh create mode 100755 src/VBox/Devices/Network/scripts/VBoxPortForwarding.py (limited to 'src/VBox/Devices/Network/scripts') diff --git a/src/VBox/Devices/Network/scripts/VBoxConvertNATStats.sh b/src/VBox/Devices/Network/scripts/VBoxConvertNATStats.sh new file mode 100755 index 00000000..6fd9a114 --- /dev/null +++ b/src/VBox/Devices/Network/scripts/VBoxConvertNATStats.sh @@ -0,0 +1,123 @@ +#!/bin/sh +## @file +# ??? +# + +# +# Copyright (C) 2009-2023 Oracle and/or its affiliates. +# +# This file is part of VirtualBox base platform packages, as +# available from https://www.virtualbox.org. +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation, in version 3 of the +# License. +# +# 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. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, see . +# +# SPDX-License-Identifier: GPL-3.0-only +# + +#change device to select NAT${NAT_STAT_DEVICE} counters + +NAT_STAT_DEBUG=1 +NAT_STAT_DEVICE=0 +NAT_TMP=/tmp +NAT_STATS_FMT=wiki + +NAT_STATS_CONVERTER=$NAT_TMP/converter.awk +NAT_STATS_COUNTERS_RAW=${NAT_TMP}/counters.out.raw +NAT_STATS_COUNTERS=${NAT_TMP}/counters.out +NAT_STATS_REPORT=NAT_STATS_NAME.${NAT_STATS_FMT} + +NAT_IN_FILE=$1 + +[ x"$TMP" != x ] && NAT_TMP=TMP + +grep NAT${NAT_STAT_DEVICE} $NAT_IN_FILE > $NAT_STATS_COUNTERS_RAW +[ $? -ne 0 ] && echo "error happens while grep'ing the NAT's counters" && exit 1 + +#sed -ne "s/\ */\t/gp" $NAT_STATS_COUNTERS_RAW > $NAT_STATS_COUNTERS +cp $NAT_STATS_COUNTERS_RAW $NAT_STATS_COUNTERS + +cat > $NAT_STATS_CONVERTER <" + if (COUNTERS == "counting") + { + NF = 2; + \$1 = "name" + \$2 = "count" + if (FMT == "wiki") + print "" \$0 "" + else + print \$0 + } + else if (COUNTERS == "profiling") + { + NF=6 + \$1 = "name" + \$2 = "ticks_per_count" + \$3 = "total_ticks" + \$4 = "times" + \$5 = "max" + \$6 = "min" + if (FMT == "wiki") + print "" \$0 "" + else + print \$0 + } +} +/*counting counters */ +NF == 3 && COUNTERS=="counting"{ + name = \$1 + count = \$2 + NF=2 + if (FMT == "wiki") + print "" \$0 "" + else + print \$0 +} +/*profiling counters */ +NF == 12 && COUNTERS=="profiling"{ + name = \$1 + ticks_per_count = \$2 + total_ticks = \$5 + times = \$7 + max = \$10 + min = \$12 + NF=6 + \$1 = name + \$2 = ticks_per_count + \$3 = total_ticks + \$4 = times + \$5 = substr(max,0, index(max, ",") -1) + \$6 = substr(min,0, index(min, ")") - 1) + + if (FMT == "wiki") + print "" \$0 "" + else + print \$0 +} +END{ + if (FMT == "wiki") + print "" +} +EOF +awk -v FMT=$NAT_STATS_FMT -v COUNTERS=profiling -f $NAT_STATS_CONVERTER $NAT_STATS_COUNTERS > $NAT_STATS_REPORT +awk -v FMT=$NAT_STATS_FMT -v COUNTERS=counting -f $NAT_STATS_CONVERTER $NAT_STATS_COUNTERS >> $NAT_STATS_REPORT + diff --git a/src/VBox/Devices/Network/scripts/VBoxPortForwarding.py b/src/VBox/Devices/Network/scripts/VBoxPortForwarding.py new file mode 100755 index 00000000..4968c947 --- /dev/null +++ b/src/VBox/Devices/Network/scripts/VBoxPortForwarding.py @@ -0,0 +1,149 @@ +#!/usr/bin/python + +""" +Copyright (C) 2009-2023 Oracle and/or its affiliates. + +This file is part of VirtualBox base platform packages, as +available from https://www.virtualbox.org. + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation, in version 3 of the +License. + +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. + +You should have received a copy of the GNU General Public License +along with this program; if not, see . + +SPDX-License-Identifier: GPL-3.0-only +""" + +################################################################################# +# This program is a port-forwarding configurator supposed to simplify +# port-forwarding for NAT users +# > python VBoxPortForwarding.py --vm winXP -a 1 -p TCP -l 8080 -g 80 -P www +# generates sequence of API calls, equivalent to: +# > VBoxManage setextradata "winXP" +# "VBoxInternal/Devices/pcnet/0/LUN#0/Config/www/Protocol" TCP +# > VBoxManage setextradata "winXP" +# "VBoxInternal/Devices/pcnet/0/LUN#0/Config/www/GuestPort" 80 +# > VBoxManage setextradata "winXP" +# "VBoxInternal/Devices/pcnet/0/LUN#0/Config/www/HostPort" 8080 +################################################################################ + +import os,sys +from vboxapi import VirtualBoxManager +import optparse + +class OptionParser (optparse.OptionParser): + def check_required(self, opt): + option = self.get_option(opt) + if option.type == "string" and getattr(self.values, option.dest) != None: + return True + if option.type == "int" and getattr(self.values, option.dest) != -1: + return True + return False + +def generate_profile_name(proto, host_port, guest_port): + return proto + '_' + str(host_port) + '_' + str(guest_port) + +def main(argv): + + usage = "usage: %prog --vm winXP -a 1 -p TCP -l 8080 -g 80 -P www" + parser = OptionParser(usage=usage) + parser.add_option("-V", "--vm", action="store", dest="vmname", type="string", + help="Name or UID of VM to operate on", default=None) + parser.add_option("-P", "--profile", dest="profile", type="string", + default=None) + parser.add_option("-p", "--ip-proto", dest="proto", type="string", + default=None) + parser.add_option("-l", "--host-port", dest="host_port", type="int", + default = -1) + parser.add_option("-g", "--guest-port", dest="guest_port", type="int", + default = -1) + parser.add_option("-a", "--adapter", dest="adapter", type="int", + default=-1) + (options,args) = parser.parse_args(argv) + + if (not (parser.check_required("-V") or parser.check_required("-G"))): + parser.error("please define --vm or --guid option") + if (not parser.check_required("-p")): + parser.error("please define -p or --ip-proto option") + if (not parser.check_required("-l")): + parser.error("please define -l or --host_port option") + if (not parser.check_required("-g")): + parser.error("please define -g or --guest_port option") + if (not parser.check_required("-a")): + parser.error("please define -a or --adapter option") + + man = VirtualBoxManager(None, None) + vb = man.getVirtualBox() + print "VirtualBox version: %s" % vb.version, + print "r%s" % vb.revision + + vm = None + try: + if options.vmname != None: + vm = vb.findMachine(options.vmname) + elif options.vmname != None: + vm = vb.getMachine(options.vmname) + except: + print "can't find VM by name or UID:",options.vmname + del man + return + + print "vm found: %s [%s]" % (vm.name, vm.id) + + session = man.openMachineSession(vm.id) + vm = session.machine + + adapter = vm.getNetworkAdapter(options.adapter) + + if adapter.enabled == False: + print "adapter(%d) is disabled" % adapter.slot + del man + return + + name = None + if (adapter.adapterType == man.constants.NetworkAdapterType_Null): + print "none adapter type detected" + return -1 + elif (adapter.adapterType == man.constants.NetworkAdapterType_Am79C970A): + name = "pcnet" + elif (adapter.adapterType == man.constants.NetworkAdapterType_Am79C973): + name = "pcnet" + elif (adapter.adapterType == man.constants.NetworkAdapterType_I82540EM): + name = "e1000" + elif (adapter.adapterType == man.constants.NetworkAdapterType_I82545EM): + name = "e1000" + elif (adapter.adapterType == man.constants.NetworkAdapterType_I82543GC): + name = "e1000" + print "adapter of '%s' type has been detected" % name + + profile_name = options.profile + if profile_name == None: + profile_name = generate_profile_name(options.proto.upper(), + options.host_port, + options.guest_port) + config = "VBoxInternal/Devices/" + name + "/" + config = config + str(adapter.slot) +"/LUN#0/Config/" + profile_name + proto = config + "/Protocol" + host_port = config + "/HostPort" + guest_port = config + "/GuestPort" + + vm.setExtraData(proto, options.proto.upper()) + vm.setExtraData(host_port, str(options.host_port)) + vm.setExtraData(guest_port, str(options.guest_port)) + + + vm.saveSettings() + man.closeMachineSession(session) + + del man + +if __name__ == "__main__": + main(sys.argv) -- cgit v1.2.3