Skip to content

Commit

Permalink
Include an option to add hostname suffix
Browse files Browse the repository at this point in the history
  • Loading branch information
dormant-user committed Aug 17, 2024
1 parent c5568dc commit fa48c6d
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.idea
venv
__pycache__/
doc_gen/_build
doc_gen/_static
doc_gen/_templates
Expand Down
2 changes: 2 additions & 0 deletions docs/genindex.html
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ <h2 id="S">S</h2>
</ul></td>
<td style="width: 33%; vertical-align: top;"><ul>
<li><a href="index.html#netfuse.config.Settings.start">start (netfuse.config.Settings attribute)</a>
</li>
<li><a href="index.html#netfuse.config.Settings.suffix">suffix (netfuse.config.Settings attribute)</a>
</li>
</ul></td>
</tr></table>
Expand Down
9 changes: 7 additions & 2 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ <h1>——–NetFuse——–<a class="headerlink" href="#netfuse" title="Permal

<dl class="py function">
<dt class="sig sig-object py" id="netfuse.main.dump">
<span class="sig-prename descclassname"><span class="pre">netfuse.main.</span></span><span class="sig-name descname"><span class="pre">dump</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">dry_run</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><span class="pre">bool</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">filepath</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><span class="pre">str</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">output</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><span class="pre">str</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">module</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><span class="pre">module</span></span></em><span class="sig-paren">)</span> <span class="sig-return"><span class="sig-return-icon">&#x2192;</span> <span class="sig-return-typehint"><span class="pre">None</span></span></span><a class="headerlink" href="#netfuse.main.dump" title="Permalink to this definition"></a></dt>
<span class="sig-prename descclassname"><span class="pre">netfuse.main.</span></span><span class="sig-name descname"><span class="pre">dump</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">dry_run</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><span class="pre">bool</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">filepath</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><span class="pre">str</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">output</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><span class="pre">str</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">module</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><span class="pre">module</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">suffix</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><span class="pre">str</span></span></em><span class="sig-paren">)</span> <span class="sig-return"><span class="sig-return-icon">&#x2192;</span> <span class="sig-return-typehint"><span class="pre">None</span></span></span><a class="headerlink" href="#netfuse.main.dump" title="Permalink to this definition"></a></dt>
<dd><p>Dumps all devices’ hostname and IP addresses into the hosts file.</p>
</dd></dl>

Expand Down Expand Up @@ -154,7 +154,12 @@ <h1>——–NetFuse——–<a class="headerlink" href="#netfuse" title="Permal

<dl class="py attribute">
<dt class="sig sig-object py" id="netfuse.config.Settings.router_pass">
<span class="sig-name descname"><span class="pre">router_pass</span></span><em class="property"><span class="w"> </span><span class="p"><span class="pre">=</span></span><span class="w"> </span><span class="pre">None</span></em><a class="headerlink" href="#netfuse.config.Settings.router_pass" title="Permalink to this definition"></a></dt>
<span class="sig-name descname"><span class="pre">router_pass</span></span><em class="property"><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="pre">str</span></em><em class="property"><span class="w"> </span><span class="p"><span class="pre">=</span></span><span class="w"> </span><span class="pre">None</span></em><a class="headerlink" href="#netfuse.config.Settings.router_pass" title="Permalink to this definition"></a></dt>
<dd></dd></dl>

<dl class="py attribute">
<dt class="sig sig-object py" id="netfuse.config.Settings.suffix">
<span class="sig-name descname"><span class="pre">suffix</span></span><em class="property"><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="pre">str</span></em><em class="property"><span class="w"> </span><span class="p"><span class="pre">=</span></span><span class="w"> </span><span class="pre">None</span></em><a class="headerlink" href="#netfuse.config.Settings.suffix" title="Permalink to this definition"></a></dt>
<dd></dd></dl>

</dd></dl>
Expand Down
Binary file modified docs/objects.inv
Binary file not shown.
2 changes: 1 addition & 1 deletion docs/searchindex.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions netfuse/config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
import platform
import time
from os import environ
from typing import Callable, Tuple


Expand All @@ -19,7 +19,8 @@ class Settings:
flush_dns: Tuple = ()
# Unlike Windows and macOS, Ubuntu and Linux Mint do not cache DNS queries at the operating system level by default.
start: float = None
router_pass = None
router_pass: str = None
suffix: str = None


settings = Settings()
Expand All @@ -38,5 +39,6 @@ class Settings:
"sudo resolvectl flush-caches", # Ubuntu 22.04 and higher
"sudo systemd-resolve --flush-caches", # Ubuntu 17.04 and higher (18.04)
)
settings.router_pass = environ.get("router_pass") or environ.get("ROUTER_PASS")
settings.router_pass = os.environ.get("router_pass") or os.environ.get("ROUTER_PASS")
settings.suffix = os.environ.get("suffix") or os.environ.get("SUFFIX") or ""
settings.start = time.time()
25 changes: 21 additions & 4 deletions netfuse/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,15 @@ def flush_dns_cache() -> None:
)


def dump(dry_run: bool, filepath: str, output: str, module: ModuleType) -> None:
def dump(
dry_run: bool, filepath: str, output: str, module: ModuleType, suffix: str
) -> None:
"""Dumps all devices' hostname and IP addresses into the hosts file."""
with open(filepath) as file:
tmp_entries = file.read()
host_entries = ""
inside_block = False
for idx, line in enumerate(tmp_entries.splitlines()):
for line in tmp_entries.splitlines():
if HEADER in line:
inside_block = True
continue
Expand All @@ -92,7 +94,7 @@ def dump(dry_run: bool, filepath: str, output: str, module: ModuleType) -> None:
host_entries += "\n" + "*" * 83 + "\n\n"
for device in module.attached_devices():
if device.ipv4_address:
host_entries += f"{device.ipv4_address}\t{device.name}\n"
host_entries += f"{device.ipv4_address}\t{device.name}{suffix}\n"
else:
LOGGER.debug(
"%s [%s] does not have an IP address", device.name, device.mac_address
Expand All @@ -119,6 +121,13 @@ def dump(dry_run: bool, filepath: str, output: str, module: ModuleType) -> None:
default=settings.host_id,
help="Host ID of the network administrator page",
)
@click.option(
"-s",
"--suffix",
required=False,
default=settings.suffix,
help="Suffix to add to each hostname (eg: .attlocal.net)",
)
@click.option(
"-d",
"--dry",
Expand All @@ -144,6 +153,7 @@ def run(
*args,
model: str,
host_id: int = settings.host_id,
suffix: str = settings.suffix,
dry: bool = False,
path: str = settings.etc_hosts,
output: str = settings.etc_hosts,
Expand All @@ -154,6 +164,7 @@ def run(
*args: Placeholder arg for click module.
model: NetFuse module to choose. Choices are att or netgear.
host_id: Host ID for At&t home network.
suffix: Suffix to add to each hostname.
dry: Boolean flag to enable dry run.
path: Input filepath for host entry file.
output: Output filepath for host entry file.
Expand All @@ -165,7 +176,13 @@ def run(
LOGGER.critical(error)
mapped = {"att": att, "netgear": netgear}
if model in mapped.keys():
dump(dry_run=dry, filepath=path, output=output, module=mapped[model])
dump(
dry_run=dry,
filepath=path,
output=output,
module=mapped[model],
suffix=suffix,
)
else:
raise ValidationError(
"\n\n-m/--model\n\tInput should be 'att' or 'netgear' "
Expand Down

0 comments on commit fa48c6d

Please sign in to comment.