Plink的解决方案
将此Python脚本保存为known_hosts.py
:
import fileinput
import base64
import struct
import string
import re
import sys
import getopt
def winmungestr(s):
"Duplicate of PuTTY's mungestr() in winstore.c:1.10 for Registry keys"
candot = 0
r = ""
for c in s:
if c in ' \*?%~' or ord(c)<ord(' ') or (c == '.' and not candot):
r = r + ("%%%02X" % ord(c))
else:
r = r + c
candot = 1
return r
def strtolong(s):
"Convert arbitrary-length big-endian binary data to a Python long"
bytes = struct.unpack(">%luB" % len(s), s)
return reduce ((lambda a, b: (long(a) << 8) + long(b)), bytes)
def longtohex(n):
"""Convert long int to lower-case hex.
Ick, Python (at least in 1.5.2) doesn't appear to have a way to
turn a long int into an unadorned hex string -- % gets upset if the
number is too big, and raw hex() uses uppercase (sometimes), and
adds unwanted "0x...L" around it."""
plain=string.lower(re.match(r"0x([0-9A-Fa-f]*)l?$", hex(n), re.I).group(1))
return "0x" + plain
output_type = 'windows'
try:
optlist, args = getopt.getopt(sys.argv[1:], '', [ 'win', 'unix' ])
if filter(lambda x: x[0] == '--unix', optlist):
output_type = 'unix'
except getopt.error, e:
sys.stderr.write(str(e) + "\n")
sys.exit(1)
if output_type == 'windows':
sys.stdout.write("""REGEDIT4
[HKEY_CURRENT_USER\Software\SimonTatham\PuTTY\SshHostKeys]
""")
for line in fileinput.input(args):
try:
line = string.strip (line)
if line == '' or line[0] == '#':
raise "Skipping input line"
fields = string.split (line, ' ')
hostpat = fields[0]
magicnumbers = []
keytype = ""
if re.match (r"\d*$", fields[1]):
magicnumbers = map (long, fields[2:4])
keytype = "rsa"
else:
sshkeytype, blob = fields[1], base64.decodestring (fields[2])
subfields = []
while blob:
sizefmt = ">L"
(size,) = struct.unpack (sizefmt, blob[0:4])
size = int(size)
(data,) = struct.unpack (">%lus" % size, blob[4:size+4])
subfields.append(data)
blob = blob [struct.calcsize(sizefmt) + size : ]
magicnumbers = map (strtolong, subfields[1:])
if sshkeytype == "ssh-rsa": keytype = "rsa2"
elif sshkeytype == "ssh-dss": keytype = "dss"
else:
raise "Unknown SSH key type", sshkeytype
for host in string.split (hostpat, ','):
if re.search (r"[*?!]", host):
sys.stderr.write("Skipping wildcard host pattern '%s'\n"
% host)
continue
elif re.match (r"\|", host):
sys.stderr.write("Skipping hashed hostname '%s'\n" % host)
continue
else:
m = re.match (r"\[([^]]*)\]:(\d*)$", host)
if m:
(host, port) = m.group(1,2)
port = int(port)
else:
port = 22
key = keytype + ("@%d:%s" % (port, host))
value = string.join (map (longtohex, magicnumbers), ',')
if output_type == 'unix':
sys.stdout.write('%s %s\n' % (key, value))
else:
sys.stdout.write("\"%s\"=\"%s\"\n"
% (winmungestr(key), value))
except "Unknown SSH key type", k:
sys.stderr.write("Unknown SSH key type '%s', skipping\n" % k)
except "Skipping input line":
pass
测试环境为Win7x64和Python 2.7。
然后运行以下命令:
ssh-keyscan -t rsa bitbucket.org >>~/.ssh/known_hosts
python --win known_hosts.py >known_hosts.reg
start known_hosts.reg
选择导入到注册表中。使用keyscan检索域的公钥(我在使用bitbucket时遇到了问题),然后Python脚本将其转换为Plink格式。
$HOME
没有正确设置。尝试在Windows上使用“我的电脑”->右键单击->“属性”->“高级”选项卡->“环境变量”按钮来设置HOME
环境变量。 - eckes/c//
(可能是驱动器号)之后仍然有%HOMEDRIVE%
确实让我感到奇怪...你可以通过调整值并回显来节省一些时间。 - CascabelHOMEDRIVE
和HOMEPATH
并将HOME
设置为结果值... - eckes