VMware Communities > VMware Developer > Forums > VIX API > Discussions

This Question is Not Answered

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

How do I script changing from Bridged to NAT?

Jun 30, 2009 12:47 PM

Click to view mswarbrick77's profile Enthusiast mswarbrick77 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

Reply Re: How do I script changing from Bridged to NAT? Jul 3, 2009 8:40 AM
Click to view DougBaer's profile Expert DougBaer 600 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.
Reply Re: How do I script changing from Bridged to NAT? Jul 3, 2009 9:03 AM
in response to: DougBaer
Click to view mswarbrick77's profile Enthusiast mswarbrick77 40 posts since
Jun 15, 2009

That is a helpful suggestion. I had not thought of two NICs, yes that would do it. I am familiar with netsh and am already using that in what I am doing, as I am switching from DHCP to STATIC with that command, and yes, I did not mean "immediate" to be exactly literal.

I would llike to find a way to do it though on the same NIC. I wish there was a command to order the infrastructure to make the switch.

Reply Re: How do I script changing from Bridged to NAT? Jul 4, 2009 11:12 AM
in response to: mswarbrick77
Click to view DougBaer's profile Expert DougBaer 600 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
Reply Re: How do I script changing from Bridged to NAT? Jul 4, 2009 11:19 AM
in response to: DougBaer
Click to view mswarbrick77's profile Enthusiast mswarbrick77 40 posts since
Jun 15, 2009
Thanks for the helpful answer. Unfortunately that command is not available in Windows with Server 2.x. Also, I need this command to run from inside the VM. I think you answer is close to the right track, but I'm not quite there yet. Any more suggestions? It seems that no one out there knows how to make this happen, but I am still hoping.
Reply Re: How do I script changing from Bridged to NAT? Jul 6, 2009 6:41 PM
in response to: mswarbrick77
Click to view fixitchris's profile Expert fixitchris 531 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
Reply Re: How do I script changing from Bridged to NAT? Jul 6, 2009 8:05 PM
in response to: DougBaer
Click to view fixitchris's profile Expert fixitchris 531 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
Reply Re: How do I script changing from Bridged to NAT? Jul 6, 2009 10:51 PM
in response to: fixitchris
Click to view mswarbrick77's profile Enthusiast mswarbrick77 40 posts since
Jun 15, 2009

Wow - I think you are on to the answer here. I downloaded and installed all of that and have the VIX Virtuozo Client available. But I don't know what to do next. A specific test? Well, here is what needs to happen. From a command line inside a VM, run a script that changes the NIC of that VM from Bridged to NAT, and another script that does the reverse, changes it from NAT back to Bridged.


Reply Re: How do I script changing from Bridged to NAT? Jul 7, 2009 5:44 AM
in response to: mswarbrick77
Click to view fixitchris's profile Expert fixitchris 531 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?
Reply Re: How do I script changing from Bridged to NAT? Jul 7, 2009 9:50 AM
in response to: fixitchris
Click to view mswarbrick77's profile Enthusiast mswarbrick77 40 posts since
Jun 15, 2009
A good test is this: If it switched to NAT, then the VM can see the real network and map drives to machines on the physical network, but no machines on the physical network can see a VM that is on NAT. If it is bridged, then machines on the physical network can see and map drives to the VM. I think that is the ultimate test.
Reply Re: How do I script changing from Bridged to NAT? Jul 8, 2009 5:42 AM
in response to: mswarbrick77
Click to view fixitchris's profile Expert fixitchris 531 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.
Reply Re: How do I script changing from Bridged to NAT? Jul 8, 2009 9:48 AM
in response to: fixitchris
Click to view mswarbrick77's profile Enthusiast mswarbrick77 40 posts since
Jun 15, 2009
I will install those right now. Do you know the URL link to download them? Tell me how to change the connectiion type varialble or any other command you want me to try and I'll try this right away.
Reply Re: How do I script changing from Bridged to NAT? Jul 8, 2009 6:39 PM
in response to: fixitchris
Click to view mswarbrick77's profile Enthusiast mswarbrick77 40 posts since
Jun 15, 2009
Ok, I now have Windows Powershell 1.0 and VMware vSphere PowerCLI installed? Is this correct? Do I have the right things installed? Bear in mind I am not running ESX, but am running Windows Vista 64-bit as the host with VMware Server 2.x and am running 2003 Server in a VM as the guest. What do I do now?
Reply Re: How do I script changing from Bridged to NAT? Jul 9, 2009 7:15 AM
in response to: mswarbrick77
Click to view fixitchris's profile Expert fixitchris 531 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;

Reply Re: How do I script changing from Bridged to NAT? Jul 9, 2009 6:41 PM
in response to: fixitchris
Click to view mswarbrick77's profile Enthusiast mswarbrick77 40 posts since
Jun 15, 2009

I wasn't sure what to do with that code. I put it in a batch file, but those don't run in powershell apparently. I then pasted it directly into the powershell windw. Is that right? This is what I get when I do that. I'm a bit lost here. I appreciate your help...

PS C:\Documents and Settings\Administrator> $VMCore = Get-PSSnapin VMware.VimAutomation.Core -EA 0
PS C:\Documents and Settings\Administrator> if ( -not $VMCore ) { Add-PSSnapin VMware.VimAutomation.Core };
PS C:\Documents and Settings\Administrator>
PS C:\Documents and Settings\Administrator> voidhttp://Reflection.Assembly::LoadWithParti
alName("VMware.VimAutomation.Core")
Unable to find type http://Reflection.Assembly: make sure that the assembly containing thi
s type is loaded.
At line:1 char:62
+ voidhttp://Reflection.Assembly: <<<< :LoadWithPartialName("VMware.VimAutomation.Core")
PS C:\Documents and Settings\Administrator> voidhttp://Reflection.Assembly::LoadWithParti
alName("VMware.Vim")
Unable to find type http://Reflection.Assembly: make sure that the assembly containing thi
s type is loaded.
At line:1 char:62
+ voidhttp://Reflection.Assembly: <<<< :LoadWithPartialName("VMware.Vim")
PS C:\Documents and Settings\Administrator> voidhttp://Reflection.Assembly::LoadWithParti
alName("VMware.VimAutomation.Types")
Unable to find type http://Reflection.Assembly: make sure that the assembly containing thi
s type is loaded.
At line:1 char:62
+ voidhttp://Reflection.Assembly: <<<< :LoadWithPartialName("VMware.VimAutomation.Types"
)
PS C:\Documents and Settings\Administrator> voidhttp://Reflection.Assembly::LoadWithParti
alName("VMware.VimAutomation")
Unable to find type http://Reflection.Assembly: make sure that the assembly containing thi
s type is loaded.
At line:1 char:62
+ voidhttp://Reflection.Assembly: <<<< :LoadWithPartialName("VMware.VimAutomation")
PS C:\Documents and Settings\Administrator> voidhttp://Reflection.Assembly::LoadWithParti
alName("VMware.VimAutomation.Common")
Unable to find type http://Reflection.Assembly: make sure that the assembly containing thi
s type is loaded.
At line:1 char:62
+ voidhttp://Reflection.Assembly: <<<< :LoadWithPartialName("VMware.VimAutomation.Common
")
PS C:\Documents and Settings\Administrator> voidhttp://Reflection.Assembly::LoadWithParti
alName("VMware.VimAutomation.Client20")
Unable to find type http://Reflection.Assembly: make sure that the assembly containing thi
s type is loaded.
At line:1 char:62
+ voidhttp://Reflection.Assembly: <<<< :LoadWithPartialName("VMware.VimAutomation.Client
20")
PS C:\Documents and Settings\Administrator> voidhttp://Reflection.Assembly::LoadWithParti
alName("VMware.Security.CredentialStore")
Unable to find type http://Reflection.Assembly: make sure that the assembly containing thi
s type is loaded.
At line:1 char:62
+ voidhttp://Reflection.Assembly: <<<< :LoadWithPartialName("VMware.Security.CredentialS
tore")
PS C:\Documents and Settings\Administrator>
PS C:\Documents and Settings\Administrator>
PS C:\Documents and Settings\Administrator> $chost = "192.168.0.4";
PS C:\Documents and Settings\Administrator> $cuser = "mswarbrick";
PS C:\Documents and Settings\Administrator> $cpassword = "cnecne997";
PS C:\Documents and Settings\Administrator> $cport = 8333;
PS C:\Documents and Settings\Administrator> $cprotocol = "https";
PS C:\Documents and Settings\Administrator>
PS C:\Documents and Settings\Administrator> Connect-VIServer -Server $chost -User $cUser -Password $cPassword -Port $cpo
rt -Protocol $cprotocol;
WARNING: There were one or more problems with the server certificate:

  • A certification chain processed correctly, but terminated in a root certificate which isn't trusted by the trust
provider.

  • The certificate's CN name does not match the passed value.


IsConnected : True
Name : 192.168.0.4
Port : 8333
SessionId : A4FA03DA-B4B1-4336-8FF4-15B864F79EA0
User : mswarbrick


PS C:\Documents and Settings\Administrator>
PS C:\Documents and Settings\Administrator> 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;
};
Connected : True
PS C:\Documents and Settings\Administrator> #do something
PS C:\Documents and Settings\Administrator>
PS C:\Documents and Settings\Administrator> Disconnect-VIServer -Server $global:DefaultVIServer -WhatIf:$False -Confirm:
$false -EA 0;
PS C:\Documents and Settings\Administrator>
1 2 Previous Next
Actions