2010-04-27

ESXi Deployment Solution - Part 3

Today we will deal with the client and server scripts, and before we start I would like to get the definitions straight.

Client Script: The script that is executed on the ESXi machine (Python)

Server Script:The script running on a Windows host that will configure the ESXi machine after deployment (Powershell)

So let's start. Here is the client script - it was adapted from here

# TCP client example
import socket
s = socket.socket()
s.connect(("192.168.113.1",3333))
s.send("myuniquestring")
s.close()


Simple isn't it? - Well for me to understand this took a while so I will try and explain in as much detail as possible.

Line 2. Import the socket module - which will allow us to to create the communication socket

Line 3. Create the variable s as a socket

Line 4. Connect to an "IP","port" - in my case 192.168.113.1 and port 3333 - You can define this to the IP and port of your choice

Line 5. Send a string of text. This again can be anything you would like - but I would define it as something unique so not to have any false positives.

Line 6. Close the connection - and close the script.

And in plain text - the machine will open a communication socket on port 3333 to 192.168.113.1, send myuniquestring and exit.

Now on to the Server script - adapted from here

###############################################################################################
##	TCP port Listener + Connect to ESXi
## 	Author: 	Maish Saidel-Keesing                        
##  	       	http://technodrone.blogspot.com				
##	Date:		April 15, 2010								
##	Version: 	1.0
##	Synopsis:	This script will configure a TCP listener that will recive a string
##		It will then connect to a ESXi machine ready to configure the instance
###############################################################################################

#Define parameters with setting default port
function Trace-Port {
	param ([int]$port=3333, [string]$IPAdress="192.168.113.1", [switch]$Echo=$false)
	
		#create a new .net listener object
		$listener = new-object System.Net.Sockets.TcpListener([System.Net.IPAddress]::Parse($IPAdress), $port)
		$listener.start()
		[byte[]]$bytes = 0..255|%{0}
		write-debug "Waiting for a connection on port $port..."
		$client = $listener.AcceptTcpClient()
		$script:remoteIP = $client.Client.RemoteEndPoint
		$stream = $client.GetStream()
		while(($i = $stream.Read($bytes, 0, $bytes.Length)) -ne 0)
		{
			$bytes[0..($i-1)]|%{$_}
			if ($Echo){$stream.Write($bytes,0,$i)}
		}
		$client.Close()
		$listener.Stop()
		write-debug "Connection closed."
} #end Trace-Port Function

####entry point to script

#set Parameters
$result = $null
$script:string = "myuniquestring"

trace-port | foreach {
	$script:output = ([char]$_)
	$script:result += $output
}

if ($result -eq $string) {
	Connect-VIServer -Server $remoteIP.Address.IPAddressToString -User root -Password ""
}

 

Line 12. Two Parameters are passed by default to the script, IP and Port

Line 15. Create the listener object using the two parameters above

Line 16. Start the listener

Line 19. Open the TCP Client connection

Line 20. Assign the incoming IP to the remoteIP variable. The variable is in the Script Scope - so that I can use it again outside the function.

Line 22-26. In essence the input is translated into characters until the connection is closed

Line 27-28. Close the connection and then close the listener.

Line 35-36. Clear the results variable and define my unique string variable

Line 38-40. Get each character that is sent to the listener, and put it in the result string

Line 43.44. If the string that is sent matches the string I defined - then connect to the ESXi server with the remoteIP variable. The username is always root and password is always empty. This is the default of an ESXi installation.

And in Plain text - wait for a connection on port 3333. Once received  - check the string that is sent through this connection matches the string that is have predefined. If so that means it is a connection from an ESXi machine and that a connection should be opened to the ESXi server.

Now of course this is just a proof of concept for the script - but you should understand that once you can connect to the ESXi machine with root privileges you can configure what ever you would like.

Now of course to run the Server script, all you need is Powershell and the PowerCLI Cmdlets installed,

Next up is how to get the script into the ESXi installation process.