ValentinBELYN / icmplib

Easily forge ICMP packets and make your own ping and traceroute.
GNU Lesser General Public License v3.0
266 stars 46 forks source link

privileged paramter has no effect #36

Closed inviridi closed 2 years ago

inviridi commented 3 years ago

Hi Valentin,

first of all, thank you very much for this nice library. It is exactly, what I am looking for! Yet, I have two small issues... perhaps you can enlighten me.

The first one is the privileged parameter. Please see the error message below... whether I set it to True or False, does not matter -- I get this error.

a = ping("10.10.10.10", count=3, interval=1, timeout=2, privileged=True) File "/home/fl/SW/miniconda/envs/mib/lib/python3.9/site-packages/icmplib/ping.py", line 141, in ping with _Socket(source, privileged) as sock: File "/home/fl/SW/miniconda/envs/mib/lib/python3.9/site-packages/icmplib/sockets.py", line 97, in __init__ raise SocketPermissionError icmplib.exceptions.SocketPermissionError: Root privileges are required to create the socket

I attempted to use icmplib in an anaconda environment with up-to-date packages. The only thing a little unusual is that it is started from inside a thread. Multiping also does not work here, it exits like this:

RuntimeError: can't register atexit after shutdown Task exception was never retrieved future: <Task finished name='Task-11' coro=<async_ping() done, defined at /home/fl/SW/miniconda/envs/mib/lib/python3.9/site-packages/icmplib/ping.py:168> exception=RuntimeError("can't register atexit after shutdown")> Traceback (most recent call last): File "/home/fl/SW/miniconda/envs/mib/lib/python3.9/site-packages/icmplib/ping.py", line 263, in async_ping address = (await async_resolve(address, family))[0] File "/home/fl/SW/miniconda/envs/mib/lib/python3.9/site-packages/icmplib/utils.py", line 158, in async_resolve lookup = await loop.getaddrinfo( File "/home/fl/SW/miniconda/envs/mib/lib/python3.9/asyncio/base_events.py", line 856, in getaddrinfo return await self.run_in_executor( File "/home/fl/SW/miniconda/envs/mib/lib/python3.9/asyncio/base_events.py", line 809, in run_in_executor executor = concurrent.futures.ThreadPoolExecutor( File "/home/fl/SW/miniconda/envs/mib/lib/python3.9/concurrent/futures/__init__.py", line 49, in __getattr__ from .thread import ThreadPoolExecutor as te File "/home/fl/SW/miniconda/envs/mib/lib/python3.9/concurrent/futures/thread.py", line 37, in <module> threading._register_atexit(_python_exit) File "/home/fl/SW/miniconda/envs/mib/lib/python3.9/threading.py", line 1374, in _register_atexit raise RuntimeError("can't register atexit after shutdown") RuntimeError: can't register atexit after shutdown

But multiping is not that important to me and I just wanted to let you know.

Thanks for your help!

Cheers, Frank

ValentinBELYN commented 2 years ago

Hi @inviridi,

Thank you for your compliments!

Did you follow the procedure described here? https://github.com/ValentinBELYN/icmplib/blob/main/docs/6-use-icmplib-without-privileges.md

When you set the privileged parameter to False, the kernel handles some parts of the ICMP headers.

If you still get the SocketPermissionError exception after setting the privileged parameter to False, that doesn't mean the option has no effect. It's just that your system does not allow you to use this feature (unless you follow the above procedure).

I have to modify the SocketPermissionError message which is not very explicit!

Regarding your second problem, maybe asyncio works differently in your environment. We can deal with this problem later if you want.

inviridi commented 2 years ago

Hi Valentin,

Thanks for taking the time to answer! I followed the procedure described you linked... still no success.

(base) fl@tuxedo:~> echo 'net.ipv4.ping_group_range = 0 2147483647' | sudo tee -a /etc/sysctl.conf [sudo] password for root: net.ipv4.ping_group_range = 0 2147483647 (base) fl@tuxedo:~> sysctl -p tuxedo:~ # sysctl -p net.ipv6.conf.all.disable_ipv6 = 1# added by Insync fs.inotify.max_user_watches = 1048576 net.ipv4.ping_group_range = 0 2147483647 Any idea what could be the reason for the above message? I run an Opensuse 15.3 Linux with up-to-date packages.

I did some reading about why this is all so difficult: disappointing stuff. But I am grateful that you tackled the problem with icmplib! If we cannot get this to work that would be acceptable for me, too. It is just a side endeavour to check whether my IoT-logger can still reach the internet.

ValentinBELYN commented 2 years ago

I did a lot of tests on different versions of openSUSE, including openSUSE 15.3 and Python 3.9 without being able to reproduce your problems.

As you can see below, this works well for the privileged parameter and the multiping function:

valentin@localhost:~> cat /etc/os-release 
NAME="openSUSE Leap"
VERSION="15.3"
ID="opensuse-leap"
ID_LIKE="suse opensuse"
VERSION_ID="15.3"
PRETTY_NAME="openSUSE Leap 15.3"
ANSI_COLOR="0;32"
CPE_NAME="cpe:/o:opensuse:leap:15.3"
BUG_REPORT_URL="https://bugs.opensuse.org"
HOME_URL="https://www.opensuse.org/"
valentin@localhost:~> python3.9
Python 3.9.5 (default, May 05 2021, 15:23:13) [GCC] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from icmplib import ping, multiping, __version__
>>> __version__
'3.0'
>>> ping('google.com', privileged=False)
<Host [142.250.179.78]>
>>> multiping(['google.com'], privileged=False)
[<Host [142.250.179.78]>]

Your issues may be related to the Anaconda environment you are using. Unfortunately, not using it, I cannot tell you more about it.

inviridi commented 2 years ago

What an interesting turn of events! I could reproduce all your commands in the REPL...

(mib) fl@tuxedo:~/SW/MIB/crate/bin> python3.9
Python 3.9.5 (default, Jun  4 2021, 12:28:51) 
[GCC 7.5.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from icmplib import ping, multiping, __version__
>>> __version__
'3.0'
>>> ping('google.com', privileged=False)
<Host [64.233.184.102]>
>>> multiping(['google.com'], privileged=False)
[<Host [64.233.184.139]>]

...but get the same error when I execute the Python file. Will definitely have a closer look and report back.

ValentinBELYN commented 2 years ago

Hi! I hope you have solved your problem. Do not hesitate to give me a feedback 😉 In the meantime, I close this issue since it is not related to this library.

inviridi commented 2 years ago

Thank you for your swift answers and understanding. I will definitely investigate this issue further and get back to you shall questions arise. Cheers!

inviridi commented 2 years ago

Hi Valentin, I found some time this weekend to rework my project and wanted to let you know that it works beautifully now. The only major change I applied was switching the environment manager from conda to pdm. I was reluctant to do so due to my long history and experience with the former but what can I say... your library works as expected now. Thanks a lot for writing it and all the best to you!

ValentinBELYN commented 2 years ago

Great news! Thank you for your support 😃