When managing the system, you may come across a case where you want to monitor the life and death of other machines with the ping command. In such a case, when using the ping command from a system made with Python, I think that it is common to call the OS ping command using subprocess. However, in this case, since external commands are used, there are problems that it depends on the external environment and that fine control is difficult.
Therefore, this time I will introduce a library for life and death monitoring by socket so that it will be completed in Python.
pings is a library for sending pings in Python. As mentioned above, the ICMP packet used for ping can be sent using socket without using the subprocess command.
** Note **: pings must be run as root / admin user.
It can be installed via pip.
pip install pings
If you want to monitor the life and death status of the target, do as follows. * Be sure to run it as ** root user / admin user ** at runtime.
--Initialize the Ping
object
--Specify the monitoring target in the argument of the ping ()
method
--This method pings the target and returns a response
object as a return value.
--Communication result is stored in ʻis_reached ()` of this object
Specifically, the code looks like this:
.python
import pings
p = pings.Ping() #Ping object creation
res = p.ping("google.com") #Monitor google
if res.is_reached():
#I was able to connect to the monitoring target
do_something()
else:
#Could not connect to the monitored target
do_something()
You can use the response
object obtained as the return value of the ping method to help monitor the network status.
res = p.ping("google.com")
res.print_messages() #Message is displayed
Example of execution result:
PING google.com (172.217.27.174): 55 data bytes
47 bytes from 172.217.27.174: icmp_seq=0 ttl=49 time=32.333 ms
--- google.com ping statistics ---
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max = 32.333/32.333/32.333 ms
To monitor the life and death of multiple machines, do as follows.
.python
import pings
#IP address of life and death monitoring machine
hosts = ["192.168.0.1", "192.168.0.2", "192.168.0.3"]
p = pings.Ping()
#Extract the IP addresses of the machines one by one and execute the ping command
for h in hosts:
res = p.ping(h)
if not res.is_reached():
#What to do if you cannot connect
do_something()
With the default settings, you can ping only once, so use the times
option if you want to send multiple times.
.python
import pings
p = pings.Ping()
res = p.ping("google.com", times=3) #Monitor google,Ping 3 times
res.print_messages()
Example of execution result:
PING google.com (172.217.27.174): 55 data bytes
47 bytes from 172.217.27.174: icmp_seq=0 ttl=49 time=32.333 ms
--- google.com ping statistics ---
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max = 32.333/32.333/32.333 ms
By specifying the quiet
option to False
when initializing the Ping class, the ping result will be displayed on standard output during execution.
.python
import pings
p = pings.Ping(quiet=False)
res = p.ping("google.com") #Monitor google
Example of execution result:
PING google.com (172.217.27.174): 55 data bytes
47 bytes from 172.217.27.174: icmp_seq=0 ttl=49 time=32.333 ms
--- google.com ping statistics ---
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max = 32.333/32.333/32.333 ms
Due to insufficient testing, there may be problems such as not working or not working well in some environments. If you want to use it, please be aware of it before using it.
If you have any problems or comments, please write them in the comments section or in the issue on Github.
http://edo.blog.jp/archives/1790336.html
Recommended Posts