-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
get_lan_ip: better algorithm
- Loading branch information
Showing
14 changed files
with
168 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#!/usr/bin/env python3 | ||
""" | ||
example of using findssh from a Python script | ||
""" | ||
|
||
import asyncio | ||
|
||
import findssh | ||
|
||
PORT = 22 # default SSH port | ||
TIMEOUT = 1.0 # seconds | ||
# timeout needs to be finite otherwise non-existant hosts are waited for forever | ||
|
||
ownIP = findssh.get_lan_ip() | ||
print("own address", ownIP) | ||
net = findssh.address2net(ownIP) | ||
print("searching", net) | ||
|
||
asyncio.run(findssh.get_hosts(net, PORT, TIMEOUT)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#!/usr/bin/env python3 | ||
""" | ||
example of using findssh from a Python script. | ||
Sequential non-parallel host discovery is impractically slow | ||
""" | ||
|
||
import findssh | ||
from findssh.base import get_hosts_seq | ||
|
||
PORT = 22 # default SSH port | ||
TIMEOUT = 1.0 # seconds | ||
# timeout needs to be finite otherwise non-existant hosts are waited for forever | ||
|
||
ownIP = findssh.get_lan_ip() | ||
print("own address", ownIP) | ||
net = findssh.address2net(ownIP) | ||
print("searching", net) | ||
|
||
for host in get_hosts_seq(net, PORT, TIMEOUT): | ||
print(host) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#!/usr/bin/env python3 | ||
""" | ||
example of using findssh from a Python script. | ||
Threads are MUCH slower than using asyncio as in asyncio_get_hosts.py | ||
""" | ||
|
||
import findssh | ||
import findssh.threadpool | ||
|
||
PORT = 22 # default SSH port | ||
TIMEOUT = 1.0 # seconds | ||
# timeout needs to be finite otherwise non-existant hosts are waited for forever | ||
|
||
ownIP = findssh.get_lan_ip() | ||
print("own address", ownIP) | ||
net = findssh.address2net(ownIP) | ||
print("searching", net) | ||
|
||
for host in findssh.threadpool.get_hosts(net, PORT, TIMEOUT): | ||
print(host) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,30 @@ | ||
from __future__ import annotations | ||
import socket | ||
import ipaddress | ||
|
||
from .coro import get_hosts | ||
from .base import netfromaddress, getLANip | ||
|
||
__all__ = ["get_hosts", "netfromaddress", "getLANip"] | ||
__all__ = ["get_hosts", "address2net", "get_lan_ip"] | ||
|
||
|
||
def get_lan_ip() -> ipaddress.IPv4Address | ipaddress.IPv6Address: | ||
""" | ||
get IP address of currently used LAN interface | ||
ref: http://stackoverflow.com/a/23822431 | ||
""" | ||
|
||
return ipaddress.ip_address(socket.gethostbyname(socket.gethostname())) | ||
|
||
|
||
def address2net( | ||
addr: ipaddress.IPv4Address, mask: str = "24" | ||
) -> ipaddress.IPv4Network | ipaddress.IPv6Network: | ||
|
||
if isinstance(addr, ipaddress.IPv4Address): | ||
net = ipaddress.ip_network(addr.exploded.rsplit(".", 1)[0] + f".0/{mask}") | ||
elif isinstance(addr, ipaddress.IPv6Address): | ||
net = ipaddress.ip_network(addr.exploded.rsplit(":", 1)[0] + f":0/{mask}") | ||
else: | ||
raise TypeError(addr) | ||
|
||
return net |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.