]>
Commit | Line | Data |
---|---|---|
35681c06 | 1 | #!/usr/bin/env python3 |
15ef32ea | 2 | # |
d486dd0d | 3 | # Copyright 2014-2017 Cumulus Networks, Inc. All rights reserved. |
15ef32ea RP |
4 | # Author: Roopa Prabhu, roopa@cumulusnetworks.com |
5 | # | |
6 | ||
15ef32ea | 7 | import os |
223ba5af | 8 | import errno |
15ef32ea | 9 | |
d486dd0d JF |
10 | try: |
11 | from ifupdown2.ifupdown.utils import utils | |
12 | from ifupdown2.ifupdownaddons.utilsbase import * | |
bd441a51 | 13 | except (ImportError, ModuleNotFoundError): |
d486dd0d JF |
14 | from ifupdown.utils import utils |
15 | from ifupdownaddons.utilsbase import * | |
16 | ||
15ef32ea RP |
17 | |
18 | class dhclient(utilsBase): | |
19 | """ This class contains helper methods to interact with the dhclient | |
20 | utility """ | |
21 | ||
22 | def _pid_exists(self, pidfilename): | |
23 | if os.path.exists(pidfilename): | |
223ba5af JF |
24 | try: |
25 | return os.readlink( | |
26 | "/proc/%s/exe" % self.read_file_oneline(pidfilename) | |
27 | ).endswith("dhclient") | |
28 | except OSError as e: | |
29 | try: | |
30 | if e.errno == errno.EACCES: | |
31 | return os.path.exists("/proc/%s" % self.read_file_oneline(pidfilename)) | |
32 | except: | |
33 | return False | |
34 | except: | |
15ef32ea | 35 | return False |
223ba5af | 36 | return False |
15ef32ea RP |
37 | |
38 | def is_running(self, ifacename): | |
39 | return self._pid_exists('/run/dhclient.%s.pid' %ifacename) | |
40 | ||
41 | def is_running6(self, ifacename): | |
42 | return self._pid_exists('/run/dhclient6.%s.pid' %ifacename) | |
43 | ||
717cee31 RP |
44 | def _run_dhclient_cmd(self, cmd, cmd_prefix=None): |
45 | if not cmd_prefix: | |
46 | cmd_aslist = [] | |
47 | else: | |
48 | cmd_aslist = cmd_prefix.split() | |
49 | if cmd_aslist: | |
50 | cmd_aslist.extend(cmd) | |
51 | else: | |
52 | cmd_aslist = cmd | |
a193d8d1 | 53 | utils.exec_commandl(cmd_aslist, stdout=None, stderr=None) |
717cee31 RP |
54 | |
55 | def stop(self, ifacename, cmd_prefix=None): | |
15ef32ea RP |
56 | if os.path.exists('/sbin/dhclient3'): |
57 | cmd = ['/sbin/dhclient3', '-x', '-pf', | |
58 | '/run/dhclient.%s.pid' %ifacename, '-lf', | |
59 | '/var/lib/dhcp3/dhclient.%s.leases' %ifacename, | |
60 | '%s' %ifacename] | |
61 | else: | |
62 | cmd = ['/sbin/dhclient', '-x', '-pf', | |
63 | '/run/dhclient.%s.pid' %ifacename, | |
64 | '-lf', '/var/lib/dhcp/dhclient.%s.leases' %ifacename, | |
65 | '%s' %ifacename] | |
717cee31 | 66 | self._run_dhclient_cmd(cmd, cmd_prefix) |
15ef32ea | 67 | |
717cee31 | 68 | def start(self, ifacename, wait=True, cmd_prefix=None): |
15ef32ea RP |
69 | if os.path.exists('/sbin/dhclient3'): |
70 | cmd = ['/sbin/dhclient3', '-pf', | |
71 | '/run/dhclient.%s.pid' %ifacename, | |
72 | '-lf', '/var/lib/dhcp3/dhclient.%s.leases' %ifacename, | |
73 | '%s' %ifacename] | |
74 | else: | |
717cee31 RP |
75 | cmd = ['/sbin/dhclient', '-pf', |
76 | '/run/dhclient.%s.pid' %ifacename, '-lf', | |
77 | '/var/lib/dhcp/dhclient.%s.leases' %ifacename, | |
15ef32ea | 78 | '%s' %ifacename] |
a0a8d7e0 | 79 | if not wait: |
6d359159 | 80 | cmd.append('-nw') |
717cee31 | 81 | self._run_dhclient_cmd(cmd, cmd_prefix) |
15ef32ea | 82 | |
717cee31 | 83 | def release(self, ifacename, cmd_prefix=None): |
15ef32ea RP |
84 | if os.path.exists('/sbin/dhclient3'): |
85 | cmd = ['/sbin/dhclient3', '-r', '-pf', | |
86 | '/run/dhclient.%s.pid' %ifacename, '-lf', | |
87 | '/var/lib/dhcp3/dhclient.%s.leases' %ifacename, | |
88 | '%s' %ifacename] | |
89 | else: | |
90 | cmd = ['/sbin/dhclient', '-r', '-pf', | |
91 | '/run/dhclient.%s.pid' %ifacename, | |
92 | '-lf', '/var/lib/dhcp/dhclient.%s.leases' %ifacename, | |
93 | '%s' %ifacename] | |
717cee31 | 94 | self._run_dhclient_cmd(cmd, cmd_prefix) |
15ef32ea | 95 | |
f62cfb7b | 96 | def start6(self, ifacename, wait=True, cmd_prefix=None, duid=None): |
717cee31 | 97 | cmd = ['/sbin/dhclient', '-6', '-pf', |
15ef32ea | 98 | '/run/dhclient6.%s.pid' %ifacename, '-lf', |
004d1e65 | 99 | '/var/lib/dhcp/dhclient6.%s.leases' % ifacename, |
6d359159 | 100 | '%s' %ifacename] |
a0a8d7e0 | 101 | if not wait: |
6d359159 | 102 | cmd.append('-nw') |
f62cfb7b RD |
103 | if duid is not None: |
104 | cmd.append('-D') | |
105 | cmd.append(duid) | |
717cee31 | 106 | self._run_dhclient_cmd(cmd, cmd_prefix) |
15ef32ea | 107 | |
f62cfb7b | 108 | def stop6(self, ifacename, cmd_prefix=None, duid=None): |
717cee31 | 109 | cmd = ['/sbin/dhclient', '-6', '-x', '-pf', |
004d1e65 JF |
110 | '/run/dhclient6.%s.pid' % ifacename, '-lf', |
111 | '/var/lib/dhcp/dhclient6.%s.leases' % ifacename, | |
717cee31 | 112 | '%s' %ifacename] |
f62cfb7b RD |
113 | if duid is not None: |
114 | cmd.append('-D') | |
115 | cmd.append(duid) | |
717cee31 | 116 | self._run_dhclient_cmd(cmd, cmd_prefix) |
15ef32ea | 117 | |
f62cfb7b | 118 | def release6(self, ifacename, cmd_prefix=None, duid=None): |
717cee31 | 119 | cmd = ['/sbin/dhclient', '-6', '-r', '-pf', |
20fd3a06 | 120 | '/run/dhclient6.%s.pid' %ifacename, |
de4104b0 | 121 | '-lf', '/var/lib/dhcp/dhclient6.%s.leases' % ifacename, |
717cee31 | 122 | '%s' %ifacename] |
f62cfb7b RD |
123 | if duid is not None: |
124 | cmd.append('-D') | |
125 | cmd.append(duid) | |
717cee31 | 126 | self._run_dhclient_cmd(cmd, cmd_prefix) |