VMware

This Question is Not Answered

1 "correct" answer available (10 pts)
22 Replies Last post: Jul 27, 2009 8:51 PM by mswarbrick77   1 2 Previous Next

How do I script changing from Bridged to NAT? posted: Jun 30, 2009 12:47 PM

Click to view mswarbrick77's profile Enthusiast 40 posts since
Jun 15, 2009

I want to be able change from Bridged to NAT on a running VM and have it take effect immediately. I need to do this with a script. How can I do it?

My host is Vista. My guest is Server 2003. I am running VMware server 2x

Click to view DougBaer's profile Expert 602 posts since
Oct 20, 2004
I am thinking here that 'immediately' is a bit relative. You will drop packets and the VM will be unavailable for a brief period of time.

In order to complete the transition, you'll have to ipconfig /release and ipconfig /renew within the guest (or something equivalent) to drop the old IP and get the new one -- assuming you're using DHCP. If not, you will have issues anyway.

The simplest course of action may be to have two NICs configured in the VM -- one NAT and one Bridged. You could then selectively activate/deactivate them within the guest without touching the VMware Server environment. Would that work for you?

If so, take a look at netsh commands within Windows:
netsh interface set interface <interface name> [ENABLED|DISABLED]


For example, to disable an interface with its default name “Local Area Connection,” run:
netsh interface set interface “Local Area Connection” DISABLED 

To enable the interface, you'd then run:
netsh interface set interface “Local Area Connection” ENABLED


This may be cleaner if you're trying to do everything within the guest OS.
Click to view DougBaer's profile Expert 602 posts since
Oct 20, 2004
I am no VIX guru, but it looks to me like VIX is intended for manipulating the guest OS from an external source. Unfortunately, this does not include messing with the settings in the VMX file. I'm not that familiar with VMware Server and its APIs, but that is more along the lines of what you are asking.

You might look into using vmware-cmd -- it lives in C:\Program Files\VMware\VMware Server, I think:
vmware-cmd (path to VMX file) setconfig ethernet0.connectionType="nat"


I am guessing that setting the connectionType to "bridged" would revert to Bridged, but I am not certain since I don't have a VMware Server machine to play with.

(Update) A little more information. You may be able to run vmware-cmd from within a VM and have it talk to the host that is running the VM. Get vmware-cmd.exe and any required support files into the guest, and use the connection options described below (put them before the path to the VMX file)
    Connection Options:
       -H <host>       specifies an alternative host (if set, -U and -P must also be set)
       -O <port>       specifies an alternative port
       -U <username>   specifies a user
       -P <password>   specifies a password


Message was edited by: DougBaer
Click to view fixitchris's profile Expert 547 posts since
Dec 8, 2006
I'm wondering if it's as simple as calling Set-NetworkAdapter cmdlet and setting the network name, VMNet0/1/2/etc... Can anyone confirm?

Looking at my Virtual Network Editor in Server2.0, VMnet0 is bridged, VMnet1 is host-only and VMnet8 is nat....

http://blogs.vmware.com/files/vi-toolkit-lab----vmworld-europe-2009.pdf

Re: How do I script changing from Bridged to NAT?

6. Jul 6, 2009 8:05 PM in response to: DougBaer
Click to view fixitchris's profile Expert 547 posts since
Dec 8, 2006
RE:
vmware-cmd (path to VMX file) setconfig ethernet0.connectionType="nat"

d/l and install http://communities.vmware.com/thread/191565?tstart=10. All you'll care about is the Console shortcut. See the pic for explanation. Do you have a specific test we could do?

Untitled.jpg
Click to view fixitchris's profile Expert 547 posts since
Dec 8, 2006
Once you have the Virtuozo Suite installed, run the Console from Vix_Virtuozo_Suite folder. Connect to your Server2.0. Select the VM. Then use R and W to read/write RUNTIME variables. (http://www.vmware.com/support/developer/vix-api/vix16_reference/lang/com/functions/WriteVariable.html)

We need a network traffic test to see if the NIC is actually changing from NAT to BRIDGED. How will you know that this change is working correctly?

My worry is that changing the variable might not be enough. If you look at your virtual network/switch configuration using the 'Manage Virtual Networks' app that comes with Server2.0, you'll see on the 'NAT' tab that, by default, VMNet8 virtual switch is used by the NAT service (http://www.extremetech.com/article2/0,2845,1156371,00.asp). So, the thing to do in this situation is either keep searching for answers inside the VI Toolkit, or assign two Virtual NICs to the VM. One on VMNet0 and one on VMNet8. Then, as suggested, use netsh to switch between the two (http://sanbarrow.com/network/commandlineguide2vnets.html)(http://communities.vmware.com/thread/133297).

Any other ideas?
Click to view fixitchris's profile Expert 547 posts since
Dec 8, 2006
Have you tried this by just changing the connectionType variable?

Do you have Powershell and VI Toolkit installed... We can try something once you do.
Click to view fixitchris's profile Expert 547 posts since
Dec 8, 2006
With the VI toolkit there is a CMDLETs reference.

I would try playing with VMNET0/VMNET8 with Set-VirtualSwitch , Set-NetworkAdapter to begin with.

Here is how you connect to your server:

$VMCore = Get-PSSnapin VMware.VimAutomation.Core -EA 0
if ( -not $VMCore ) { Add-PSSnapin VMware.VimAutomation.Core };

[void][http://Reflection.Assembly|http://Reflection.Assembly]::LoadWithPartialName("VMware.VimAutomation.Core")
[void][http://Reflection.Assembly|http://Reflection.Assembly]::LoadWithPartialName("VMware.Vim")
[void][http://Reflection.Assembly|http://Reflection.Assembly]::LoadWithPartialName("VMware.VimAutomation.Types")
[void][http://Reflection.Assembly|http://Reflection.Assembly]::LoadWithPartialName("VMware.VimAutomation")
[void][http://Reflection.Assembly|http://Reflection.Assembly]::LoadWithPartialName("VMware.VimAutomation.Common")
[void][http://Reflection.Assembly|http://Reflection.Assembly]::LoadWithPartialName("VMware.VimAutomation.Client20")
[void][http://Reflection.Assembly|http://Reflection.Assembly]::LoadWithPartialName("VMware.Security.CredentialStore")


$chost = "192.168.1.1";
$cuser = "me";
$cpassword = "mypass";
$cport = 8333;
$cprotocol = "https";

Connect-VIServer -Server $chost -User $cUser -Password $cPassword -Port $cport -Protocol $cprotocol;

if ( $DefaultVIServer -eq $null) 
	{ Write-Host "Connection Failed!"; return $false }
else
	{ Write-Host "Connected : " $DefaultVIServer.IsConnected;
				## Lets assign some views to variables
				$global:moServiceInstance = (get-view ServiceInstance);
				$global:moHostSystem = Get-View -VIObject (Get-VMHost);
				$global:DefaultVIServer = $DefaultVIServer;
	};

#do something

Disconnect-VIServer -Server $global:DefaultVIServer -WhatIf:$False -Confirm:$false -EA 0;

Developer Social Media

Communities