VMware Cloud Community
SOMVCOE
Contributor
Contributor
Jump to solution

Add ip route in vRO

I am trying to add a new route to our ESXi host using vRO. I've tried a few different things, but nothing seems to be working. My current script successfully launches a task to 'update the IP route table configuration', but the changes are not applied.

Before anyone suggests it, I am already calling a powershell script to use the 'New-VMHostRoute' cmdlet, but I want to do this via the API, and remove powershell altogether.

Has anyone done this before?

Current script:

var configmanager = host.configManager;
var networkSystem = configmanager.networkSystem;
var netStackArray = networkSystem.networkConfig.netStackSpec;


for each (var netStack in netStackArray) {

System.log(netStack.netStackInstance.name);
if (netStack.netStackInstance.name == "defaultTcpipStack") {
  var routeTableConfig = netStack.netStackInstance.routeTableConfig;
 
  //Build IP route entry
  myRoute = new VcHostIpRouteEntry() ;
  myRoute.deviceName = "vmk0" ;
  myRoute.gateway = "192.168.1.1";
  myRoute.network = "192.168.1.0";
  myRoute.prefixLength = 24 ;
 
  var myVcHostIpRouteOp = new VcHostIpRouteOp() ;
  myVcHostIpRouteOp.changeOperation = "add" ;
  myVcHostIpRouteOp.route = myRoute ;
 
  // Add Ip route entry to the array of routes
  routeTableConfig.ipRoute.push(myVcHostIpRouteOp);
 
  // Apply change to host
  networkSystem.updateIpRouteTableConfig(routeTableConfig);
 

}
}

0 Kudos
1 Solution

Accepted Solutions
iiliev
VMware Employee
VMware Employee
Jump to solution

Something like the following should work. The main difference compared to your code is how the new route entry is added - instead of pushing it to the existing array with routes (such modification of existing arrays may not work as expected), a new array is prepared combining the new route + existing route entries.

var configmanager = host.configManager;

var networkSystem = configmanager.networkSystem;

var netStackArray = networkSystem.networkConfig.netStackSpec;

for each (var netStack in netStackArray) {

System.log(netStack.netStackInstance.name);

if (netStack.netStackInstance.name == "defaultTcpipStack") {

  var routeTableConfig = netStack.netStackInstance.routeTableConfig;

 

  //Build IP route entry

  myRoute = new VcHostIpRouteEntry() ;

  myRoute.deviceName = "vmk0" ;

  myRoute.gateway = "192.168.1.1";

  myRoute.network = "192.168.1.0";

  myRoute.prefixLength = 22 ;

 

  var myVcHostIpRouteOp = new VcHostIpRouteOp() ;

  myVcHostIpRouteOp.changeOperation = "add" ;

  myVcHostIpRouteOp.route = myRoute ;

 

  // Add Ip route entry to the array of routes

  var config = new VcHostIpRouteTableConfig([myVcHostIpRouteOp].concat(routeTableConfig.ipRoute), routeTableConfig.ipv6Route);

 

  // Apply change to host

  networkSystem.updateIpRouteTableConfig(config);

}

}

View solution in original post

2 Replies
iiliev
VMware Employee
VMware Employee
Jump to solution

Something like the following should work. The main difference compared to your code is how the new route entry is added - instead of pushing it to the existing array with routes (such modification of existing arrays may not work as expected), a new array is prepared combining the new route + existing route entries.

var configmanager = host.configManager;

var networkSystem = configmanager.networkSystem;

var netStackArray = networkSystem.networkConfig.netStackSpec;

for each (var netStack in netStackArray) {

System.log(netStack.netStackInstance.name);

if (netStack.netStackInstance.name == "defaultTcpipStack") {

  var routeTableConfig = netStack.netStackInstance.routeTableConfig;

 

  //Build IP route entry

  myRoute = new VcHostIpRouteEntry() ;

  myRoute.deviceName = "vmk0" ;

  myRoute.gateway = "192.168.1.1";

  myRoute.network = "192.168.1.0";

  myRoute.prefixLength = 22 ;

 

  var myVcHostIpRouteOp = new VcHostIpRouteOp() ;

  myVcHostIpRouteOp.changeOperation = "add" ;

  myVcHostIpRouteOp.route = myRoute ;

 

  // Add Ip route entry to the array of routes

  var config = new VcHostIpRouteTableConfig([myVcHostIpRouteOp].concat(routeTableConfig.ipRoute), routeTableConfig.ipv6Route);

 

  // Apply change to host

  networkSystem.updateIpRouteTableConfig(config);

}

}

SOMVCOE
Contributor
Contributor
Jump to solution

Thank you Ilian Iliev. That worked like a charm.

0 Kudos