Last active
May 17, 2017 05:50
-
-
Save anishsane/f9c61b7e1fe3c59554f5e60e11ba4243 to your computer and use it in GitHub Desktop.
Parse putty options in a bash script and launch ssh command
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Sample command arguments supported: | |
# "/path/to/putty.exe" -ssh -load "anishsane-dev" -l asane anishsane-dev | |
# "/path/to/putty.exe" -load "anishsane-dev" | |
# "/path/to/putty.exe" -local <~~~ This is not a standard putty option. | |
# But it is allowed to pass arbitrary options | |
# to putty binary from mtputty. | |
local_session=false | |
for ((i=1;i<=$#;i++)); do | |
if [ "${!i}" = "-local" ]; then | |
local_session=true | |
fi | |
if [ "${!i}" = "-l" ]; then | |
t=$((i+1)) | |
user=${!t} | |
fi | |
if [ "${!i}" = "-load" ]; then | |
t=$((i+1)) | |
putty_session=${!t} | |
fi | |
done | |
if $local_session; then | |
cd | |
bash | |
else | |
((i--)) | |
user_host=${!i} | |
RemoteCommand=$(reg query 'HKEY_CURRENT_USER\Software\SimonTatham\PuTTY\Sessions\'"$putty_session" /v RemoteCommand | awk '/RemoteCommand/{$1=$2=""; print substr($0,3);}') | |
HostName=$(reg query 'HKEY_CURRENT_USER\Software\SimonTatham\PuTTY\Sessions\'"$putty_session" /v HostName | awk '/HostName/{$1=$2=""; print substr($0,3);}') | |
UserName=$(reg query 'HKEY_CURRENT_USER\Software\SimonTatham\PuTTY\Sessions\'"$putty_session" /v UserName | awk '/UserName/{$1=$2=""; print substr($0,3);}') | |
if [ -z "$user_host" ]; then user_host=$HostName; fi | |
if [ -z "$user" ]; then user=$UserName; fi | |
if [ "${user_host#*@}" = "$user_host" ]; then | |
user_host="$user@$user_host" | |
fi | |
ssh -X -t "$user_host" $RemoteCommand | |
fi | |
echo | |
echo | |
read -s -n1 -p "Press any key to exit this window..." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment