Thursday, October 14, 2010

WAKE-ON-VNC

This is a small script I made to send a WAKE ON LAN message to a computer, then VNC into it.  It could be easily modified to use RDP, or other remote access/remote desktop applications.  I use it for a PC connected to a projector that is out-of-reach.

For this script, I used mc-wol.exe, and UltraVNC Viewer/UltraVNC Server.  For installing and configuring VNC, please use UltraVNC's site, or Google it. : )

To open CMD and run "cscript.exe wake-on-lan.vbs"

wake-on-lan.vbs

Option Explicit


'==============================================================================
'                                WAKE-ON-VNC
'==============================================================================
'Sends WOL message to specified MAC, then launches VNC
'Created:  2010-10-13, A. Tres Finocchiaro
'License:  GPL 3.0+


Dim timeout, mac, host, shell, vnc, pwd, hst, vncwait
Set shell = WScript.CreateObject("WScript.Shell")


'==============================================================================
'                     SAFE TO CHANGE BELOW THIS LINE
'==============================================================================
timeout = 180 'IN SECONDS
vncwait = 10 'IN SECONDS
mac = "00:88:77:44:33:77" 'MAC ADDRESS
host = "remote_pc" 'IP ADDRESS/HOSTNAME
vnc = "%PROGRAMFILES%\UltraVNC\vncviewer.exe" 'PATH TO VNCVIEWER
pwd = "password1" 'VNC PASSWORD
'==============================================================================
'                     DO NOT CHANGE BELOW THIS LINE
'==============================================================================


WScript.Timeout = timeout
hst = CHR(34) & host & CHR(34)
WScript.Echo "======================================================================="
WScript.Echo "                            WAKE-ON-VNC"
WScript.Echo "======================================================================="
WScript.Echo vbCrLf & "Starting script for host " & hst & " (" & mac & ") ..."
WScript.Echo "    Note:  This script will timeout in " & timeout & " seconds"


If (doPing()) Then
WScript.Echo vbCrLf & "Host " & hst & " is already pingable.  Trying VNC..."
doVnc()
WScript.Quit(1)
End If


shell.run "mc-wol.exe " & mac
If (Not doPing()) Then
WScript.Echo vbCrLf & "Will attempt to ping host until it is reached..."
While (Not doPing())
WScript.Echo "    Trying to ping " & hst & "..."
WScript.Sleep 1000 'Wait 1 second before trying again
Wend
End If

WScript.Echo vbCrLF & "Ping succeeded for host " & hst & "!"
WScript.Echo "    Waiting " & vncwait & " seconds for vncserver to start..."
WScript.Sleep vncwait * 1000


doVnc()


Function doVnc()
shell.run CHR(34) + vnc + CHR(34) & " " & host & " /password " & pwd & " /fullscreen /notoolbar"
End Function


Function doPing()
    Dim ping, status
    Set ping = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery _
     ("select * from Win32_PingStatus where address = '" & host & "'")
    
    For Each status In ping
        If IsNull(status.StatusCode) or status.StatusCode<>0 Then
         doPing = False
        Else
doPing = True
        End If
    Next
End Function


WScript.Quit(0)


-Tres

No comments: