Monday, March 12, 2007

Run VNC and RDP from hyperlinks

It looks like Google has finally spidered my new blog so I hope to get some more visitors soon. ;-)

Anyways, just wanted to share a handy way to connect to RDP and VNC servers. If you, as I do, use tools like Nagios to monitor my servers, wouldn't it be easy if you could just click a link to start the VNC or RDP connection? Well, you can:

Save the following text as C:\Windows\RDP.js:
var destination=(WScript.Arguments(0))
var search='rdp://'
var rdpexe='C:\\WINDOWS\\system32\\mstsc.exe'
//WScript.Echo(destination)
destination=destination.replace(search, '')
destination=destination.replace('/', '')
var ws = new ActiveXObject("WScript.Shell")
//WScript.Echo(rdpexe + " /v:" + destination)
ws.Exec(rdpexe + " /v:" + destination)

Save the next piece as RDP.reg:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\rdp]
@="URL:Remote Desktop Connection"
"URL Protocol"=""
[HKEY_CLASSES_ROOT\rdp\DefaultIcon]
@="C:\\WINDOWS\\System32\\mstsc.exe"
[HKEY_CLASSES_ROOT\rdp\shell]
[HKEY_CLASSES_ROOT\rdp\shell\open]
[HKEY_CLASSES_ROOT\rdp\shell\open\command]
@="wscript.exe C:\\WINDOWS\\rdp.js %1"

Double click and presto! When you click something like rdp://192.168.0.1 you will be connected to that server by RDP.

VNC is not much more difficult, edit the following text to point to your VNC and save as C:\Windows\VNC.js:
var destination=(WScript.Arguments(0))
var search='vnc://'
//Modify the path to VNC Viewer!
var vncexe='D:\\Apps\\VNC\\vncviewer.exe'
//WScript.Echo(destination)
destination=destination.replace(search, '')
destination=destination.replace('/', '')
var ws = new ActiveXObject("WScript.Shell")
//WScript.Echo(vncexe + " " + destination)
ws.Exec(vncexe + " " + destination)

Save the next piece of text as VNC.reg and doubleclick:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\vnc]
@="URL:VNC Connection"
"URL Protocol"=""
[HKEY_CLASSES_ROOT\vnc\DefaultIcon]
@="C:\\WINDOWS\\System32\\mstsc.exe"
[HKEY_CLASSES_ROOT\vnc\shell]
[HKEY_CLASSES_ROOT\vnc\shell\open]
[HKEY_CLASSES_ROOT\vnc\shell\open\command]
@="wscript.exe C:\\WINDOWS\\vnc.js %1"

19 comments:

Anonymous said...

Thank you!

Anonymous said...

Thanks you very much this is very helpfull. How do you come up with this idea?

RogierG said...

@creatron: uhhh, brainfart?

Futasec-e said...

This rocks!

As we say this side of the world -Scraaaaaaabada to you! :)

Futasec-e said...

I just have the itch to get this also for ssh://

If you can do the update, I 'll crown you...

Anonymous said...

And I'm GLAD google spidered you too - thanks man! Mitch (from Canada)

Anonymous said...

nice job! it really helped me out and caused me to discover something.

RogierG said...

@anonymous: now I'm wondering what you discovered. ;-)

Unknown said...

Rogierg, great tip!! I've been searching everywhere for a way to add a link to my Nagios interface that would direct me to an HTTP, RDP, or VNC session. How do you add the links to Nagios itself?

Anonymous said...

Thanks, this has come in very handy. In the process of trying to use this in a cloud based virtualization product I needed to make it easy to "install". The complexity was that the registry file assumes the javascript file is in a certain location. Rather than build a full installer to do this (it seemed like overkill), I played around with doing the URL parsing as part of the shell/open/command entry. What I came up with does not require the separate file, so users can simply download the registry file and import it into their registry.


The key to this working is the /V:ON parameter that turns on delayed expansion. This allows you to use shell expansion, specifically the string replacement functionality of it, to modify the url into server:port.

Hope this helps others out there.

====begin registry file====
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\rdp]
@="URL:Remote Desktop Connection"
"URL Protocol"=""
[HKEY_CLASSES_ROOT\rdp\DefaultIcon]
@="C:\\WINDOWS\\System32\\mstsc.exe"
[HKEY_CLASSES_ROOT\rdp\shell]
[HKEY_CLASSES_ROOT\rdp\shell\open]
[HKEY_CLASSES_ROOT\rdp\shell\open\command]
@="cmd /V:ON /s /c set url=%1 && set url=!url:rdp:=! && set url=!url:/=! && start C:\\WINDOWS\\system32\\mstsc.exe /v:!url!"
====end registry file====

Anonymous said...

@ Lonnie Hutchinson...

Can you do the same this as this, but for VNC??

Anonymous said...

Hi thanks for this. How did you manage to get hyperlinks to run from nagios, i have spend ages trying to figure it out to no avail?

Anonymous said...

Thanks, looks create but i get an error:
c:windows\vnc.js
line:10
token:1
the system can't find the file
code:80070002
source:WshShell.Exec

can somebody please help??

RogierG said...

@Anonymous: I guess VNC is not in D:\Apps\VNC\vncviewer.exe. Either create this location or edit vnc.js so the location in line 4 points to vncviewer.exe on your computer.

Anonymous said...

@rogierG

the vncviewer is in 'C:\Program Files\RealVNC\VNC4\vncviewer.exe'

so i made
var vncexe=''C:\\Program Files\\RealVNC\\VNC4\\vncviewer.exe'

it is something in line 10

var destination=(WScript.Arguments(0))
var search='vnc://'
//Modify the path to VNC Viewer!
var vncexe='C:\\Program Files\\RealVNC\\VNC4\\vncviewer.exe'
//WScript.Echo(destination)
destination=destination.replace(search, '')
destination=destination.replace('/', '')
var ws = new ActiveXObject("WScript.Shell")
WScript.Echo(vncexe + " " + destination)
ws.Exec(vncexe + " " + destination)

can it be something about ws.Exec

RogierG said...

@Anonymous: strange, that should work. I have tested it with vncviewer.exe in the same location and that works. Have you tried copying vncviewer.exe to another location? Also some antivirus tools perceive vncviewer.exe as malware and might deny access.

Anonymous said...

works great! my mistake i forgot // to add to the url.

anybody figured out how to include password in url

Jeff Saunders said...

I love this, but I can't seem to get a URL like RDP://servername to actually show up as a link in IE, Firefox, or Office apps (Excel).

Am I missing something?

When I do start - run - RDP://servername, that does work.

RogierG said...

@Jeff: For Excel just insert a hyperlink and add rdp://whatever as the address.

For webpages I think you need to do something like: [a href="rdp://whatever"]rdp://whatever[/a]" (Replace [ with < and ] with >)