Skip to content

Instantly share code, notes, and snippets.

@0xjac
Last active April 19, 2016 09:29
Show Gist options
  • Save 0xjac/513527f114d44c94c2b5d8c3255f41de to your computer and use it in GitHub Desktop.
Save 0xjac/513527f114d44c94c2b5d8c3255f41de to your computer and use it in GitHub Desktop.

Revisions

  1. 0xjac revised this gist Apr 19, 2016. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions config
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    Host aca-lab
    Hostname 127.0.0.1
    Port 3022
    User lab
  2. 0xjac created this gist Apr 19, 2016.
    43 changes: 43 additions & 0 deletions acalab
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    #!/usr/bin/env bash

    HOSTNAME="aca-lab"
    VMNAME="aca-lab"
    HOST_PORT=3022
    SSH_PORT=22

    function cleanup {
    # Poweroff the machine on exit or failure
    VBoxManage controlvm "$VMNAME" poweroff
    }

    trap cleanup EXIT SIGINT SIGTERM

    function config {
    # Setup SSH port forwarding if not setup
    VBoxManage showvminfo "$VMNAME" | grep -q \
    "Rule.*host\ port\ = $HOST_PORT.*guest\ port = $SSH_PORT";
    local port_unset=$?;
    echo "Port: $port_unset"
    if [[ $port_unset -eq 1 ]]; then
    echo "Setting SSH port forwarding"
    echo "VBoxManage modifyvm \"$VMNAME\" --natpf1 \"ssh,tcp,,$HOST_PORT,,$SSH_PORT\""
    VBoxManage modifyvm "$VMNAME" --natpf1 "ssh,tcp,,$HOST_PORT,,$SSH_PORT"
    fi
    }

    function init {
    # Start the VM
    VBoxManage startvm "$VMNAME" --type headless;
    local status=$?
    if [ $status -ne 0 ]; then
    echo "Failed to start the machine" >&2
    exit $status
    else
    echo "Booting up, this might take a while..."
    fi

    }

    config;
    init;
    ssh $HOSTNAME