Skip to content

Instantly share code, notes, and snippets.

@timedcy
Created December 4, 2019 08:53
Show Gist options
  • Save timedcy/a9da42ec66710e9a28535f769dcee694 to your computer and use it in GitHub Desktop.
Save timedcy/a9da42ec66710e9a28535f769dcee694 to your computer and use it in GitHub Desktop.
convert to and import putty config from xshell's
#!/bin/bash
# convert to and import putty config from xshell's
# author: timedcy
# date: 2019-12-04
# usage:
# In Windows 10 cmd, execute commands
# wsl bash xshell2putty_config.sh
# reg import tmp_xshell2putty_config.reg
# set vars bellow first
PATH_XSH="/mnt/d/conf/XShellSessions/Links"
PublicKeyFile="D:\\conf\\UserKeys\\keydcy_4096.ppk"
fn_tmp_reg=./tmp_xshell2putty_config.reg
proc_list() {
echo "Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\SimonTatham]
[HKEY_CURRENT_USER\Software\SimonTatham\PuTTY]
[HKEY_CURRENT_USER\Software\SimonTatham\PuTTY\Sessions]
" >${fn_tmp_reg}
for xsh in $(ls "${PATH_XSH}"/*xsh); do
proc_xsh $xsh
done
}
extr() { iconv -f utf-16 -t utf-8 $filepath | sed -nr "s#(^${1}=)([^\r]+)#\2#gp" | tr -d '\r'; }
proc_xsh() {
filename="${1##*/}"
name=${filename%.*}
filepath="${PATH_XSH}/$filename"
HostName=$(extr Host)
PortNumber=$(extr Port)
UserName=$(extr UserName)
PortNumber="dword:$(printf '%08x\n' ${PortNumber})"
WinTitle="${UserName}@: ${name}~"
FwdReqCount=$(extr FwdReqCount)
PortForwardings=""
if [[ "$FwdReqCount" -gt 0 ]]; then
for ((x = 0; x < $FwdReqCount; x++)); do
Port=$(extr "FwdReq_${x}_Port")
Host=$(extr "FwdReq_${x}_Host")
HostPort=$(extr "FwdReq_${x}_HostPort")
PortForwardings+="L${Port}=${Host}:${HostPort},"
done
PortForwardings="${PortForwardings%,*}"
fi
echo
echo filepath: $filepath
echo HostName: $HostName
echo PortNumber: $PortNumber
echo UserName: $UserName
echo ${PortForwardings}
echo WinTitle: $WinTitle
echo PublicKeyFile: $PublicKeyFile
echo fn_tmp_reg: $fn_tmp_reg
echo "
[HKEY_CURRENT_USER\Software\SimonTatham\PuTTY\Sessions\\${name}]
\"HostName\"=\"${HostName}\"
\"PortNumber\"="${PortNumber}"
\"UserName\"=\"${UserName}\"
\"PublicKeyFile\"=\"${PublicKeyFile}\"
\"PortForwardings\"=\"${PortForwardings}\"
\"WinTitle\"=\"${WinTitle}\"
\"AgentFwd\"=dword:00000001
\"Protocol\"=\"ssh\"
\"FullScreenOnAltEnter\"=dword:00000001
\"LineCodePage\"=\"UTF-8\"
\"TCPNoDelay\"=dword:00000001
\"TCPKeepalives\"=dword:00000001
\"Font\"=\"DejaVu Sans Mono\"
\"FontIsBold\"=dword:00000000
\"FontCharSet\"=dword:00000000
\"FontHeight\"=dword:00000012
\"FontVTMode\"=dword:00000004
" >>${fn_tmp_reg}
}
proc_list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment