Skip to content

Instantly share code, notes, and snippets.

@furandon-pig
Last active August 29, 2015 14:14
Show Gist options
  • Save furandon-pig/f101b5f3f7eea8284378 to your computer and use it in GitHub Desktop.
Save furandon-pig/f101b5f3f7eea8284378 to your computer and use it in GitHub Desktop.
VirtualBoxでVM毎のポートフォワーディング設定を一覧表示するシェルスクリプトです。
#!/bin/sh
# VirtualBoxでVM毎のポートフォワーディング設定を一覧表示するシェルスクリプトです。
# NATのホスト側ポート番号が重複していないかの確認用に利用します。
#
# "-t"オプションを付与するとHTMLで結果を出力するので、w3m等で
# 見やすく整形できます。
# $ ./list_port_forward_setting.sh -t | w3m -T text/html
if [ "$1" = "-t" ]; then
html_table=1
else
html_table=0
fi
if [ $html_table -ne 0 ]; then
echo "<table border=\"1\">"
echo "<tr><td>VM name</td><td>host port</td><td>guest port</td></tr>"
else
echo "#VM name,host port,guest port"
fi
# "nbsd615_test2" {5dc8a64d-c132-4d40-8155-e62f6e47b4f3}
for uuid in `VBoxManage list vms | sed -e "s/^.*{/{/"`
do
# VMの名前(表示名)を切り出す。
vm_name=`VBoxManage showvminfo $uuid | head -n1 | sed -e "s/Name: //"`
# Nat Ruleからhost/guestのssh ruleを切り出す。
nat_rule=`VBoxManage showvminfo $uuid | grep 'NIC [0-9]* Rule'`
# NAT Ruleは以下のような文字列を想定している。
# NIC 1 Rule(0): name = ssh, protocol = tcp, host ip = , host port = 2225, guest ip = , guest port = 22
host_port=`echo $nat_rule | sed -e "s/^.*host port = //" -e "s/,.*$//"`
guest_port=`echo $nat_rule | sed -e "s/^.*guest port = //" -e "s/,*$//"`
if [ $html_table -ne 0 ]; then
echo "<tr><td>$vm_name</td><td>$host_port</td><td>$guest_port</td></tr>"
else
echo "$vm_name,$host_port,$guest_port"
fi
done
if [ $html_table -ne 0 ]; then
echo "</table>"
fi
# Copyright (c) 2015, @furandon_pig
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the
# distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment