ping parsing是一个用于分析ping命令输出的cli工具/python库。
pingparsing的Python项目详细描述
ping解析
CLI Usage
包含在打包的pingparsing中的cli。pingparsing命令可以执行以下操作:
- 执行ping并分析结果
- 分析ping结果文件
- 从标准输入进行分析
Execute ping and parse the result
如果将pingparsing命令的目标指定为位置参数, 该命令对每个目标执行ping,并分析结果。 ping将对多个目标并行执行。 解析结果以json格式输出。
$ pingparsing google.com { "google.com": { "destination": "google.com", "packet_transmit": 10, "packet_receive": 10, "packet_loss_rate": 0.0, "packet_loss_count": 0, "rtt_min": 34.189, "rtt_avg": 46.054, "rtt_max": 63.246, "rtt_mdev": 9.122, "packet_duplicate_rate": 0.0, "packet_duplicate_count": 0 } }
$ pingparsing google.com twitter.com { "google.com": { "destination": "google.com", "packet_transmit": 10, "packet_receive": 10, "packet_loss_rate": 0.0, "packet_loss_count": 0, "rtt_min": 37.341, "rtt_avg": 44.538, "rtt_max": 53.997, "rtt_mdev": 5.827, "packet_duplicate_rate": 0.0, "packet_duplicate_count": 0 }, "twitter.com": { "destination": "twitter.com", "packet_transmit": 10, "packet_receive": 10, "packet_loss_rate": 0.0, "packet_loss_count": 0, "rtt_min": 45.377, "rtt_avg": 68.819, "rtt_max": 78.581, "rtt_mdev": 9.769, "packet_duplicate_rate": 0.0, "packet_duplicate_count": 0 } }
$ pingparsing google.com -c 3 --icmp-reply { "google.com": { "destination": "google.com", "packet_transmit": 3, "packet_receive": 3, "packet_loss_count": 0, "packet_loss_rate": 0.0, "rtt_min": 136.097, "rtt_avg": 140.476, "rtt_max": 148.341, "rtt_mdev": 5.589, "packet_duplicate_count": 0, "packet_duplicate_rate": 0.0, "icmp_replies": [ { "timestamp": null, "icmp_seq": 1, "ttl": 39, "time": 148.0, "duplicate": false }, { "timestamp": null, "icmp_seq": 2, "ttl": 39, "time": 136.0, "duplicate": false }, { "timestamp": null, "icmp_seq": 3, "ttl": 39, "time": 136.0, "duplicate": false } ] } }
Parse ping result file
Input: | ^{pr 4}$ |
---|---|
Output: | ^{pr 5}$ ^{pr 6}$ |
Parse from the standard input
$ ping -i 0.2 -w 20192.168.2.101 | pingparsing - { "destination": "192.168.2.101", "packet_transmit": 99, "packet_receive": 88, "packet_loss_count": 11, "packet_loss_rate": 11.11111111111111, "rtt_min": 1.615, "rtt_avg": 26.581, "rtt_max": 93.989, "rtt_mdev": 19.886, "packet_duplicate_count": 0, "packet_duplicate_rate": 0.0 }
CLI help
usage: pingparsing [-h] [-V] [--max-workers MAX_WORKERS] [--indent INDENT] [--icmp-reply] [--debug | --quiet] [--timestamp {none,epoch,datetime}] [-c COUNT] [-w DEADLINE] [--timeout TIMEOUT] [-I INTERFACE] destination_or_file [destination_or_file ...] positional arguments: destination_or_file Destinations to send ping, or files to parse. '-' for parse the standard input. optional arguments: -h, --help show this help message and exit -V, --version show program's version number and exit --max-workers MAX_WORKERS Number of threads for when multiple destination/file specified. defaults to equals to two times number of cores. --indent INDENT JSON output will be pretty-printed with the indent level. (default= 4) --icmp-reply, --icmp-replies print results for each ICMP packet reply. --debug for debug print. --quiet suppress execution log messages. Ping Options: --timestamp {none,epoch,datetime} [Only for LINUX] none: no timestamps. epoch: add timestamps with UNIX epoch time format. datetime: add timestamps with ISO time format. -c COUNT, --count COUNT Stop after sending the count. see also ping(8) [-c count] option description. -w DEADLINE, --deadline DEADLINE Timeout before ping exits. valid time units are: d/day/days, h/hour/hours, m/min/mins/minute/minutes, s/sec/secs/second/seconds, ms/msec/msecs/millisecond/milliseconds, us/usec/usecs/microsecond/microseconds. if no unit string found, considered seconds as the time unit. see also ping(8) [-w deadline] option description. note: meaning of the 'deadline' may differ system to system. --timeout TIMEOUT Time to wait for a response per packet. Valid time units are: d/day/days, h/hour/hours, m/min/mins/minute/minutes, s/sec/secs/second/seconds, ms/msec/msecs/millisecond/milliseconds, us/usec/usecs/microsecond/microseconds. if no unit string found, considered milliseconds as the time unit. Attempt to send packets with milliseconds granularity in default. If the system does not support timeout in milliseconds, round up as seconds. Use system default if not specified. This option wll ignored if the system does not support timeout itself. See also ping(8) [-W timeout] option description. note: meaning of the 'timeout' may differ system to system. -I INTERFACE, --interface INTERFACE network interface Documentation: https://pingparsing.rtfd.io/ Issue tracker: https://github.com/thombashi/pingparsing/issues
Library Usage
Execute ping and parse the result
PingTransmitter类可以执行ping命令并获取 ping输出为字符串。
Sample Code: | ^{pr 9}$ |
---|---|
Output: | ^{pr 10}$ |
Parsing ^{tt1}$ command output
Sample Code: | ^{pr 11}$ |
---|---|
Output: | ^{pr 12}$ |
建议执行ping命令
建议使用以下方法执行ping命令以获取用于分析的输出。 这些命令包括将语言环境设置临时更改为英语的操作。
Linux
LC_ALL=C ping <host or IP address> -w <seconds> [option] > <output.file>
窗口
> chcp Active code page: <XXX> # get current code page > chcp 437# change code page to english > ping <host or IP address> -n <ping count> > <output.file> > chcp <XXX> # restore code page
Installation
pip install pingparsing
Supported Environments
- Linux
- 窗口
- MacOS
Tested Environments
OS | ping version |
---|---|
Ubuntu 16.04 | ^{tt12}$ |
Ubuntu 18.04 | ^{tt13}$ |
Debian 8.6 | ^{tt14}$ |
Fedora 25 | ^{tt15}$ |
Windows 10 | ^{tt16}$ |
macOS 10.13 | ^{tt16}$ |
Premise
pingparsing要求ping命令执行环境中的语言环境为英语。 使用任何其他语言环境分析ping命令输出可能会失败。 这是因为ping命令的输出将根据区域设置而更改。