Skip to content

Instantly share code, notes, and snippets.

@ganto
Last active July 30, 2016 20:57
Show Gist options
  • Save ganto/ccd08a5935ab2aa16c5ec09b8f0a74ce to your computer and use it in GitHub Desktop.
Save ganto/ccd08a5935ab2aa16c5ec09b8f0a74ce to your computer and use it in GitHub Desktop.
sshd.fact
#!/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