Last active
October 15, 2019 13:51
-
-
Save kkappel/b32c18a972fea10512d2a36d78142565 to your computer and use it in GitHub Desktop.
This file contains hidden or 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/python3 | |
| # Read Clients from cients.conf | |
| # Requires: ipaddress, freeradius | |
| # | |
| # | |
| # (c) 2019 by Klaus Kappel <kkappel@jugendhilfe-wuemmetal.de> | |
| import ipaddress | |
| FN = '/etc/freeradius/3.0/clients.conf' | |
| class Client: | |
| def __init__(self, Name, IP): | |
| self.Name = Name | |
| self.IP = ipaddress.ip_address(IP) | |
| def display(self): | |
| ''' | |
| ''' | |
| print('%32s: %s' % (self.Name, self.IP)) | |
| class Clients: | |
| def __init__(self, FN): | |
| ''' | |
| Read Clients from FileName | |
| and list them in Object. | |
| ''' | |
| self.FN = FN | |
| self.clients = [] | |
| w = {} | |
| fh = open(FN, 'r') | |
| for line in fh: | |
| if line[0:6] == 'client': | |
| sect = 1 | |
| elif line.find('}') < 0: | |
| line = line.strip() | |
| if line.find('=') > -1: | |
| z = line.split('=') | |
| z[0] = z[0].strip() | |
| z[1] = z[1].strip() | |
| if z[0] == 'shortname': | |
| w['name'] = z[1] | |
| c = Client(w['name'], w['ip']) | |
| self.clients.append(c) | |
| elif z[0] == 'ipaddr': | |
| w['ip'] = z[1] | |
| else: | |
| sect = 0 | |
| w = {} | |
| fh.close() | |
| return | |
| def list(self): | |
| ''' | |
| list sorted by Name | |
| ''' | |
| self.clients.sort(key=lambda x: x.Name, reverse=False) | |
| for l in self.clients: | |
| l.display() | |
| def Ips(self): | |
| ''' | |
| sort by IP-Address | |
| ''' | |
| self.clients.sort(key=lambda x: x.IP, reverse=False) | |
| for l in self.clients: | |
| l.display() | |
| if __name__ = '__main__': | |
| radius = Clients(FN) | |
| # radius.list() | |
| # or |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Even better for testing.