forked from vencax/py-dhcpd-manipulation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dhcpdmanip_cli.py
executable file
·64 lines (54 loc) · 1.86 KB
/
dhcpdmanip_cli.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
58
59
60
61
62
63
64
#!/usr/bin/env python
# coding=utf-8
'''
Created on Jun 2, 2014
CLI for dhcpdtools
'''
import sys
import logging
import json
from optparse import OptionParser
import dhcpdmanip
import os
usage = '''\
python %prog [action] [options]
action: add, remove, getleases, getreserved
'''
if __name__ == '__main__':
parser = OptionParser()
parser.add_option('-m', '--mac', action='store', dest='mac',
help='mac address (12 hexadigits)',)
parser.add_option('-i', '--ip', action='store', dest='ip',
help='IP address (x.x.x.x)',)
parser.add_option('-H', '--host', action='store', dest='name',
help='host name',)
parser.add_option('-d', '--desc', action='store', dest='desc',
help='host description',)
parser.set_usage(usage + parser.format_option_help())
(options, args) = parser.parse_args()
if len(args) != 1:
parser.error('wrong argument count. Action required.')
logging.basicConfig(level=logging.INFO)
action = args[0]
manipulator = dhcpdmanip.Manipulator()
def _render():
backfile = manipulator._dhcpd_conf_file + '.manip.back'
os.rename(manipulator._dhcpd_conf_file, backfile)
manipulator.render()
try:
if action == 'add':
desc = getattr(options, 'desc', None)
manipulator.add(options.name, options.mac, options.ip, desc)
_render()
elif action == 'remove':
manipulator.remove(options.mac)
_render()
elif action == 'getleases':
json.dump(manipulator.get_leases(), sys.stdout, indent=2)
elif action == 'getreserved':
json.dump(manipulator.get_reserved(), sys.stdout, indent=2)
else:
parser.error('Bad action')
except ValueError, e:
sys.stderr.write(e.message)
sys.exit(-1)