~andreserl/maas/lp1592666

« back to all changes in this revision

Viewing changes to src/provisioningserver/dhcp/probe.py

  • Committer: LaMont Jones
  • Date: 2016-04-11 16:23:26 UTC
  • mfrom: (4900 maas)
  • mto: This revision was merged to the branch mainline in revision 4924.
  • Revision ID: lamont@canonical.com-20160411162326-6ycj8l2j66v2o5es
merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python2.7
2
 
# Copyright 2013-2015 Canonical Ltd.  This software is licensed under the
3
 
# GNU Affero General Public License version 3 (see the file LICENSE).
4
 
 
5
 
"""Probe network on given network interface for a DHCP server.
6
 
 
7
 
This needs to be run as root, in order to be allowed to broadcast on the
8
 
BOOTP port.
9
 
 
10
 
Exit code is zero ("success") if no servers were detected, or the number of
11
 
DHCP servers that were found.
12
 
"""
13
 
 
14
 
import argparse
15
 
from sys import exit
16
 
 
17
 
from provisioningserver.dhcp.detect import probe_dhcp
18
 
 
19
 
 
20
 
argument_parser = argparse.ArgumentParser(description=__doc__)
21
 
 
22
 
 
23
 
def main():
24
 
    argument_parser.add_argument(
25
 
        'interface',
26
 
        help="Probe network on this network interface.")
27
 
 
28
 
    args = argument_parser.parse_args()
29
 
 
30
 
    servers = probe_dhcp(args.interface)
31
 
 
32
 
    num_servers = len(servers)
33
 
    if num_servers == 0:
34
 
        print("No DHCP servers detected.")
35
 
        exit(0)
36
 
    else:
37
 
        print("DHCP servers detected: %s" % ', '.join(sorted(servers)))
38
 
        exit(num_servers)
39
 
 
40
 
if __name__ == "__main__":
41
 
    main()