summaryrefslogtreecommitdiffstats
path: root/command-stubs/dhcpclient-stub
blob: b5ffdc78db2f98fa0f711eea0399040bd86388b5 (plain)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/python
#
# dhcpclient-stub
#
# Copyright (C) 2007  Red Hat, Inc.  All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

import os
import sys
import getopt

# for testing
if (os.path.exists('isys')):
    sys.path.append('isys')

sys.path.append('/usr/lib/anaconda')

import isys
from sys import argv

import network
from network import NetworkDevice

def showusage():
    print "Usage: dhcpclient [-4] [-6] [-a] [-i device] [-c class]"

def showhelp():
    showusage()
    print "Options:"
    print "    -4              Configure IPv4 stack via DHCP"
    print "    -6              Configure IPv6 stack (DHCPv6 unless -a given)"
    print "    -a              Use IPv6 auto neighbor discovery"
    print "    -i device       Device to configure (e.g., eth0)"
    print "    -c class        Optional DHCP class name"
    print "Defaults:"
    print "    dhcpclient -4 -6 -a -i eth0"

if __name__ == "__main__":
    dev = NetworkDevice('eth0')
    dev.set(('bootproto', 'dhcp'))

    auto = False
    stacks = 0

    help = False
    unknown = False

    try:
        opts, args = getopt.getopt(sys.argv[1:], '46ai:c:',
                                   ['ipv4', 'ipv6', 'auto', 'interface',
                                    'class', 'help'])
    except getopt.GetoptError:
        help = True

    for o, a in opts:
        if o in ('-4', '--ipv4'):
            stacks += 4
        elif o in ('-6', '--ipv6'):
            stacks += 6
        elif o in ('-a', '--auto'):
            auto = True
        elif o in ('-i', '--interface'):
            dev.set(('device', a))
        elif o in ('-c', '--class'):
            dev.set(('dhcpclass', a))
        elif o in ('--help'):
            help = True
        else:
            unknown = True

    if help:
        showhelp()
        sys.exit(0)

    if unknown:
        showusage()
        sys.exit(1)

    if auto:
        dev.set(('ipv6_autoconf', 'yes'))
    else:
        dev.set(('ipv6_autoconf', 'no'))

    if stacks == 10:
        dev.set(('useipv4', True))
        dev.set(('useipv6', True))
    elif stacks == 6:
        dev.set(('useipv4', False))
        dev.set(('useipv6', True))
    elif stacks == 4:
        dev.set(('useipv4', True))
        dev.set(('useipv6', False))

    try:
        ns = isys.dhcpNetDevice(dev)
        if ns:
            f = open('/etc/resolv.conf', 'w')
            f.write("nameserver %s\n" % ns)
            f.close()
    except:
        print "Error configuring device %s." % (dev.get('device'),)

    sys.exit(0)