| 1 |
# This file is part of Ansible |
| 2 |
# |
| 3 |
# Ansible is free software: you can redistribute it and/or modify |
| 4 |
# it under the terms of the GNU General Public License as published by |
| 5 |
# the Free Software Foundation, either version 3 of the License, or |
| 6 |
# (at your option) any later version. |
| 7 |
# |
| 8 |
# Ansible is distributed in the hope that it will be useful, |
| 9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
# GNU General Public License for more details. |
| 12 |
# |
| 13 |
# You should have received a copy of the GNU General Public License |
| 14 |
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
|
| 16 |
from __future__ import annotations |
| 17 |
|
| 18 |
from ansible.module_utils.facts.network.base import Network, NetworkCollector |
| 19 |
|
| 20 |
|
| 21 |
class HPUXNetwork(Network): |
| 22 |
""" |
| 23 |
HP-UX-specific subclass of Network. Defines networking facts: |
| 24 |
- default_interface |
| 25 |
- interfaces (a list of interface names) |
| 26 |
- interface_<name> dictionary of ipv4 address information. |
| 27 |
""" |
| 28 |
platform = 'HP-UX' |
| 29 |
|
| 30 |
def populate(self, collected_facts=None): |
| 31 |
network_facts = {} |
| 32 |
netstat_path = self.module.get_bin_path( |
| 33 |
'netstat', |
| 34 |
opt_dirs=['/usr/bin'] |
| 35 |
) |
| 36 |
|
| 37 |
if netstat_path is None: |
| 38 |
return network_facts |
| 39 |
|
| 40 |
default_interfaces_facts = self.get_default_interfaces() |
| 41 |
network_facts.update(default_interfaces_facts) |
| 42 |
|
| 43 |
interfaces = self.get_interfaces_info() |
| 44 |
network_facts['interfaces'] = interfaces.keys() |
| 45 |
for iface in interfaces: |
| 46 |
network_facts[iface] = interfaces[iface] |
| 47 |
|
| 48 |
return network_facts |
| 49 |
|
| 50 |
def get_default_interfaces(self): |
| 51 |
default_interfaces = {} |
| 52 |
netstat_path = self.module.get_bin_path( |
| 53 |
'netstat', |
| 54 |
opt_dirs=['/usr/bin'] |
| 55 |
) |
| 56 |
|
| 57 |
if netstat_path is None: |
| 58 |
return default_interfaces |
| 59 |
rc, out, err = self.module.run_command("%s -nr" % netstat_path) |
| 60 |
lines = out.splitlines() |
| 61 |
for line in lines: |
| 62 |
words = line.split() |
| 63 |
if len(words) > 1: |
| 64 |
if words[0] == 'default': |
| 65 |
default_interfaces['default_interface'] = words[4] |
| 66 |
default_interfaces['default_gateway'] = words[1] |
| 67 |
|
| 68 |
return default_interfaces |
| 69 |
|
| 70 |
def get_interfaces_info(self): |
| 71 |
interfaces = {} |
| 72 |
netstat_path = self.module.get_bin_path( |
| 73 |
'netstat', |
| 74 |
opt_dirs=['/usr/bin'] |
| 75 |
) |
| 76 |
|
| 77 |
if netstat_path is None: |
| 78 |
return interfaces |
| 79 |
rc, out, err = self.module.run_command("%s -niw" % netstat_path) |
| 80 |
lines = out.splitlines() |
| 81 |
for line in lines: |
| 82 |
words = line.split() |
| 83 |
for i in range(len(words) - 1): |
| 84 |
if words[i][:3] == 'lan': |
| 85 |
device = words[i] |
| 86 |
interfaces[device] = {'device': device} |
| 87 |
address = words[i + 3] |
| 88 |
interfaces[device]['ipv4'] = {'address': address} |
| 89 |
network = words[i + 2] |
| 90 |
interfaces[device]['ipv4'] = {'network': network, |
| 91 |
'interface': device, |
| 92 |
'address': address} |
| 93 |
return interfaces |
| 94 |
|
| 95 |
|
| 96 |
class HPUXNetworkCollector(NetworkCollector): |
| 97 |
_fact_class = HPUXNetwork |
| 98 |
_platform = 'HP-UX' |