I have a custom URL handler that calls a .bat file which then launches PuTTY. When the .bat file executes, a cmd.exe window appears which then launches PuTTY. I know I can pass /c to cmd.exe to avoid the creation of the cmd.exe window. Can I tell KeePass to invoke cmd.exe with /C?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
but the command window still appears followed by the Putty window. Is there a way to avoid the command window appearing? I presume cmd://cmd.exe has Keepass invoking cmd.exe with /C rather than the initial cmd.exe being passed /C to avoid the command window.
Last edit: Albert Chin-A-Young 2018-08-21
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Attached is the .bat script. It just launches putty with a temporary file that contains "root@<servername>". This temporary file is removed once putty terminates.</servername>
@ECHO off
SETLOCAL EnableExtensions EnableDelayedExpansion
SET pamSSHRelayServer=SERVERNAME
SET pamSSHRelayPort=PORT
IF "%~1" == "" ECHO No server name specified
IF "%~1" == "" EXIT /b
SET "usernameUpper=%USERNAME%"
CALL :toUpper usernameUpper
REM Get unique file name
:uniqLoop
SET "uniqueFileName=%tmp%\putty~%RANDOM%.tmp"
IF EXIST "%uniqueFileName%" GOTO :uniqLoop
ECHO root@%1 >%uniqueFileName%
putty -ssh -t -P %pamSSHRelayPort% %usernameUpper%@%pamSSHRelayServer%^
-m %uniqueFileName%
DEL %uniqueFileName%
EXIT /b
REM Functions
:toUpper str -- converts lowercase character to uppercase
:: -- str [in,out] - valref of string variable to be converted
:$created 20060101 :$changed 20080219 :$categories StringManipulation
:$source https://www.dostips.com
IF NOT DEFINED %~1 EXIT /b
FOR %%a IN ("a=A" "b=B" "c=C" "d=D" "e=E" "f=F" "g=G" "h=H" "i=I"
"j=J" "k=K" "l=L" "m=M" "n=N" "o=O" "p=P" "q=Q" "r=R"
"s=S" "t=T" "u=U" "v=V" "w=W" "x=X" "y=Y" "z=Z") do (
CALL SET %~1=%%%~1:%%~a%%
)
EXIT /b
Last edit: Albert Chin-A-Young 2018-08-21
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The /C command line option tells cmd.exe to execute the command that follows and terminate afterwards. It doesn't hide the cmd.exe window.
PowerShell scripts and commands can more easily be executed invisibly (by running PowerShell.exe with the -WindowStyle Hidden command line parameter); maybe you could convert your batch file to a PowerShell script or run your batch file using PowerShell.
Another solution would be to use the {CMD:...} placeholder (which can run applications with a hidden window, see [1] for details) together with running a GUI dummy executable (like consent.exe), e.g. cmd://consent.exe {CMD:!"H:\pamssh.bat" {BASE:RMVSCM}!W=0,WS=H!}
(that's a bit of a hack; the PowerShell way is cleaner).
A cmd window appears (to run powershell) but then disappears and then Putty is launched. Wish the cmd window never appeared but that's probably the best that can be achieved with trying to execute a .bat file. Might try to rewrite in .vbs to eliminate this window all-together. Thanks everyone for the help.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Here's a PowerShell version for you.
I've not tested the whole thing together but the parts are all OK. Comment the test command and un-comment the real command when you've tested it.
cheers, Paul
# PS variables are local by default# check command line argumentif(-Not($Args[0])){Break}# set required values$pamSSHRelayServer=SERVERNAME$pamSSHRelayPort=PORT$usernameUpper="$env:USERNAME".ToUpper()# create a random file name; Get-Random -Maximum 99999 -minimum 1000do{$uniqueFileName=$env:tmp+"\putty~"+(Get-Random-Maximum99999-minimum1000)+".tmp"}While(Test-Path"$uniqueFileName")# create command fileWrite-Output"root@"+$Args[0]|Add-Content$uniqueFileName# test command. comment out these 2 lines after testingWrite-Output"putty -ssh -t -P $pamSSHRelayPort $usernameUpper@$pamSSHRelayServer^ -m $uniqueFileName"pause# run puTTY##& putty -ssh -t -P $pamSSHRelayPort $usernameUpper@$pamSSHRelayServer^ -m $uniqueFileName#clean upDEL$uniqueFileName
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I have a custom URL handler that calls a .bat file which then launches PuTTY. When the .bat file executes, a cmd.exe window appears which then launches PuTTY. I know I can pass /c to cmd.exe to avoid the creation of the cmd.exe window. Can I tell KeePass to invoke cmd.exe with /C?
Use
cmd://cmd.exe /C
For more information see Executing command lines in the KeePass URL Field Capabilities documentation.
Put
exit
at the end of your batch file.cheers, Paul
Thanks. I modified my URL-handler to:
but the command window still appears followed by the Putty window. Is there a way to avoid the command window appearing? I presume
cmd://cmd.exe
has Keepass invokingcmd.exe
with/C
rather than the initialcmd.exe
being passed/C
to avoid the command window.Last edit: Albert Chin-A-Young 2018-08-21
Did you add the
exit
to the end of your batch file?I have
EXIT /b
at the end of the file.When you run your batch file outside of KeePass, does the console window stay open until you exit PuTTY?
If I execute my script from the cmd promp as above, only the Putty window appears.
Attached is the .bat script. It just launches putty with a temporary file that contains "root@<servername>". This temporary file is removed once putty terminates.</servername>
Last edit: Albert Chin-A-Young 2018-08-21
Have you tried just:
cmd://H:\pamssh.bat {BASE:RMVSCM}
cheers, Paul
Yes. I did that initially. I get the cmd window and then the Putty window. Both windows remain open until Putty terminates.
When you run a batch file, the console window is going to stay open until the job is finished. There is nothing KeePass can do about that.
The
/C
command line option tellscmd.exe
to execute the command that follows and terminate afterwards. It doesn't hide thecmd.exe
window.PowerShell scripts and commands can more easily be executed invisibly (by running
PowerShell.exe
with the-WindowStyle Hidden
command line parameter); maybe you could convert your batch file to a PowerShell script or run your batch file using PowerShell.Another solution would be to use the
{CMD:...}
placeholder (which can run applications with a hidden window, see [1] for details) together with running a GUI dummy executable (likeconsent.exe
), e.g.cmd://consent.exe {CMD:!"H:\pamssh.bat" {BASE:RMVSCM}!W=0,WS=H!}
(that's a bit of a hack; the PowerShell way is cleaner).
Best regards,
Dominik
[1] https://keepass.info/help/base/placeholders.html#cmd
Last edit: Dominik Reichl 2018-08-21
Ok, thanks.
I modified my URL handler to:
A cmd window appears (to run powershell) but then disappears and then Putty is launched. Wish the cmd window never appeared but that's probably the best that can be achieved with trying to execute a
.bat
file. Might try to rewrite in.vbs
to eliminate this window all-together. Thanks everyone for the help.Here's a PowerShell version for you.
I've not tested the whole thing together but the parts are all OK. Comment the test command and un-comment the real command when you've tested it.
cheers, Paul
Wow! Thanks. Seems simpler than the vbscript version I wrote yesterday::