-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtinysshfp.py
executable file
·57 lines (48 loc) · 1.66 KB
/
tinysshfp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env python3
# tinysshfp - takes the output of "ssh-keygen -r" and converts into tinydns
#
# example: ssh-keygen -r example.com | ./tinysshfp.py
#
# https://www.rfc-editor.org/rfc/rfc4255
# 2017,2022 Lee Maguire
import getopt
import sys
import re
ttl = "86400"
def tinyBytes( bytearr, escape_all=False ):
## output printable ascii but not space, "/", ":", "\"
## all other characters output as octal \nnn codes
output = ""
for b in bytearr:
if not escape_all and int(b) > 32 and int(b) < 127 and int(b) not in [47,58,92]:
output += chr(b)
else:
output += "\\{0:03o}".format(b)
return( output )
def nboInt( length, number ):
## returns bytes representing an integer in network byte order (big endian)
## if input "2, 256", output should be equivalent to "\001\000"
intbytes = int(number).to_bytes(length, "big")
return( intbytes )
def tinySshfpRecord( hostname, algid, fptype, fp, ttl ):
output = ""
output += ":"
output += tinyBytes(bytes(hostname, "ascii"))
output += ":44:"
output += tinyBytes( nboInt(1,algid) )
output += tinyBytes( nboInt(1,fptype) )
output += tinyBytes( bytes.fromhex(fp), True )
output += ":" + ttl
return( output );
opts, args = getopt.getopt(sys.argv[1:],"ht:",["ttl="])
for opt, arg in opts:
if opt == '-h':
print('Usage: tinysshfp.py -t 60')
sys.exit()
elif opt in ("-t", "--ttl"):
ttl = arg
for input_text in sys.stdin:
hostname,xin,xsshfp,algid,fptype,fp = input_text.rstrip().split(" ")
line = tinySshfpRecord( hostname, algid, fptype, fp, ttl )
sys.stdout.write( line + "\n")
sys.exit(0)