Skip to content

Commit

Permalink
Merge pull request #16 from thombashi/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
thombashi committed Oct 15, 2016
2 parents 1001889 + 02797d3 commit e37af92
Show file tree
Hide file tree
Showing 14 changed files with 707 additions and 342 deletions.
258 changes: 142 additions & 116 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,104 @@ Installation
Premise
=======

This library expects to output language of the ``ping`` command is in
English. The ``ping`` command output in any other languages is may fail
to parse. This is because the output of the ``ping`` command is changed
depending on the language.
This library expects locale setup to English. Parsing the ``ping``
command output with any other locale may fail. This is because the
output of the ``ping`` command is changed depending on the locale
setting.

Usage
=====

Execute ping and parse
----------------------

``PingTransmitter`` class can execute ``ping`` command and obtain the
ping output as a string.

Sample code
~~~~~~~~~~~

``examples/ping_sample.py``

Sample output: Debian 8
~~~~~~~~~~~~~~~~~~~~~~~

.. code:: console
./ping_sample.py -d 192.168.0.1
# returncode ---
0
# properties ---
packet_transmit: 10 packets
packet_receive: 10 packets
packet_loss: 0.0 %
rtt_min: 0.445
rtt_avg: 1.069
rtt_max: 4.854
rtt_mdev: 1.312
# asdict ---
{
"packet_loss": 0.0,
"packet_receive": 10,
"packet_transmit": 10,
"rtt_min": 0.445,
"rtt_max": 4.854,
"rtt_mdev": 1.312,
"rtt_avg": 1.069
}
Example execution result: Windows 10
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. code:: console
>ping_sample.py -d google.com
# returncode ---
0
# properties ---
packet_transmit: 10 packets
packet_receive: 10 packets
packet_loss: 0.0 %
rtt_min: 30.0
rtt_avg: 37.0
rtt_max: 58.0
rtt_mdev: None
# asdict ---
{
"rtt_avg": 37.0,
"rtt_max": 58.0,
"packet_loss": 0.0,
"packet_transmit": 10,
"rtt_min": 30.0,
"rtt_mdev": null,
"packet_receive": 10
}
Note: ``rtt_mdev`` not available with Windows


Parsing ``ping`` command output
-------------------------------

Recommended ping command execution
==================================
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The following methods are recommended to execute ``ping`` command for
parsing. These will change the language setting to English temporarily.
parsing. These will change the locale setting to English temporarily.

Linux
-----
^^^^^

.. code:: console
LC_ALL=C ping <host or IP address> -w <seconds> [option] > <output.file>
Windows
-------
^^^^^^^

.. code:: console
Expand All @@ -53,18 +131,18 @@ Windows
> ping <host or IP address> -n <ping count> > <output.file>
> chcp <XXX> # restore code page
Reference
~~~~~~~~~
- Reference

https://technet.microsoft.com/en-us/library/cc733037
- https://technet.microsoft.com/en-us/library/cc733037

Usage
=====

Sample code
~~~~~~~~~~~

``examples/parse_sample.py``

Parsing ``ping`` command output
-------------------------------

Example: Debian 8.2 w/ iputils-ping 20121221-5+b2
Example: Debian 8
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Input
Expand All @@ -80,54 +158,43 @@ Input
60 packets transmitted, 60 received, 0% packet loss, time 59153ms
rtt min/avg/max/mdev = 61.425/99.731/212.597/27.566 ms

Sample code
^^^^^^^^^^^

``parse_sample.py``

.. code:: python
import pingparsing
ping_parser = pingparsing.PingParsing()
with open("ping.txt") as f:
ping_parser.parse(f.read())
print("packet_transmit:", ping_parser.packet_transmit)
print("packet_receive:", ping_parser.packet_receive)
print("packet_loss:", ping_parser.packet_loss)
print("rtt_min:", ping_parser.rtt_min)
print("rtt_avg:", ping_parser.rtt_avg)
print("rtt_max:", ping_parser.rtt_max)
print("rtt_mdev:", ping_parser.rtt_mdev)
print(ping_parser.as_dict())
Output
^^^^^^

.. code:: console
python parse_sample.py
./parse_sample.py -f ping.txt
# properties ---
packet_transmit: 60
packet_receive: 60
packet_loss: 0.0
rtt_min: 61.425
rtt_avg: 99.731
rtt_max: 212.597
rtt_mdev: 27.566
{'rtt_avg': 99.731, 'packet_loss': 0.0, 'packet_transmit': 60, 'rtt_max': 212.597, 'rtt_min': 61.425, 'rtt_mdev': 27.566, 'packet_receive': 60}
Example: Windows 7 SP1
~~~~~~~~~~~~~~~~~~~~~~
# asdict ---
{
"rtt_avg": 99.731,
"packet_transmit": 60,
"rtt_max": 212.597,
"packet_loss": 0.0,
"rtt_min": 61.425,
"rtt_mdev": 27.566,
"packet_receive": 60
}
Example: Windows 10
~~~~~~~~~~~~~~~~~~~

Input
^^^^^

.. code:: console
>ping google.com -n 10 > ping_win7.txt
>ping google.com -n 10 > ping_win.txt
>type ping_win7.txt
>type ping_win.txt
Pinging google.com [216.58.196.238] with 32 bytes of data:
Reply from 216.58.196.238: bytes=32 time=87ms TTL=51
Expand All @@ -146,79 +213,40 @@ Input
Approximate round trip times in milli-seconds:
Minimum = 56ms, Maximum = 194ms, Average = 107ms
Sample code
^^^^^^^^^^^

