| 1 |
def provides_facts(): |
| 2 |
""" |
| 3 |
Returns a dictionary keyed on the facts provided by this module. The value |
| 4 |
of each key is the doc string describing the fact. |
| 5 |
""" |
| 6 |
return { |
| 7 |
"_iri_hostname": "A dictionary keyed by internal routing instance " |
| 8 |
"ip addresses. The value of each key is the " |
| 9 |
"internal routing instance hostname for the ip", |
| 10 |
"_iri_ip": "A dictionary keyed by internal routing instance " |
| 11 |
"hostnames. The value of each key is the internal " |
| 12 |
"routing instance ip for the hostname", |
| 13 |
} |
| 14 |
|
| 15 |
|
| 16 |
def get_facts(device): |
| 17 |
""" |
| 18 |
Gathers facts from a <file-show/> RPC on the '/etc/hosts.junos' file. |
| 19 |
""" |
| 20 |
iri_hostname = None |
| 21 |
iri_ip = None |
| 22 |
|
| 23 |
rsp = device.rpc.file_show(filename="/etc/hosts.junos", normalize=False) |
| 24 |
|
| 25 |
if rsp is not None: |
| 26 |
hosts_file_content = rsp.findtext(".", default="") |
| 27 |
if hosts_file_content is not None: |
| 28 |
for line in hosts_file_content.splitlines(): |
| 29 |
(line, _, _) = line.partition("#") |
| 30 |
components = line.split(None) |
| 31 |
if len(components) > 1: |
| 32 |
ip = components[0] |
| 33 |
hosts = components[1:] |
| 34 |
if iri_hostname is None: |
| 35 |
iri_hostname = {} |
| 36 |
if iri_ip is None: |
| 37 |
iri_ip = {} |
| 38 |
if ip in iri_hostname: |
| 39 |
iri_hostname[ip] += hosts |
| 40 |
else: |
| 41 |
iri_hostname[ip] = hosts |
| 42 |
for host in hosts: |
| 43 |
if host in iri_ip: |
| 44 |
iri_ip[host].append(ip) |
| 45 |
else: |
| 46 |
iri_ip[host] = [ip] |
| 47 |
for host in hosts: |
| 48 |
# Handle templates with %d |
| 49 |
if "%d" in host: |
| 50 |
octets = ip.split(".", 3) |
| 51 |
for count in range(255): |
| 52 |
t_ip = ( |
| 53 |
octets[0] |
| 54 |
+ "." |
| 55 |
+ octets[1] |
| 56 |
+ "." |
| 57 |
+ str(count) |
| 58 |
+ "." |
| 59 |
+ octets[3] |
| 60 |
) |
| 61 |
t_host = host.replace("%d", str(count)) |
| 62 |
if t_ip in iri_hostname: |
| 63 |
iri_hostname[t_ip].append(t_host) |
| 64 |
else: |
| 65 |
iri_hostname[t_ip] = [t_host] |
| 66 |
if t_host in iri_ip: |
| 67 |
iri_ip[t_host].append(t_ip) |
| 68 |
else: |
| 69 |
iri_ip[t_host] = [t_ip] |
| 70 |
|
| 71 |
return { |
| 72 |
"_iri_hostname": iri_hostname, |
| 73 |
"_iri_ip": iri_ip, |
| 74 |
} |