-
-
Save vpiserchia/e42366dfff5a23276ddba03700c0ba7f to your computer and use it in GitHub Desktop.
A script to list all existing NAT forwarding rules in VirtualBox
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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