@@ -43,7 +43,7 @@ def dispatch(self, node: Node) -> Task:
4343
4444
4545class PingLinuxImplementation (Task ):
46- requirements = ["sudo apt-get install -y inetutils -ping" ]
46+ requirements = ["sudo apt-get install -y iputils -ping" ]
4747
4848 def __init__ (self , address : str , count : int = 1 , * args , ** kwargs ):
4949 self .address = address .strip ()
@@ -57,47 +57,56 @@ def run(self):
5757
5858 def _format (self , output : str ) -> PingResult :
5959 raw_output = output [:]
60- lines = [x for x in output .split ("\n " ) if x ]
61- if not lines [- 1 ].startswith ("round-trip" ):
60+
61+ try :
62+ lines = [x for x in output .split ("\n " ) if x ]
63+
64+ # parse rtt statistics
65+ rtts , unit = lines [- 1 ].split ("=" )[1 ].strip ().split (" " )
66+ rtt_min , rtt_avg , rtt_max , rtt_stddev = [float (x ) for x in rtts .split ("/" )]
67+ lines = lines [:- 1 ]
68+
69+ # take packet_loss line and packets received
70+ _ , packets_received , packet_loss , _ = lines [- 1 ].split ("," )
71+ packets_received = int (packets_received .strip ().split (" " )[0 ])
72+ packet_loss = float (
73+ packet_loss .removesuffix ("packet loss" ).strip ().split ("%" )[0 ]
74+ )
75+ lines = lines [:- 1 ]
76+
77+ # remove first and last line
78+ lines = lines [1 :- 1 ]
79+
80+ # parse received packets
81+ packets = []
82+ for packet in lines [:packets_received ]:
83+ packet = packet .split (":" )[1 ]
84+ seq , ttl , time , unit = [x .strip () for x in packet .split (" " ) if x ]
85+ seq = int (seq .split ("=" )[1 ])
86+ ttl = int (ttl .split ("=" )[1 ])
87+ time = float (time .split ("=" )[1 ])
88+ packets .append (PacketResult (seq , ttl , time , unit ))
89+ lines = lines [packets_received :]
90+
91+ # theoretically, here should be 0 lines left
92+ unparsed_output = lines
93+
94+ return PingResult (
95+ self .address ,
96+ packets ,
97+ packet_loss ,
98+ rtt_min ,
99+ rtt_avg ,
100+ rtt_max ,
101+ rtt_stddev ,
102+ unit ,
103+ unparsed_output ,
104+ raw_output ,
105+ )
106+ except Exception :
62107 return PingResult (self .address , [], 100 , 0 , 0 , 0 , 0 , "" , [], raw_output )
63108
64- # parse rtt statistics
65- rtts , unit = lines [- 1 ].split ("=" )[1 ].strip ().split (" " )
66- rtt_min , rtt_avg , rtt_max , rtt_stddev = [float (x ) for x in rtts .split ("/" )]
67- lines = lines [:- 1 ]
68-
69- # take packet_loss line and packets received
70- packets_received , packet_loss = lines [- 1 ].split ("," )[1 :]
71- packets_received = int (packets_received .strip ().split (" " )[0 ])
72- packet_loss = float (packet_loss .strip ().split ("%" )[0 ])
73- lines = lines [:- 1 ]
74-
75- # remove first and last line
76- lines = lines [1 :- 1 ]
77-
78- # parse received packets
79- packets = []
80- for packet in lines [:packets_received ]:
81- packet = packet .split (":" )[1 ]
82- seq , ttl , time , unit = [x .strip () for x in packet .split (" " ) if x ]
83- seq = int (seq .split ("=" )[1 ])
84- ttl = int (ttl .split ("=" )[1 ])
85- time = float (time .split ("=" )[1 ])
86- packets .append (PacketResult (seq , ttl , time , unit ))
87- lines = lines [packets_received :]
88-
89- # theoretically, here should be 0 lines left
90- unparsed_output = lines
91-
92- return PingResult (
93- self .address ,
94- packets ,
95- packet_loss ,
96- rtt_min ,
97- rtt_avg ,
98- rtt_max ,
99- rtt_stddev ,
100- unit ,
101- unparsed_output ,
102- raw_output ,
103- )
109+
110+ if __name__ == "__main__" :
111+ ping = PingLinuxImplementation ("8.8.8.8" , count = 10 )
112+ print (ping .run ())
0 commit comments