Skip to content

Instantly share code, notes, and snippets.

@micheleb
Last active February 8, 2020 09:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save micheleb/08fecb36794cb64d5f72 to your computer and use it in GitHub Desktop.
Save micheleb/08fecb36794cb64d5f72 to your computer and use it in GitHub Desktop.
A script to list all existing NAT forwarding rules in VirtualBox
#!/bin/bash
#
# vm_nmap
# Created on 2013/07/16 MB
#
# Lists all configured port forwarding rules for all VirtualBox VMs found
# in the caller user's home.
#
# If called from user root, lists all port forwarding rules for all VMs
# in all homes.
user="$(whoami)"
OLD_IFS=$IFS
IFS='
'
function scan_vm_folder() {
vm="$1"
if [[ -d "$vm" ]]; then
for vbox in "$vm"/*.vbox; do
if [[ -f "$vbox" ]]; then
echo
echo "$vbox"
xmlstarlet sel -N x="http://www.innotek.de/VirtualBox-settings" -T -t -m "//x:Machine/x:Hardware/x:Network/x:Adapter/x:NAT/x:Forwarding" -v "concat(@name, ' - host:', @hostport, ' guest:', @guestport)" -n "$vbox"
echo "DISABLED RULES:"
xmlstarlet sel -N x="http://www.innotek.de/VirtualBox-settings" -T -t -m "//x:Machine/x:Hardware/x:Network/x:Adapter/x:DisabledModes/x:NAT/x:Forwarding" -v "concat(@name, ' - host:', @hostport, ' guest:', @guestport)" -n "$vbox"
echo
fi
done
fi
}
function scan_home() {
home="$1"
# old virtual box
for vm in "$home"/.VirtualBox/Machines/*; do
scan_vm_folder "$vm"
done
# new virtual box
for vm in "$home"/VirtualBox\ VMs/*; do
scan_vm_folder "$vm"
done
}
if [[ "$user" == "root" ]]; then
echo "Listing all VMs in all homes..."
for home in $(grep bash /etc/passwd | awk -F':' '{print $6}'); do
echo "searching $home..."
scan_home "$home"
done
else
echo "Listing all VMs for user $user...."
scan_home "/home/$user"
fi
IFS=$OLD_IFS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment