Last active
July 30, 2016 20:57
-
-
Save ganto/ccd08a5935ab2aa16c5ec09b8f0a74ce to your computer and use it in GitHub Desktop.
sshd.fact
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from re import match | |
sshd_facts = [ | |
{ 'variable': 'AllowUsers', | |
'fact': 'allow_users', | |
'default': [] }, | |
{ 'variable': 'AllowGroups', | |
'fact': 'allow_groups', | |
'default': [] }, | |
{ 'variable': 'Port', | |
'fact': 'port', | |
'default': 22 }, | |
{ 'variable': 'TCPKeepAlive', | |
'fact': 'tcp_keep_alive', | |
'default': '' } | |
] | |
output = {'configured': 'true'} | |
for item in sshd_facts: | |
output.update({item['fact']: item['default']}) | |
with open("/etc/ssh/sshd_config") as fd: | |
for line in fd.readlines(): | |
for item in sshd_facts: | |
match_fact = match(r"^" + item['variable'] + " ([ \w]+)", line) | |
if match_fact: | |
if isinstance(item['default'], list): | |
output[item['fact']] = match_fact.groups()[0].split() | |
elif isinstance(item['default'], str): | |
output[item['fact']] = match_fact.groups()[0] | |
elif isinstance(item['default'], int): | |
output[item['fact']] = int(match_fact.groups()[0]) | |
print("{") | |
for key, value in sorted(output.items()): | |
if isinstance(value, str): | |
print(' "%s": "%s"' % (key, value)) | |
else: | |
print(' "%s": %s' % (key, value)) | |
print("}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment