VMware Cloud Community
Pilu1978
Enthusiast
Enthusiast

Assign IP to vmk interface

Hi,

I would like to assign IP, subnet mask and gateway to vmk (specifically vmk5 interface)  interface on each esx host as shown below using a workflow. Could anybody please share the javascript code to assign IP to vmkernel adapter. Please help.

Pilu1978_0-1614764374233.png

 

 

 

Reply
0 Kudos
1 Reply
Pilu1978
Enthusiast
Enthusiast

After lot of findings I found the code. I have written it as a function so that I can avoid writing same code multiple times:

The below function will create a new vmk adapter and assign the portgroup and Ip address to it. If you want to add ip to a existing vmk interface then replace:

var newVnic = hostConfigManager.addVirtualNic(portGroup, hostNicSpec); 

with

var newVnic = hostConfigManager.updateVirtualNic(portGroup, hostNicSpec);

Hope it helps.

function addvmk(gw, ip, mask, nsikey, dvsid, pgkey, mtu, esxhost)

{
var portGroup = "";
var hostNicSpec = new VcHostVirtualNicSpec();
hostNicSpec.ipRouteSpec = new VcHostVirtualNicIpRouteSpec();
hostNicSpec.ipRouteSpec.ipRouteConfig = new VcHostIpRouteConfig();
hostNicSpec.ipRouteSpec.ipRouteConfig.defaultGateway = gw.trim();
hostNicSpec.ip = new VcHostIpConfig();
hostNicSpec.ip.ipAddress = ip.trim();
hostNicSpec.ip.subnetMask = mask.trim();
hostNicSpec.ip.dhcp = false;
hostNicSpec.netStackInstanceKey = nsikey;
hostNicSpec.distributedVirtualPort = new VcDistributedVirtualSwitchPortConnection();
hostNicSpec.distributedVirtualPort.switchUuid = dvsid
hostNicSpec.distributedVirtualPort.portgroupKey = pgkey
hostNicSpec.mtu = mtu;
var hostConfigManager = esxhost.configManager.networkSystem;
var newVnic = hostConfigManager.addVirtualNic(portGroup, hostNicSpec);
System.sleep(1000)

}

Reply
0 Kudos