解析 ping 的输出
Parsing output from ping
尝试通过 ping IP 地址解析数据时遇到问题。我一直在尝试从 ping 结果中解析数据并将其格式化为:
IP, TimeToPing (ms)
10.1.2.3, 10
这是脚本目前所在的位置:
import sys
import ipaddress
import subprocess
import os
import re
#Main routine
def main():
address = sys.argv[1]
pingthis = ['ping', '-c', '1', address]
header = "IP, TimeToPing (ms)"
subprocess.call(pingthis)
re.search(r'.* time=(.*) ms', os.system('ping -c1'))
if __name__ == "__main__":
main()
这是你想要的吗?
import re
import subprocess
import sys
from tabulate import tabulate
def main():
address = sys.argv[1]
pingthis = ['ping', '-c', '1', address]
r = (
subprocess
.run(
pingthis,
stdout=subprocess.PIPE,
check=True,
)
.stdout
.decode('utf-8')
)
table = tabulate(
[[address, (re.search(r'time=(\d+)', r).group(1))]],
headers=["IP", "TimeToPing (ms)"],
tablefmt="simple",
)
print(table)
if __name__ == "__main__":
main()
python main.py 8.8.8.8
的输出
IP TimeToPing (ms)
------- -----------------
8.8.8.8 14
尝试通过 ping IP 地址解析数据时遇到问题。我一直在尝试从 ping 结果中解析数据并将其格式化为:
IP, TimeToPing (ms)
10.1.2.3, 10
这是脚本目前所在的位置:
import sys
import ipaddress
import subprocess
import os
import re
#Main routine
def main():
address = sys.argv[1]
pingthis = ['ping', '-c', '1', address]
header = "IP, TimeToPing (ms)"
subprocess.call(pingthis)
re.search(r'.* time=(.*) ms', os.system('ping -c1'))
if __name__ == "__main__":
main()
这是你想要的吗?
import re
import subprocess
import sys
from tabulate import tabulate
def main():
address = sys.argv[1]
pingthis = ['ping', '-c', '1', address]
r = (
subprocess
.run(
pingthis,
stdout=subprocess.PIPE,
check=True,
)
.stdout
.decode('utf-8')
)
table = tabulate(
[[address, (re.search(r'time=(\d+)', r).group(1))]],
headers=["IP", "TimeToPing (ms)"],
tablefmt="simple",
)
print(table)
if __name__ == "__main__":
main()
python main.py 8.8.8.8
IP TimeToPing (ms)
------- -----------------
8.8.8.8 14