-
Notifications
You must be signed in to change notification settings - Fork 4
/
keyutils.py
executable file
·32 lines (25 loc) · 974 Bytes
/
keyutils.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
#!/usr/bin/env python
import dns.tsigkeyring
import re
import sys
def read_tsigkey(tsig_key_file, key_name):
"""Accept a TSIG keyfile and a key name to retrieve.
Return a keyring object with the key name and TSIG secret."""
try:
key_file = open(tsig_key_file)
key_struct = key_file.read()
key_file.close()
except IOError:
print "A problem was encountered opening your keyfile, %s." % tsig_key_file
raise
try:
key_data = re.search(r"key \"%s\" \{(.*?)\}\;" % key_name, key_struct, re.DOTALL).group(1)
algorithm = re.search(r"algorithm ([a-zA-Z0-9_-]+?)\;", key_data, re.DOTALL).group(1)
tsig_secret = re.search(r"secret \"(.*?)\"", key_data, re.DOTALL).group(1)
except AttributeError:
print "Unable to decipher the keyname and secret from your key file."
raise
keyring = dns.tsigkeyring.from_text({
key_name : tsig_secret
})
return keyring