Tuesday, November 24, 2009

Quick & Dirty startup script for VM + Internet + sendmail

Someone asked me the other day how to write a script for Windows to automatically start a VM, connect to the internet and send the IP Address out so that they can reconnect to the machine if there's a crash. Without using DynDNS us here in South Africa have dynamic IP Addresses so you cannot rely on it remaining the same.

You'll need to download sendmail for Windows for this.

@echo off
vmrun start c:\path\to\virtual\machine.vmx
rasdial connection_name username password

ECHO From: someone@example.com > %TEMP%\temp.mail
ECHO To: someone-else@example.com. >> %TEMP%\temp.mail
ECHO Subject: this is a test >> %TEMP%\temp.mail
ECHO.>> %TEMP%\temp.mail
ECHO testing. >> %TEMP%\temp.mail
ECHO blah blah.. >> %TEMP%\temp.mail
ECHO. >> %TEMP%\temp.mail
ECHO blah. >> %TEMP%\temp.mail
ipconfig/all >> %TEMP%\temp.mail

sendmail -t < %TEMP%\temp.mail
DEL %TEMP%\temp.mail

And that's it short and sweet. If your VM requires you to press a key to start such as Y you can pipe that in when you start it with
echo Y| vmrun start c:\path\to\virtual\machine.vmx