Make kh2reg.py compatible with modern Python.

Bare string exceptions aren't supported any more.
Patch by Will Aoki, plus a backward compatibility tweak from Colin Watson.
Seen working with Python 2.4.3 and 2.7.6.
This commit is contained in:
Jacob Nevins 2015-02-24 10:30:18 +00:00
Родитель 087ca595f3
Коммит 56a42d09d4
1 изменённых файлов: 14 добавлений и 6 удалений

Просмотреть файл

@ -8,7 +8,8 @@
# kh2reg.py --unix known_hosts1 2 3 4 ... > sshhostkeys # kh2reg.py --unix known_hosts1 2 3 4 ... > sshhostkeys
# Creates data suitable for storing in ~/.putty/sshhostkeys (Unix). # Creates data suitable for storing in ~/.putty/sshhostkeys (Unix).
# Line endings are someone else's problem as is traditional. # Line endings are someone else's problem as is traditional.
# Developed for Python 1.5.2. # Originally developed for Python 1.5.2, but probably won't run on that
# any more.
import fileinput import fileinput
import base64 import base64
@ -63,6 +64,13 @@ if output_type == 'windows':
[HKEY_CURRENT_USER\Software\SimonTatham\PuTTY\SshHostKeys] [HKEY_CURRENT_USER\Software\SimonTatham\PuTTY\SshHostKeys]
""") """)
class BlankInputLine(Exception):
pass
class UnknownKeyType(Exception):
def __init__(self, keytype):
self.keytype = keytype
# Now process all known_hosts input. # Now process all known_hosts input.
for line in fileinput.input(args): for line in fileinput.input(args):
@ -72,7 +80,7 @@ for line in fileinput.input(args):
# Skip blanks and comments # Skip blanks and comments
if line == '' or line[0] == '#': if line == '' or line[0] == '#':
raise "Skipping input line" raise BlankInputLine
# Split line on spaces. # Split line on spaces.
fields = string.split (line, ' ') fields = string.split (line, ' ')
@ -119,7 +127,7 @@ for line in fileinput.input(args):
if sshkeytype == "ssh-rsa": keytype = "rsa2" if sshkeytype == "ssh-rsa": keytype = "rsa2"
elif sshkeytype == "ssh-dss": keytype = "dss" elif sshkeytype == "ssh-dss": keytype = "dss"
else: else:
raise "Unknown SSH key type", sshkeytype raise UnknownKeyType(sshkeytype)
# Now print out one line per host pattern, discarding wildcards. # Now print out one line per host pattern, discarding wildcards.
for host in string.split (hostpat, ','): for host in string.split (hostpat, ','):
@ -150,7 +158,7 @@ for line in fileinput.input(args):
sys.stdout.write("\"%s\"=\"%s\"\n" sys.stdout.write("\"%s\"=\"%s\"\n"
% (winmungestr(key), value)) % (winmungestr(key), value))
except "Unknown SSH key type", k: except UnknownKeyType, k:
sys.stderr.write("Unknown SSH key type '%s', skipping\n" % k) sys.stderr.write("Unknown SSH key type '%s', skipping\n" % k.keytype)
except "Skipping input line": except BlankInputLine:
pass pass