Debian Codegraph @

icmplib/multiping.py

raw
1 '''
2 icmplib
3 ~~~~~~~
4
5 Easily forge ICMP packets and make your own ping and traceroute.
6
7 https://github.com/ValentinBELYN/icmplib
8
9 :copyright: Copyright 2017-2023 Valentin BELYN.
10 :license: GNU LGPLv3, see the LICENSE for details.
11
12 ~~~~~~~
13
14 This program is free software: you can redistribute it and/or
15 modify it under the terms of the GNU Lesser General Public License
16 as published by the Free Software Foundation, either version 3 of
17 the License, or (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU Lesser General Public License for more details.
23
24 You should have received a copy of the GNU Lesser General Public
25 License along with this program. If not, see
26 <https://www.gnu.org/licenses/>.
27 '''
28
29 import asyncio
30
31 from .ping import async_ping
32
33
34 async def async_multiping(addresses, count=2, interval=0.5, timeout=2,
35 concurrent_tasks=50, source=None, family=None, privileged=True,
36 **kwargs):
37 '''
38 Send ICMP Echo Request packets to several network hosts.
39
40 This function is non-blocking.
41
42 :type addresses: list[str]
43 :param addresses: The IP addresses of the hosts to which messages
44 should be sent. Hostnames and FQDNs are allowed but not
45 recommended. You can easily retrieve their IP address by calling
46 the built-in `async_resolve` function.
47
48 :type count: int, optional
49 :param count: The number of ping to perform per address.
50 Default to 2.
51
52 :type interval: int or float, optional
53 :param interval: The interval in seconds between sending each packet.
54 Default to 0.5.
55
56 :type timeout: int or float, optional
57 :param timeout: The maximum waiting time for receiving a reply in
58 seconds. Default to 2.
59
60 :type concurrent_tasks: int, optional
61 :param concurrent_tasks: The maximum number of concurrent tasks to
62 speed up processing. This value cannot exceed the maximum number
63 of file descriptors configured on the operating system.
64 Default to 50.
65
66 :type source: str, optional
67 :param source: The IP address from which you want to send packets.
68 By default, the interface is automatically chosen according to
69 the specified destinations. This parameter should not be used if
70 you are passing both IPv4 and IPv6 addresses to this function.
71
72 :type family: int, optional
73 :param family: The address family if a hostname or FQDN is specified.
74 Can be set to `4` for IPv4 or `6` for IPv6 addresses. By default,
75 this function searches for IPv4 addresses first before searching
76 for IPv6 addresses.
77
78 :type privileged: bool, optional
79 :param privileged: When this option is enabled, this library fully
80 manages the exchanges and the structure of ICMP packets.
81 Disable this option if you want to use this function without
82 root privileges and let the kernel handle ICMP headers.
83 Default to True.
84 Only available on Unix systems. Ignored on Windows.
85
86 Advanced (**kwags):
87
88 :type payload: bytes, optional
89 :param payload: The payload content in bytes. A random payload is
90 used by default.
91
92 :type payload_size: int, optional
93 :param payload_size: The payload size. Ignored when the `payload`
94 parameter is set. Default to 56.
95
96 :type traffic_class: int, optional
97 :param traffic_class: The traffic class of ICMP packets.
98 Provides a defined level of service to packets by setting the DS
99 Field (formerly TOS) or the Traffic Class field of IP headers.
100 Packets are delivered with the minimum priority by default
101 (Best-effort delivery).
102 Intermediate routers must be able to support this feature.
103 Only available on Unix systems. Ignored on Windows.
104
105 :rtype: list[Host]
106 :returns: A list of `Host` objects containing statistics about the
107 desired destinations. The list is sorted in the same order as
108 the addresses passed in parameters.
109
110 :raises NameLookupError: If you pass a hostname or FQDN in
111 parameters and it does not exist or cannot be resolved.
112 :raises SocketPermissionError: If the privileges are insufficient to
113 create the socket.
114 :raises SocketAddressError: If the source address cannot be assigned
115 to the socket.
116 :raises ICMPSocketError: If another error occurs. See the
117 `ICMPv4Socket` or `ICMPv6Socket` class for details.
118
119 Usage::
120
121 >>> import asyncio
122 >>> from icmplib import async_multiping
123 >>> hosts = asyncio.run(async_multiping(['10.0.0.5', '::1']))
124
125 >>> for host in hosts:
126 ... if host.is_alive:
127 ... print(f'{host.address} is up!')
128 ... else:
129 ... print(f'{host.address} is down!')
130
131 10.0.0.5 is down!
132 ::1 is up!
133
134 See the `Host` class for details.
135
136 '''
137 loop = asyncio.get_running_loop()
138 tasks = []
139 tasks_pending = set()
140
141 for address in addresses:
142 if len(tasks_pending) >= concurrent_tasks:
143 _, tasks_pending = await asyncio.wait(
144 tasks_pending,
145 return_when=asyncio.FIRST_COMPLETED)
146
147 task = loop.create_task(
148 async_ping(
149 address=address,
150 count=count,
151 interval=interval,
152 timeout=timeout,
153 source=source,
154 family=family,
155 privileged=privileged,
156 **kwargs))
157
158 tasks.append(task)
159 tasks_pending.add(task)
160
161 await asyncio.wait(tasks_pending)
162
163 return [task.result() for task in tasks]
164
165
166 def multiping(addresses, count=2, interval=0.5, timeout=2,
167 concurrent_tasks=50, source=None, family=None, privileged=True,
168 **kwargs):
169 '''
170 Send ICMP Echo Request packets to several network hosts.
171
172 :type addresses: list[str]
173 :param addresses: The IP addresses of the hosts to which messages
174 should be sent. Hostnames and FQDNs are allowed but not
175 recommended. You can easily retrieve their IP address by calling
176 the built-in `resolve` function.
177
178 :type count: int, optional
179 :param count: The number of ping to perform per address.
180 Default to 2.
181
182 :type interval: int or float, optional
183 :param interval: The interval in seconds between sending each packet.
184 Default to 0.5.
185
186 :type timeout: int or float, optional
187 :param timeout: The maximum waiting time for receiving a reply in
188 seconds. Default to 2.
189
190 :type concurrent_tasks: int, optional
191 :param concurrent_tasks: The maximum number of concurrent tasks to
192 speed up processing. This value cannot exceed the maximum number
193 of file descriptors configured on the operating system.
194 Default to 50.
195
196 :type source: str, optional
197 :param source: The IP address from which you want to send packets.
198 By default, the interface is automatically chosen according to
199 the specified destinations. This parameter should not be used if
200 you are passing both IPv4 and IPv6 addresses to this function.
201
202 :type family: int, optional
203 :param family: The address family if a hostname or FQDN is specified.
204 Can be set to `4` for IPv4 or `6` for IPv6 addresses. By default,
205 this function searches for IPv4 addresses first before searching
206 for IPv6 addresses.
207
208 :type privileged: bool, optional
209 :param privileged: When this option is enabled, this library fully
210 manages the exchanges and the structure of ICMP packets.
211 Disable this option if you want to use this function without
212 root privileges and let the kernel handle ICMP headers.
213 Default to True.
214 Only available on Unix systems. Ignored on Windows.
215
216 Advanced (**kwags):
217
218 :type payload: bytes, optional
219 :param payload: The payload content in bytes. A random payload is
220 used by default.
221
222 :type payload_size: int, optional
223 :param payload_size: The payload size. Ignored when the `payload`
224 parameter is set. Default to 56.
225
226 :type traffic_class: int, optional
227 :param traffic_class: The traffic class of ICMP packets.
228 Provides a defined level of service to packets by setting the DS
229 Field (formerly TOS) or the Traffic Class field of IP headers.
230 Packets are delivered with the minimum priority by default
231 (Best-effort delivery).
232 Intermediate routers must be able to support this feature.
233 Only available on Unix systems. Ignored on Windows.
234
235 :rtype: list[Host]
236 :returns: A list of `Host` objects containing statistics about the
237 desired destinations. The list is sorted in the same order as
238 the addresses passed in parameters.
239
240 :raises NameLookupError: If you pass a hostname or FQDN in
241 parameters and it does not exist or cannot be resolved.
242 :raises SocketPermissionError: If the privileges are insufficient to
243 create the socket.
244 :raises SocketAddressError: If the source address cannot be assigned
245 to the socket.
246 :raises ICMPSocketError: If another error occurs. See the
247 `ICMPv4Socket` or `ICMPv6Socket` class for details.
248
249 Usage::
250
251 >>> from icmplib import multiping
252 >>> hosts = multiping(['10.0.0.5', '127.0.0.1', '::1'])
253
254 >>> for host in hosts:
255 ... if host.is_alive:
256 ... print(f'{host.address} is up!')
257 ... else:
258 ... print(f'{host.address} is down!')
259
260 10.0.0.5 is down!
261 127.0.0.1 is up!
262 ::1 is up!
263
264 See the `Host` class for details.
265
266 '''
267 return asyncio.run(
268 async_multiping(
269 addresses=addresses,
270 count=count,
271 interval=interval,
272 timeout=timeout,
273 concurrent_tasks=concurrent_tasks,
274 source=source,
275 family=family,
276 privileged=privileged,
277 **kwargs))