Network scanner - Scanner
- class codestral_ros2_gen.network_scan.network_scanner.NetworkScanner(timeout=5.0, packet_size=64, sending_interval=0.05, send_buff_size=65536, recv_buff_size=131072, logger=None)[source]
Bases:
object
- NetworkScanner performs a complete network scan by:
Setting scan parameters.
Utilizing a ScanOperation context manager that configures both send and receive sockets.
Sending ICMP packets synchronously and then collecting responses asynchronously.
Formatting and reporting the results upon scan completion.
- Example usage:
>>> from codestral_ros2_gen.network_scan.network_scanner import NetworkScanner >>> scanner = NetworkScanner() >>> hosts = scanner.scan("192.168.10.0/24") >>> print(scanner.format_results(hosts, show_all=False))
- Parameters:
timeout (float)
packet_size (int)
sending_interval (float)
send_buff_size (int)
recv_buff_size (int)
logger (Logger | None)
- __init__(timeout=5.0, packet_size=64, sending_interval=0.05, send_buff_size=65536, recv_buff_size=131072, logger=None)[source]
Initialize the network scanner with configurable parameters.
- Parameters:
timeout (
float
) – Timeout for response in seconds.packet_size (
int
) – Size of the ICMP echo request packets.sending_interval (
float
) – Interval between sending packets.send_buff_size (
int
) – Buffer size for sending socket.recv_buff_size (
int
) – Buffer size for receiving socket.logger (
Optional
[Logger
]) – Optional logger instance.
- scan(targets)[source]
Execute a network scan synchronously for the specified targets.
- This method uses asyncio.run to run an asynchronous scan operation which:
Configures the scan environment within a ScanOperation context.
Synchronously sends ICMP packets via a blocking send socket.
Asynchronously gathers responses via a non-blocking receive socket.
- Parameters:
targets (
str
) – Network targets in CIDR format or as comma-separated IP addresses.- Return type:
Dict
[str
,dict
]- Returns:
A dictionary mapping host IP addresses to their scan results (state, response time, error).
- async _scan_async(targets)[source]
Asynchronously execute the network scan operation.
Within the ScanOperation context, this method first configures both send and receive sockets, then sends ICMP packets synchronously, and finally initiates an asynchronous loop to collect responses.
- Parameters:
targets (
str
) – Network targets in CIDR or comma-separated IP format.- Return type:
Dict
[str
,dict
]- Returns:
A dictionary of scan results for each target host.
- format_results(results, show_all=False)[source]
Format the scan results into a human-readable table.
The table includes headers for IP address, state, response time (ms), and any error messages. An appended summary shows the total scanned hosts, counts for hosts that are up, down, with errors, and the total scan duration. If show_all is False, only hosts with state “UP” are displayed.
- Parameters:
results (
Dict
[str
,dict
]) – The dictionary of scan results returned by scan().show_all (
bool
) – Whether to display all hosts or only those with state “UP”.
- Return type:
str
- Returns:
A formatted string report of the scan results.