Code Tips and Snippets


Not yet member?
Click here to register.
Only members can post comments
VirtualBox bridged mode: automatically nic select and VM start
Posted by pk on Sunday, October 09 2016 - 10:47:11
2 stars levelC LanguageLinux operating systemWindows operating system

If like me, you use a VirtualBox VM for developing server applications, you certainly have configured your VM with your network interface in bridged mode.

When you have to move your VM on a Laptop PC, and after that, each time you switch from a wired connection to a Wireless one, you definitely have been annoyed, having to update the network adapter on which the network is bridged.

You will see in this post how to develop a mini C program that allows you to switch automatically to the correct network interface and then start the VM.

The solution is to write a little piece of code that calls the vboxmanage command of  VirtualBox.

This command will be used successively with 3 different parameters:

  1. List all available network interfaces:
    vboxmanage list bridgedifs
  2. Having selected the wanted interface ( we take the 1st wired interface if exists, otherwise the 1st wireless one), we update the VM with it:
    vboxmanage modifyvm "VM Name" --bridgeadapter1 "nic name"
  3. We the launch our VM:
    vboxmanage startvm "VM Name"

You can retrieve on GitHub a C source code targeting a Windows Host.
you can even get an already compiled version (yakpro-vm-start.exe).

It is very easy to adapt this source code for Linux Hosts (you can even write a small php script,or whatever language you like to do the job).

If you enjoyed this post, a small star on GitHub will make me happy …

 

yakpro rulez!

Site has been updated on Saturday, September 21 2024 - 13:55:03