In my Django project, I need to check if the host is working on the local network using the ICMP pin. I found this SO question which answers how to ping something in Python and this SO question which refers to resources explaining how to use the sodoers file.
Customization
The Device model stores the IP address for the host on the local network and after adding a new instance of Device (through the user view, not the administrator) to the database, I review the check to see if the device responds to ping using the AJAX API call that provides opportunities.
Problem
However (from the documentation of the library proposed in the first SO question) "Please note that ICMP messages can only be sent from processes running as root."
I do not want to run Django as the root user, as this is bad practice. However, this part of the process (sending and ICMP ping) must be run as root. If, with the Django view, I want to send a ping package to check the viability of the host, then Django itself should start as root, since this is the process that will call ping.
Decision
These are the solutions that I can think of, and my question is: are there any more efficient ways to execute individual parts of the Django project as root besides these:
- Run Django as root (please no!)
- Queue the ping request so that other processes running as root can periodically check and execute. Maybe something like celery .
Is there an easier way?
I want something like the "Django run as root" library, is this possible?
source share