``parse_sample.py``

.. code:: python
import pingparsing
ping_parser = pingparsing.PingParsing()
with open("ping_win7.txt") as f:
ping_parser.parse(f.read())
print(ping_parser.as_dict())
Output
^^^^^^

.. code:: console
>python parse_sample.py
{'packet_loss': 0.0, 'packet_receive': 10, 'packet_transmit': 10, 'rtt_min': 56.0, 'rtt_max': 194.0, 'rtt_mdev': None, '
rtt_avg': 107.0}
Execute ping and parse
----------------------

``PingTransmitter`` class can execute ``ping`` command and obtain the
ping output as a string.

Sample code
~~~~~~~~~~~

``ping_sample.py``

.. code:: python
import pingparsing
transmitter = pingparsing.PingTransmitter()
transmitter.destination_host = "192.168.0.1"
transmitter.waittime = 60
result = transmitter.ping()
ping_parser = pingparsing.PingParsing()
ping_parser.parse(result)
print(ping_parser.as_dict())
Example execution result: Debian 8.2 w/ iputils-ping 20121221-5+b2
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. code:: console
# python ping_sample.py
{'packet_loss': 0.0, 'packet_receive': 60, 'packet_transmit': 60, 'rtt_min': 0.814, 'rtt_max': 27.958, 'rtt_mdev': 3.574, 'rtt_avg': 2.004}
Example execution result: Windows 7 SP1
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. code:: console
>python ping_sample.py
{'packet_loss': 0.0, 'packet_receive': 60, 'packet_transmit': 60, 'rtt_min': 0.0
, 'rtt_max': 56.0, 'rtt_mdev': None, 'rtt_avg': 2.0}
parse_sample.py -f ping_win.txt
# properties ---
packet_transmit: 10
packet_receive: 10
packet_loss: 0.0
rtt_min: 56.0
rtt_avg: 107.0
rtt_max: 194.0
rtt_mdev: None
# asdict ---
{
"packet_loss": 0.0,
"packet_transmit": 10,
"rtt_min": 56.0,
"rtt_avg": 107.0,
"packet_receive": 10,
"rtt_max": 194.0,
"rtt_mdev": null
}
Dependencies
============

Python 2.7 or 3.3+
Python 2.7+ or 3.3+

- `DataPropery <https://github.com/thombashi/DataProperty>`__
- `pyparsing <https://pyparsing.wikispaces.com/>`__
- `six <https://pypi.python.org/pypi/six/>`__

Test dependencies
-----------------
Expand All @@ -230,14 +258,12 @@ Test dependencies
Tested Environment
==================

+-----------------+----------------------------------+
| OS | ping version |
+=================+==================================+
| Debian 8.2 | iputils-ping 20121221-5+b2 |
+-----------------+----------------------------------+
| Debian 5.0.10 | iputils-ping 20071127-1+lenny1 |
+-----------------+----------------------------------+
| Windows 7 SP1 | ``-`` |
+-----------------+----------------------------------+
| Windows 8.1 | ``-`` |
+-----------------+----------------------------------+
+--------------+-----------------------------------+
| OS | ping version |
+==============+===================================+
| Debian 8.6 | iputils-ping 20121221-5+b2 |
+--------------+-----------------------------------+
| Fedora 24 | iputils-20160308-3.fc24.x86\_64 |
+--------------+-----------------------------------+
| Windows 10 | ``-`` |
+--------------+-----------------------------------+
Empty file added examples/__init__.py
Empty file.
23 changes: 23 additions & 0 deletions examples/examplecommon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env python
# encoding: utf-8

"""
.. codeauthor:: Tsuyoshi Hombashi <gogogo.vm@gmail.com>
"""

from __future__ import print_function
import json


def print_ping_parser(ping_parser):
print("# properties ---")
print("packet_transmit: {:d} packets".format(ping_parser.packet_transmit))
print("packet_receive: {:d} packets".format(ping_parser.packet_receive))
print("packet_loss: {:.1f} %".format(ping_parser.packet_loss))
print("rtt_min:", ping_parser.rtt_min)
print("rtt_avg:", ping_parser.rtt_avg)
print("rtt_max:", ping_parser.rtt_max)
print("rtt_mdev:", ping_parser.rtt_mdev)
print()
print("# asdict ---")
print(json.dumps(ping_parser.as_dict(), indent=4))
37 changes: 37 additions & 0 deletions examples/parse_sample.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env python
# encoding: utf-8

"""
.. codeauthor:: Tsuyoshi Hombashi <gogogo.vm@gmail.com>
"""

from __future__ import print_function
import argparse
import sys

import examplecommon
import pingparsing


def parse_option():
parser = argparse.ArgumentParser()
parser .add_argument(
"-f", "--file", required=True, help="input file path")

return parser.parse_args()


def main():
options = parse_option()

ping_parser = pingparsing.PingParsing()
with open(options.file) as f:
ping_parser.parse(f.read())

examplecommon.print_ping_parser(ping_parser)

return 0


if __name__ == "__main__":
sys.exit(main())
Loading

0 comments on commit e37af92

Please sign in to comment.