13 Replies Latest reply on Sep 21, 2018 12:01 AM by FineCloud

    How to update IP and Network adapter using Java

    stephanl Lurker

       

      Greetings,

       

       

      would anybody have a working example on how to update the IP address and network adapter of an existing VM?  We are using ESX Server 3.5, and the VI SDK 2.5.0.  Similar to the code provided in the samples for VMpowerOps.java, where I provide the datacenter and name of the VM, along with the desired IP and network adapter name.  Context: I am setting up a fully automated test automation environment, where VMs get deployed on-demand, as requests for test execution are submitted.  As the VMs get deployed from previously built templates, I need to set the IP and network adapter (using the value displayed for the 'Network label' in VM Properties) programmatically.

       

       

      Thanks in advance!

       

       

      - Stephan

       

       

        • 1. Re: How to update IP and Network adapter using Java
          Expert

          To set IP, you can use CustomizationSpec, below is the code snippet for reference.

          CustomizationSpec custSpec = new CustomizationSpec();

                CustomizationAdapterMapping[] custAdapMapList = new CustomizationAdapterMapping[1];

                CustomizationAdapterMapping custAdapMap = new CustomizationAdapterMapping();

                 

                CustomizationIPSettings  custIPSettings = new CustomizationIPSettings();

                 

                CustomizationFixedIp  custFixedIp = new CustomizationFixedIp();

           

          String ipAddress = "192.168.111.99";

                custFixedIp.setIpAddress(ipAddress);

                custIPSettings.setIp(custFixedIp);

                custAdapMap.setAdapter(custIPSettings);

                custAdapMapList[0] = custAdapMap;

                custSpec.setNicSettingMap(custAdapMapList);

                 

                 

                CustomizationGlobalIPSettings custGlobalIPSetting = new CustomizationGlobalIPSettings();

                custSpec.setGlobalIPSettings(custGlobalIPSetting);

                 

                 

                CustomizationIdentitySettings custIdentitySet = new      CustomizationIdentitySettings();

                CustomizationSysprep custSysprep = new CustomizationSysprep();

                      

                CustomizationGuiUnattended guiUnattended = new CustomizationGuiUnattended();

                guiUnattended.setAutoLogon(true);

                guiUnattended.setAutoLogonCount(0);

                guiUnattended.setTimeZone(190);

                 

                CustomizationPassword custPasswd = new CustomizationPassword();

                custPasswd.setPlainText(true);

                custPasswd.setValue("user");

                 

                CustomizationIdentification custIdentification = new CustomizationIdentification();

                custIdentification.setDomainAdmin("user");

                custIdentification.setDomainAdminPassword(custPasswd);

                custIdentification.setJoinDomain("vmware"); 

                 

                CustomizationUserData custUserData = new CustomizationUserData();

                //CustomizationName custname = new CustomizationName();

                CustomizationFixedName custFixedName = new CustomizationFixedName();

                custFixedName.setName("mycomputer");

                custUserData.setComputerName(custFixedName);

                custUserData.setFullName("sjain");

                custUserData.setOrgName("vmware");

                custUserData.setProductId("XXXXX-XXXXX-XXXXX-XXXXX-XXXXX");

                 

                       

                custSysprep.setUserData(custUserData);

                custSysprep.setGuiUnattended(guiUnattended);

                custSysprep.setIdentification(custIdentification);

                custSpec.setIdentity(custSysprep);

                 

                 

                cloneSpec.setCustomization(custSpec);

           

           

           

               And to update Network Adapter, refer attached script "VMCloneAddAdapter.java".

          Change the line# 92 with the name of Network Adapter you wanted to specify.

           

          String MyNetwork = "MyNetwork";

          • 2. Re: How to update IP and Network adapter using Java
            stephanl Lurker

             

            Thank you Seemankij, that is very helpful.

             

             

            Two follow-up questions, if you don't mind ....

             

            • to change the IP, do I need the code below from the line CustomizationIdentitySettings custIdentitySet = new CustomizationIdentitySettings(); onward?

            • the last line, where the customization spec is set on the cloneSpec object suggests the IP change to performed as part of a VMClone operation, as performed in the attached sample code and VMClone.java in the SDK samples.  How do I change the IP on an existing VM without performing a clone operation?

             

             

            Thanks again!

             

             

            - Stephan  

             

             

            • 3. Re: How to update IP and Network adapter using Java
              Expert

              Hi Stephan,

               

              You have to begin from making a CustomizationSpec

               

              CustomizationSpec custSpec = new CustomizationSpec();

               

              and you can also customize a existing Virtual Machine calling CustomizeVM_Task and passing the above mentioned CustomizationSpec along with Virtual machines'  reference. Earlier I kept the reference of Cloning operation, as you had mentioned that you were trying to deploy vms on demand.

               

              Seemanki

              • 4. Re: How to update IP and Network adapter using Java
                stephanl Lurker

                 

                Hmm ... using the most recent version of ESXServer available (3.5), I'm getting an error when running the code provided.  See http://communities.vmware.com/thread/125811 for reference.  The error is

                 

                 

                Message Customization of the guest operating system 'rhel4Guest' is not supported in this configuraton. Microsoft Vista (TM) and Linux guests with Logical Volume Manager are supported only for recent ESX host and VMware Tools versions. Please refer to Virtual Center documentation for supported confgurations.

                 

                 

                Could you provide me with details if that code has been executed successfully, if so on what platform(s), e.g. VM OS, ESX Server version, etc?

                 

                 

                 

                 

                 

                Thanks,

                 

                 

                 

                 

                 

                - Stephan

                 

                 

                • 5. Re: How to update IP and Network adapter using Java
                  mcowger Champion

                   

                  ESX doesn't support guest customization of RHEL4 guests. 

                   

                   

                   

                  --Matt

                  • 6. Re: How to update IP and Network adapter using Java
                    stephanl Lurker

                     

                    Great.  So then, is there a 'recommended' way/mechanism to customize RHEL4 clients?

                     

                     

                    Thanks,

                     

                     

                    - Stephan

                     

                     

                    • 7. Re: How to update IP and Network adapter using Java
                      mcowger Champion

                      We kickstart install all our VMs.

                       

                       

                       

                       

                      --Matt

                      • 8. Re: How to update IP and Network adapter using Java
                        elfner Lurker

                         

                        Hi - would you be able to provide a complete example using the Perl toolkit?

                         

                         

                        We have a need to toggle IPs of our VMs in our provisioning environment.

                         

                         

                         

                         

                         

                        Thanks...

                         

                         

                        • 9. Re: How to update IP and Network adapter using Java
                          mehendran Novice

                           

                          Hi,

                           

                           

                          I followed the same procedure using VISDK as in CreateVM.pl. It is not working. Kindly share if you have any samples.

                           

                           

                          Thanks,

                           

                           

                          Mehendran

                           

                           

                           

                           

                           

                          • 10. Re: How to update IP and Network adapter using Java
                            mehendran Novice

                             

                            Hi,

                             

                             

                            When I do customization through CloneVM_Task by passing

                            customization spec, the adapter settings don't take effect in the newly

                            created vm.

                             

                             

                            I would appreciate if anyone could help me proceed with it.

                             

                             

                            
                            
                            cloneSpec.setCustomization(customization);
                            
                            service.cloneVM_Task(vmmor, vmFolderRef, clonedName, cloneSpec);
                            

                             

                             

                            public static CustomizationSpec getCustomizationSettings(Properties props, CustomizationSpec customization) throws Exception
                                {
                             
                                    CustomizationFixedName fixedName = new CustomizationFixedName();
                             
                                    String linuxGlobalComputerName_str = props.getProperty("linuxglobal.ComputerName");
                                    String linuxGlobalDomain = props.getProperty("linuxglobal.Domain");
                                    if (linuxGlobalComputerName_str != null) {
                                        CustomizationLinuxPrep clp = new CustomizationLinuxPrep();
                                        fixedName.setName(linuxGlobalComputerName_str);
                                        
                                        clp.setHostName(fixedName);
                                        
                                        if (linuxGlobalDomain != null) {
                                            clp.setDomain(linuxGlobalDomain);
                                        }
                                        customization.setIdentity(clp);
                             
                                    } else {
                             
                                        String changeSID_str = props.getProperty("options.changeSID","true");
                                        String deleteAccounts_str = props.getProperty("options.deleteAccounts");
                                        Boolean changeSID = Boolean.TRUE;
                                        if (changeSID_str != null) {
                                            changeSID = Boolean.valueOf(changeSID_str);
                                        }
                             
                             
                                            CustomizationWinOptions cwo = new CustomizationWinOptions();
                                        cwo.setChangeSID(changeSID);
                                        Boolean deleteAccounts = null;
                                        if (deleteAccounts_str != null) {
                                            deleteAccounts = Boolean.valueOf(deleteAccounts_str);
                                            cwo.setDeleteAccounts(deleteAccounts);
                                        }
                             
                                        customization.setOptions(cwo);
                                        String runoncecmd = props.getProperty("sysprep.GuiRunOnce.Command");
                                        CustomizationSysprep sysprep = new CustomizationSysprep();
                                        if (runoncecmd != null) {
                                            CustomizationGuiRunOnce guiRunOnce = new CustomizationGuiRunOnce();
                                            guiRunOnce.setCommandList(new String[] {runoncecmd});
                                            sysprep.setGuiRunOnce(guiRunOnce);
                             
                                        }
                                        boolean plainText = true;
                                        Boolean autoLogon = Boolean.FALSE;
                                        Integer autoLogonCount = new Integer("0");
                             
                                        String pass = props.getProperty("sysprep.GuiUnattended.AdminPassword.value"); //,"*");
                                        String plainText_str = props.getProperty("sysprep.GuiUnattended.AdminPassword.plainText"); //,"true");
                                        String timeZone = props.getProperty("sysprep.GuiUnattended.TimeZone","004");
                                        String autoLogon_str = props.getProperty("sysprep.GuiUnattended.AutoLogon"); //,"true");
                                        String autoLogonCount_str = props.getProperty("sysprep.GuiUnattended.AutoLogonCount"); //,"1000");
                             
                                        if (pass != null && plainText_str != null) {
                                            plainText = Boolean.valueOf(plainText_str).booleanValue();
                                        }     
                             
                                        if (autoLogon_str != null) {
                                            autoLogon = Boolean.valueOf(autoLogon_str);
                                        }
                             
                                        if (autoLogonCount_str != null) {
                                            try {
                                                autoLogonCount = new Integer(autoLogon_str);
                                            } catch (NumberFormatException nfe) {
                                                System.err.println("Bad value for AutoLogonCount: " + nfe.getMessage());                 
                                            }
                                        }
                             
                                        CustomizationGuiUnattended guiUnattended = new CustomizationGuiUnattended();
                                        CustomizationPassword password = new CustomizationPassword();
                                        password.setPlainText(plainText);
                                        password.setValue(pass);
                                        guiUnattended.setPassword(password);
                                        guiUnattended.setTimeZone(new Integer(timeZone));
                                        guiUnattended.setAutoLogon(autoLogon);
                                        guiUnattended.setAutoLogonCount(autoLogonCount);
                                        sysprep.setGuiUnattended(guiUnattended);
                                        String autoMode = props.getProperty("sysprep.LicenseFilePrintData.AutoMode");
                                        String autoUsers = props.getProperty("sysprep.LicenseFilePrintData.AutoUsers");
                                        CustomizationLicenseFilePrintData licenseFilePrintData = new CustomizationLicenseFilePrintData();
                                        CustomizationLicenseDataMode licenseDataMode = null;
                                        if (autoMode != null && autoMode.length() > 0) {
                                            if (autoMode.equalsIgnoreCase("PerServer")) {
                                                licenseDataMode = CustomizationLicenseDataMode.perServer;
                                            } else if (autoMode.equalsIgnoreCase("PerSeat")) {
                                                licenseDataMode = CustomizationLicenseDataMode.perSeat;
                                            }
                                            licenseFilePrintData.setAutoMode(licenseDataMode);
                                            log.logLine("AutoMode=" + licenseDataMode);
                                            if (autoUsers != null && autoUsers.length() > 0) {
                                                Integer autoUsersInt = new Integer(autoUsers);
                                                licenseFilePrintData.setAutoUsers(autoUsersInt);
                                                log.logLine("AutoUsers=" + autoUsersInt);
                                            }
                                            sysprep.setLicenseFilePrintData(licenseFilePrintData);
                                        }
                             
                                        CustomizationUserData userData = new CustomizationUserData();
                             
                                        String uname = props.getProperty("sysprep.UserData.FullName");
                                        String orgName = props.getProperty("sysprep.UserData.OrgName");
                                        String computerName = props.getProperty("sysprep.UserData.ComputerName");
                             
                                        String productID = props.getProperty("sysprep.UserData.ProductID","");
                             
                                        userData.setFullName(uname);
                                        userData.setOrgName(orgName);
                                        fixedName.setName(computerName);
                                        userData.setComputerName(fixedName);
                                        userData.setProductId(productID);
                             
                                        sysprep.setUserData(userData);
                                        CustomizationIdentification Identification = new CustomizationIdentification();
                                        String joinWorkgroup = props.getProperty("sysprep.Identification.JoinWorkgroup","WORKGROUP");
                                        String joinDomain = props.getProperty("sysprep.Identification.JoinDomain");
                                        String domainAdmin = props.getProperty("sysprep.Identification.DomainAdmin");
                                        String domainAdminPasswordText = props.getProperty("sysprep.Identification.DomainAdminPassword.value");
                                        String domainAdminPasswordPlainText_str = props.getProperty("sysprep.Identification.DomainAdminPassword.plainText","true");
                             
                             
                                        boolean domainAdminPasswordPlainText = true;
                             
                                        if (domainAdminPasswordText != null) {
                                            domainAdminPasswordPlainText = Boolean.valueOf(domainAdminPasswordPlainText_str).booleanValue(); 
                                        }
                             
                                        if (joinDomain != null) {
                             
                                            Identification.setJoinDomain(joinDomain);
                                            if (domainAdmin != null) {
                             
                                                Identification.setDomainAdmin(domainAdmin);
                             
                                                password.setPlainText(domainAdminPasswordPlainText);
                                                password.setValue(domainAdminPasswordText);
                                                Identification.setDomainAdminPassword(password);
                             
                                            }
                                        } else {
                             
                                            Identification.setJoinWorkgroup(joinWorkgroup);
                                        }
                                        sysprep.setIdentification(Identification);
                                        customization.setIdentity(sysprep);
                                    } 
                                    int adapterNum = 0;
                                    Enumeration renum = props.propertyNames();
                                    while (renum.hasMoreElements()) {
                                        String name = (String)renum.nextElement();
                                        if (name.startsWith("adapters[" + adapterNum + "].")) {
                                            adapterNum++;
                                            break;
                                        }
                                    }
                                    System.out.println("No of Adapters = " + adapterNum);
                                    if (adapterNum == 0) {
                             
                                        CustomizationAdapterMapping[] adapter = new CustomizationAdapterMapping[1];
                                        adapter[0] = formNetworkAdapter(props, 0);
                                        customization.setNicSettingMap(adapter);
                                    } else {
                                        CustomizationAdapterMapping[] adapter = new CustomizationAdapterMapping[adapterNum];
                             
                                        for(int i = 0; i < adapterNum; i++) {
                                            adapter[i] = formNetworkAdapter(props, i);
                                        }
                                        customization.setNicSettingMap(adapter);
                             
                                    }
                             
                                    CustomizationGlobalIPSettings globalIPSettings = new CustomizationGlobalIPSettings();
                             
                                    String adapterPrefix = "adapters[0].";
                                    String list_str = props.getProperty(adapterPrefix + "DNSSuffixes");
                                    if (list_str != null) {
                                        String[] suffixes = convert_list2array(list_str);
                                        log.logLine("dnssuffixes=" + Arrays.asList(suffixes));
                                        globalIPSettings.setDnsSuffixList(suffixes);
                                    }        
                                    list_str = props.getProperty(adapterPrefix + "DNSServers");
                                    if (list_str != null) {
                                        String[] servers = convert_list2array(list_str);
                                        log.logLine("dnsservers=" + Arrays.asList(servers));                
                                        globalIPSettings.setDnsServerList(servers);
                                    }
                             
                             
                             
                                    customization.setGlobalIPSettings(globalIPSettings);
                                    System.out.println("Printing Spec Values ================== ");
                                    printValues(customization);
                                    return customization;
                                }
                            private static CustomizationAdapterMapping formNetworkAdapter(Properties props,int adapterNum) 
                                throws Exception {
                                    String MACAddress = "MAC00";
                                    Boolean UseDHCP = Boolean.TRUE;
                                    Boolean DNSFromDHCP = Boolean.TRUE;
                                    String ipaddr = null;
                                    String subnetMask = null;
                                    CustomizationIPSettings ipSettings = new CustomizationIPSettings();
                                    CustomizationAdapterMapping adapterMapping = new CustomizationAdapterMapping();
                                    String dnsDomain = null;
                                    String primaryWINS = null;
                                    String secondaryWINS = null;
                                    CustomizationNetBIOSMode netBIOS = null;
                                    String adapterPrefix = "adapters[" + adapterNum + "].";
                             
                                    MACAddress = props.getProperty(adapterPrefix + "MACAddress", MACAddress);
                                    String bool_str = props.getProperty(adapterPrefix + "UseDHCP");
                                    if (bool_str != null) {
                                        UseDHCP = Boolean.valueOf(bool_str);
                                    }
                             
                                    bool_str = props.getProperty(adapterPrefix + "DNSFromDHCP");
                                    if (bool_str != null) {
                                        DNSFromDHCP = Boolean.valueOf(bool_str);
                                    }
                             
                                    ipaddr = props.getProperty(adapterPrefix + "IPAddress");
                                    subnetMask = props.getProperty(adapterPrefix +  "subnetMask");
                             
                                    String list_str = props.getProperty(adapterPrefix + "Gateways");
                                    String[] gways = null;
                                    if (list_str != null) {
                                        gways = convert_list2array(list_str);
                                        log.logLine("gateways=" + Arrays.asList(gways));            
                                    }
                             
                                    list_str = props.getProperty(adapterPrefix + "DNSServers");
                                    if (list_str != null) {
                                        String[] servers = convert_list2array(list_str);
                                        if (servers != null) {
                                            log.logLine("dnsservers=" + Arrays.asList(servers));                
                                            ipSettings.setDnsServerList(servers);
                                        }
                                    }
                             
                                    dnsDomain = props.getProperty(adapterPrefix + "DNSDomain");
                                    primaryWINS = props.getProperty(adapterPrefix + "primaryWINS");
                                    secondaryWINS = props.getProperty(adapterPrefix + "secondaryWINS");
                             
                                    String netBIOS_str = props.getProperty(adapterPrefix + "netBios");
                                    if (netBIOS_str != null) {
                                        try {
                                            if (netBIOS_str.equalsIgnoreCase("EnableNetBIOSViaDhcp")) {
                                                netBIOS = CustomizationNetBIOSMode.enableNetBIOSViaDhcp;
                                            } else if (netBIOS_str.equalsIgnoreCase("EnableNetBIOS")) {
                                                netBIOS = CustomizationNetBIOSMode.enableNetBIOS;
                                            } else if (netBIOS_str.equalsIgnoreCase("DisableNetBIOS")) {
                                                netBIOS = CustomizationNetBIOSMode.disableNetBIOS;
                                            }
                             
                                        } catch(IllegalStateException ise) {
                                            throw new Exception("Illegal NetBIOS value: " + netBIOS_str);
                                        }
                                    }
                                    adapterMapping.setMacAddress(MACAddress);
                                    if (gways != null) {
                                        ipSettings.setGateway(gways);
                                    }
                                        if (UseDHCP) {
                                            CustomizationDhcpIpGenerator ipGen = new CustomizationDhcpIpGenerator();
                                            ipSettings.setIp(ipGen);
                                        } else {
                                            if (ipaddr != null) {
                                                CustomizationFixedIp fixedIp = new CustomizationFixedIp();
                                                fixedIp.setIpAddress(ipaddr);
                                                ipSettings.setIp(fixedIp);
                                            }
                                        }
                             
                                    if (subnetMask != null) {
                                        ipSettings.setSubnetMask(subnetMask);             
                                    }        
                                    if (dnsDomain != null) {
                                        ipSettings.setDnsDomain(dnsDomain);
                             
                                    }
                                    if (primaryWINS != null) {
                                        ipSettings.setPrimaryWINS(primaryWINS);
                                    }
                                    if (secondaryWINS != null) {
                                        ipSettings.setSecondaryWINS(secondaryWINS);
                                    }
                                    if (netBIOS != null) {
                             
                                        ipSettings.setNetBIOS(netBIOS);
                                    }
                                    adapterMapping.setAdapter(ipSettings);
                                    return adapterMapping;            
                                }
                             
                            

                             

                             

                             

                             

                            Thanks,

                             

                             

                            Mehendran

                             

                             

                             

                             

                             

                             

                             

                             

                             

                             

                             

                            • 11. Re: How to update IP and Network adapter using Java
                              ifonul Lurker

                              Some pointer to this information?

                               

                              In my case it's ubuntu 11.10, ESX 5.0, latest VMTools. LVM vs. non-LVM makes no difference, still same (very "explicit") error.

                              And it's 2012.

                              • 12. Re: How to update IP and Network adapter using Java
                                balajiyelmar1 Lurker

                                Hi,

                                 

                                I need to set static IP address to VM while cloning it from template. Is it necessary to provide product ID and all? What will be the code for cloning and assigning IP to VM using java.

                                 

                                Thanks

                                • 13. Re: How to update IP and Network adapter using Java
                                  FineCloud Lurker

                                  Hi,

                                  I have found that different operating systems have an impact on custom settings, so you can't simply set the static IP with CustomizationSpec and CustomizeVM_Task. In the newer ESX version, the Liunx system is generally ok, but windows will have problems.
                                  The solution is to use the machine.id to store the ip related information. This information will be written to the .vmx file of the virtual machine.And it can be retrieved with vmware tools inside the virtual machine, so the virtual machine can set Up the ip, netmask, geteway, dns, etc. according to the information.

                                  The reference links are as follows:

                                  https://communities.vmware.com/thread/443727?start=0&tstart=0

                                  http://tech.lazyllama.com/2010/06/22/passing-info-from-powercli-into-your-vm-using-guestinfo-variables/

                                  yavijava-samples/VmNetworkUtil.java at gradle · yavijava/yavijava-samples · GitHub