3 packets transmitted, 3 received, 0% packet loss, time 2003ms \n rtt min/avg/max/mdev = 6.481/6.860/7.104/0.288 ms根据这个帮我写一个fsm,取0丢包,min值,max值,
时间: 2023-12-03 08:45:47 浏览: 66
以下是一个简单的FSM,用于处理收到的ping命令结果:
```python
class PingFSM:
def __init__(self):
self.state = "INIT"
self.packet_loss = None
self.rtt_min = None
self.rtt_max = None
def process_ping_output(self, output):
if self.state == "INIT":
if "0% packet loss" in output:
self.packet_loss = 0
self.state = "RTT_MIN"
elif self.state == "RTT_MIN":
match = re.search("rtt min/avg/max/mdev = (\d+\.\d+)/", output)
if match:
self.rtt_min = float(match.group(1))
self.state = "RTT_MAX"
elif self.state == "RTT_MAX":
match = re.search("rtt min/avg/max/mdev = \d+\.\d+/[\d\.]+/(\d+\.\d+)/", output)
if match:
self.rtt_max = float(match.group(1))
self.state = "DONE"
def get_results(self):
if self.state != "DONE":
return None
return {
"packet_loss": self.packet_loss,
"rtt_min": self.rtt_min,
"rtt_max": self.rtt_max
}
```
使用方法:
```python
output = "3 packets transmitted, 3 received, 0% packet loss, time 2003ms \n rtt min/avg/max/mdev = 6.481/6.860/7.104/0.288 ms"
fsm = PingFSM()
fsm.process_ping_output(output)
results = fsm.get_results()
print(results) # 输出 {"packet_loss": 0, "rtt_min": 6.481, "rtt_max": 7.104}
```
阅读全文