Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
The aim of this vbs script is to launch putty using an URL like ssh://login@host directly from a browser. The .bat is almost the same than .vbs but less robust and just their for older systems.
:: This version is not as powerfull as vbs version, please prefere the other one.
:: It's just there if you need a non-visual basic way to perform this.
:: Restrictions:
:: - Password cannot contain a % or finish with @
:: - Chrome adds a / at the end of the URL, not yet managed
:: The aim of this script is to execute putty with parameters like ssh://login@host
:: Installation:
:: - Launch putty.reg to associate ssh:// and telnet:// to this script
:: - Edit the putty path in parameter below like puttyPath="C:\Program Files (x86)\putty.exe"
:: - Save this .bat on your PC like c:\putty.bat (or change the path in putty.reg)
:: Uninstallation:
:: - Run unputty.reg
:: Script created by Sebastien Biffi
:: Version 2014-04-17
@echo off
setlocal enableDelayedExpansion
set puttyPath="C:\Program Files (x86)\putty.exe"
set params=%1
rem set params=ssh://root:pwd@172.25.122.39:22
rem set params=telnet://172.25.41.2
:: checks the parameters and exists if nok
if "%params%"=="" (
echo [Warning] Empty parameter
goto usage
)
echo URL: %params%
:: finds protocol in URL and removes it form the rest
set URL=%params%
for /f "tokens=1* delims=/" %%a in ("%URL%") do (
set uProtocol=%%a
set uParams=%%b
)
:: remove ending : from protocol: expression
for /f "delims=:" %%a in ("%uProtocol%") do (
set uProtocol=%%a
)
if "%uParams%"=="" (
echo [Warning] Protocol or host has not been specified and are not optional
goto usage
)
:: split URL (except protocol) in authentication and host:port blocks
set uAuthent=
set uParamsRest=%uParams%
:loopParamsArobas
for /f "tokens=1* delims=@" %%a in ("%uParamsRest%") do (
if "%uAuthent%"=="" (
set uAuthent=%%a
) else (
set uAuthent=%uAuthent%@%%a
)
Echo %%b | findstr /C:"@">nul && (
set uParamsRest=%%b
goto loopParamsArobas
) || (
set uHostPort=%%b
)
)
if "%uHostPort%"=="" (
set uHostPort=%uAuthent%
set uAuthent=
)
:: remove possible / from protocol: expression
for /f "delims=/" %%a in ("%uHostPort%") do (
set uHostPort=%%a
)
:: split login:password in login & password
for /f "tokens=1* delims=:" %%a in ("%uAuthent%") do (
set uLogin=%%a
set uPwd=%%b
)
:: split host:port in host & port
for /f "tokens=1,2 delims=:" %%a in ("%uHostPort%") do (
set uHost=%%a
set uPort=%%b
)
echo protocol: %uProtocol%
:: echo params: %uParams%
:: echo authent: %uAuthent%
echo login: %uLogin%
echo password: %uPwd%
:: echo host:prot: %uHostPort%
echo host: %uHost%
echo port: %uPort%
:: check parameters and create putty execution
if "%uProtocol%"=="telnet" set pProtocol= -telnet
if "%uProtocol%"=="ssh" set pProtocol= -ssh
if "%pProtocol%"=="" (
echo [Warning] protocol not reckognized:%uProtocol%
goto usage
)
if "%uHost%"=="" (
echo [Warning] host cannot be null
goto usage
)
set pHost= %uHost%
if not "%uLogin%"=="" set pLogin= -l %uLogin%
if not "%uPwd%"=="" set pPwd= -pw %uPwd%
if not "%uPort%"=="" set pPort= -P %uPort%
set puttyCommand=%puttyPath%%pProtocol%%pHost%%pLogin%%pPwd%%pPort%
echo puttyCommand: %puttyCommand%
start "" %puttyCommand%
echo done
rem pause
exit 0
rem goto :EOF
:: display the help of this batch script
:usage
echo Usage: %~nx0 URL
echo for example: %~nx0 ssh://login:pwd@host:port
echo %~nx0 telnet://login@host
echo %~nx0 telnet://host
echo;
echo The URL must be fully qualified, including the ssh:// or telnet://
pause
goto :EOF
Windows Registry Editor Version 5.00
[-HKEY_CLASSES_ROOT\ssh]
[-HKEY_CLASSES_ROOT\telnet]
[HKEY_CLASSES_ROOT\ssh]
@="URL:PuTTY SSH Protocol"
"EditFlags"=dword:00000002
"FriendlyTypeName"="@ieframe.dll,-907"
"URL Protocol"=""
"BrowserFlags"=dword:00000008
[HKEY_CLASSES_ROOT\ssh\DefaultIcon]
@="C:\\Program Files (x86)\\putty.exe,0"
[HKEY_CLASSES_ROOT\ssh\shell]
@=""
[HKEY_CLASSES_ROOT\ssh\shell\open]
[HKEY_CLASSES_ROOT\ssh\shell\open\command]
@="wscript.exe C:\\putty.vbs %1"
[HKEY_CLASSES_ROOT\telnet]
@="URL:PuTTY telnet Protocol"
"EditFlags"=dword:00000002
"FriendlyTypeName"="@ieframe.dll,-907"
"URL Protocol"=""
"BrowserFlags"=dword:00000008
[HKEY_CLASSES_ROOT\telnet\DefaultIcon]
@="C:\\Program Files (x86)\\putty.exe,0"
[HKEY_CLASSES_ROOT\telnet\shell]
@=""
[HKEY_CLASSES_ROOT\telnet\shell\open]
[HKEY_CLASSES_ROOT\telnet\shell\open\command]
@="wscript.exe C:\\putty.vbs %1"
' The aim of this script is to execute putty with parameters like ssh://login@host directly using an URL in a browser
' Installation:
' - Launch putty.reg to associate ssh:// and telnet:// to this script
' - Edit the putty path in parameter below like puttyPath = "C:\Program Files (x86)\putty.exe"
' - Save this .vbs on your PC like c:\putty.vbs (or change the path in putty.reg)
' Uninstallation:
' - Run unputty.reg
' Version 2014-04-23'
' Script Created by Sebastien Biffi
puttyPath = "C:\Program Files (x86)\putty.exe"
On Error Resume Next
' initialisation of variables
logResult = "" ' Used for logging purpose during debug or in case of error. Log is a msgBox
logIcon = 0 ' Icon of the MsgBox. By default: none
logTitle = "" ' Title of the MsgBox. By default: none
Set re = New RegExp
' The pattern that matches the URL. Used to check the URL format provided and find fields in it.
re.Pattern = "^([^:]+)://(([^:]+)(:(.*))?@)?(([^/@:]+)(:([0-9]+))?)/?$"
' Check if only 1 parameter is passed to the script, else throw an error
if Wscript.Arguments.Count = 1 then
url = Wscript.Arguments(0)
else
exitWithError("Please pass only 1 parameter to this script like:" & vbCrLf & " - ssh://login:password@host:port " & vbCrLf & " - telnet://login@host:port" & vbCrLf & "login, password & port are optional")
end if
' Check if the URL is valid, else throw an error
If re.Test(url) Then
log("URL is valid: " & url)
Else
exitWithError(url & " is NOT a valid URL" & vbCrLf & "Please pass only 1 parameter to this script like:" & vbCrLf & " - ssh://login:password@host:port " & vbCrLf & " - telnet://login@host:port" & vbCrLf & "login, password & port are optional")
End If
' Find the putty parameters in the URL
Set Matches = re.Execute(url)
protocol = Matches.Item(0).Submatches(0)
login = Matches.Item(0).Submatches(2)
pwd = Matches.Item(0).Submatches(4)
host = Matches.Item(0).Submatches(6)
port = Matches.Item(0).Submatches(8)
log(" host: " & host)
log(" protocol: " & protocol)
log(" port: " & port)
log(" login: " & login)
log(" pwd: " & pwd)
if protocol = "ssh" then
paramProtocol = " -ssh"
elseif protocol = "telnet" then
paramProtocol = " -telnet"
else
exitWithError("Protocol: " & protocol & " is not a valid protocol (ssh or telnet).")
end if
if host <> "" then
paramHost = " " & host
else
exitWithError("Host cannot bu null.")
end if
if login <> "" then
paramLogin = " -l " & login
end if
if (pwd <> "") and (protocol <> "telnet") then
paramPwd = " -pw " & pwd
end if
if port <> "" then
paramPort = " -P " & port
end if
' build the putty command like:
' putty.exe -ssh host -l login -pw password -P port
' putty.exe -telnet host -l login -P port
puttyCommand = chr(34) & puttyPath & chr(34) & paramProtocol & paramHost & paramLogin & paramPwd & paramPort
log (puttyCommand)
' For debugging purpose, uncomment following line:
'outputLog()
' Execute putty
Set objShell = CreateObject("WScript.Shell")
objShell.Run puttyCommand,1,False
' ****************
' Error Output
' ****************
' Outputs a msgBox including the log and the error message
function exitWithError(str)
log(str)
logIcon = vbCritical
logTitle = "Error"
outputLog()
wscript.Quit
end function
' ***********
' Logging
' ***********
' Adds a message to the log String
function log(str)
if logResult = "" then
logResult = str
else
logResult = logResult & vbCrLf & str
end if
end function
' Outputs a msgBox including the log String
function outputLog()
if logResult <> "" then
MsgBox logResult, logIcon, logTitle
end if
end function
@mgjuhler

This comment has been minimized.

Copy link

mgjuhler commented Mar 12, 2016

Fix to chrome / bug in the bat file, set params=!params:~0,-1!

@kmpoppe

This comment has been minimized.

Copy link

kmpoppe commented Feb 2, 2019

I improved on @sbiffi 's script with support for 1Password-Login-URLs:
https://gist.github.com/kmpoppe/52ff04186f52e20078f66da7e4d9ff6b

Hope this is of use to anyone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.