kea-shell 2.41 KB
Newer Older
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
#!/usr/bin/python

# First, let's import the right kea_connector.
# We have two versions: one for python 2.x and another for python 3.x.
# Sadly, there's no unified way to handle http connections. The recommended
# way is to use Requests (http://docs.python-requests.org/en/master/), but
# that's a stand alone package that requires separate installation. One of
# the design requirements was to not require any additional packages, so
# the code uses standard libraries available in python. Hence two versions.
import sys
if (sys.version_info[0] == 2):
    # This is Python 2.x
    import kea_connector2 as kea_connector
else:
    if (sys.version_info[0] == 3):
        # This is Python 3.x
        import kea_connector3 as kea_connector
    else:
        # This is... have no idea what it is.
        raise SystemExit("Unknown python version:" + str(sys.version_info[0]))

from kea_conn import CARequest, CAResponse

# Second step: Need to parse command line parameters. We will use argparse for
# that purpose. It does great job with having default values, taking care of
# the help and sanity checking input parameters.

import argparse
parser = argparse.ArgumentParser(description='Connects to Kea Control Agent.')
30
parser.add_argument('--host', type=str, nargs=1, default='127.0.0.1',
31
                    help='hostname of the CA to connect to')
32
parser.add_argument('--port', type=int, nargs=1, default=8000,
33
34
35
36
37
38
39
40
41
42
43
                    help='TCP port of the CA to connect to')
parser.add_argument('--timeout', type=int, nargs=1, default='10000',
                    help='Timeout (in milliseconds) when attempting to connect to CA')
parser.add_argument('command', type=str, nargs="?", default='list-commands',
                    help='command to be executed. If not specified, "list-commands" is used')
cmd_args = parser.parse_args()

# Ok, now time to put the parameters parsed into the structure to be used by the
# connection.
params = CARequest()
params.command = cmd_args.command
44
45
params.http_host = cmd_args.host
params.http_port = cmd_args.port
46
47
48
49
50
51
52
params.timeout = cmd_args.timeout

params.generateBody()
params.generateHeaders()

conn = kea_connector.KeaConnector()

53
54
55
56
57
58
59
# Load command processor
# @todo

# Read parameters from stdin (they're optional for some commands)
for line in sys.stdin:
    params.params += line

60
61
62
63
64
65
66
67
68
try:
    resp = conn.sendCA(params)
except Exception as e:
    print("Failed to run: " + str(e))
    sys.exit(1)

resp.printResp()

sys.exit(0)