的VMware API沒有設置來設置虛擬機客戶機操作系統的IP地址,因爲IP地址設置取決於版本的客戶機操作系統。你可以用兩種方式來做到這一點:
1)你可以使用GuestOperationsManager從VMware vSphere的API來啓動IP地址設置的腳本在客戶機操作系統。
先決條件:
- 你應該寫的IP地址設置爲每個腳本支持的操作系統(Linux操作系統,Windows等)
- 的VMware Tools必須在每個受支持的VM安裝(使用GuestOperationsManager)。
Update2。下面是在客戶操作系統上運行腳本的簡化示例。這個例子不包括錯誤處理,得到了腳本,虛擬機上電源,等
using System;
using System.IO;
using System.Net;
using Vim25Api;
namespace RunScriptOnGuestOsTest
{
class Program
{
static void Main(string[] args)
{
var program = new Program();
program.RunScriptInGuestOs(
"https://10.1.1.10/sdk",
"root",
"vmware",
"c:\\temp\\test.bat",
"vm-73",
"Administrator",
"[email protected]",
"c:\\test.bat",
String.Empty);
}
public void RunScriptInGuestOs(string vCenterUrl, string vCenterUserName, string vCenterPassword, string scriptFilePatch, string vmKey, string username, string password, string destinationFilePath, string arguments)
{
var service = CreateVimService(vCenterUrl, 600000, true);
var serviceContent = RetrieveServiceContent(service);
service.Login(serviceContent.sessionManager, vCenterUserName, vCenterPassword, null);
byte[] dataFile;
using (var fileStream = new FileStream(scriptFilePatch, FileMode.Open, FileAccess.Read))
{
dataFile = new byte[fileStream.Length];
fileStream.Read(dataFile, 0, dataFile.Length);
}
FileTransferToGuest(service, vmKey, username, password, destinationFilePath, dataFile);
RunProgramInGuest(service, vmKey, username, password, destinationFilePath, arguments);
}
private static VimService CreateVimService(string url, int serviceTimeout, bool trustAllCertificates)
{
ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true;
return new VimService
{
Url = url,
Timeout = serviceTimeout,
CookieContainer = new CookieContainer()
};
}
private ServiceContent RetrieveServiceContent(VimService service)
{
var serviceInstance = new ManagedObjectReference
{
type = "ServiceInstance",
Value = "ServiceInstance"
};
var content = service.RetrieveServiceContent(serviceInstance);
if (content.sessionManager == null)
{
throw new ApplicationException("Session manager is null.");
}
return content;
}
private void FileTransferToGuest(VimService service, string vmKey, string username, string password, string fileName, byte[] fileData)
{
var auth = new NamePasswordAuthentication { username = username, password = password, interactiveSession = false };
var vmRef = new ManagedObjectReference { type = "VirtualMachine", Value = vmKey };
var fileMgr = new ManagedObjectReference { type = "GuestFileManager", Value = "guestOperationsFileManager" };
var posixFileAttributes = new GuestPosixFileAttributes();
posixFileAttributes.ownerId = 1;
posixFileAttributes.groupId = 1;
posixFileAttributes.permissions = (long)0777; //execution file
var requestUrl = service.InitiateFileTransferToGuest(fileMgr, vmRef, auth, fileName, posixFileAttributes, fileData.Length, true);
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(requestUrl);
request.ContentType = "application/octet-stream";
request.Method = "PUT";
request.ContentLength = fileData.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileData, 0, fileData.Length);
requestStream.Close();
request.GetResponse();
}
private void RunProgramInGuest(VimService service, string vmKey, string username, string password, string programPath, string arguments)
{
var auth = new NamePasswordAuthentication { username = username, password = password, interactiveSession = false };
var vmRef = new ManagedObjectReference { type = "VirtualMachine", Value = vmKey };
var progSpec = new GuestProgramSpec { programPath = programPath, arguments = arguments };
var processMgr = new ManagedObjectReference { type = "GuestProcessManager", Value = "guestOperationsProcessManager" };
var result = service.StartProgramInGuest(processMgr, vmRef, auth, progSpec);
}
}
}
2)你可以使用DHCP服務器來管理分配IP地址的記錄。使用VMware API,您可以獲取虛擬機的MAC地址。接下來,您應該設置DHCP服務器來分配獲取的MAC地址上的所需IP地址。
先決條件:
- 每個VM必須設置爲從DHCP服務器獲取IP地址。
- DHCP服務器可以配置爲適合您的需求。
我需要能夠在虛擬機上設置靜態IP地址。我的環境只有Windows 2008 R2服務器,所以我只需要一個腳本來設置IP地址。我沒有問題寫一個腳本來更改IP地址,但我不知道如何運行它。 – Rossini
你使用哪種編程語言?我已經在C#中實現了這個功能,但是這段代碼太長了,不足以在這裏引用它。如果您使用腳本語言看到這些文章http://www.virtuallyghetto.com/2011/07/automating-new-integrated-vixguest.html,http://www.vmware.com/files/pdf/techpaper/vsphere_powercli51r1_technote .PDF。這些文章描述瞭如何使用PowerCLI調用客戶機操作系統上的腳本,並在Perl中包含一個實現與客戶機操作系統進行操作的示例。 –
我使用C#,你可以私信給我一個例子嗎? – Rossini