blob: eb1f4e907ac2bcc54e4afb0be31fb9ba714db81c [file] [log] [blame]
Prashanth Katti747ce9d2019-02-07 07:23:48 -06001#!/usr/bin/env python
2
3r"""
4Network generic functions.
5
6"""
7
Michael Walsh91960fd2019-08-19 14:21:20 -05008import re
Prashanth Katti747ce9d2019-02-07 07:23:48 -06009import ipaddress
10from robot.libraries.BuiltIn import BuiltIn
Michael Walsh91960fd2019-08-19 14:21:20 -050011import var_funcs as vf
Prashanth Katti747ce9d2019-02-07 07:23:48 -060012
13
14def netmask_prefix_length(netmask):
15 r"""
16 Return the netmask prefix length.
17
18 Description of argument(s):
19 netmask Netmask value (e.g. "255.255.0.0", "255.255.255.0",
20 "255.252.0.0", etc.).
21 """
22
23 # IP address netmask format: '0.0.0.0/255.255.252.0'
24 return ipaddress.ip_network('0.0.0.0/' + netmask).prefixlen
Michael Walsh91960fd2019-08-19 14:21:20 -050025
26
27def parse_nping_output(output):
28 r"""
29 Parse the output from the nping command and return as a dictionary.
30
31 Example of output value:
32
33 Starting Nping 0.6.47 ( http://nmap.org/nping ) at 2019-08-07 22:05 IST
34 SENT (0.0181s) TCP Source IP:37577 >
35 Destination IP:80 S ttl=64 id=39113 iplen=40 seq=629782493 win=1480
36 SENT (0.2189s) TCP Source IP:37577 >
37 Destination IP:80 S ttl=64 id=39113 iplen=40 seq=629782493 win=1480
38 RCVD (0.4120s) TCP Destination IP:80 >
39 Source IP:37577 SA ttl=49 id=0 iplen=44 seq=1078301364 win=5840 <mss 1380>
40 Max rtt: 193.010ms | Min rtt: 193.010ms | Avg rtt: 193.010ms
41 Raw packets sent: 2 (80B) | Rcvd: 1 (46B) | Lost: 1 (50.00%)
42 Nping done: 1 IP address pinged in 0.43 seconds
43
44 Example of data returned by this function:
45
46 nping_result:
47 [max_rtt]: 193.010ms
48 [min_rtt]: 193.010ms
49 [avg_rtt]: 193.010ms
50 [raw_packets_sent]: 2 (80B)
51 [rcvd]: 1 (46B)
52 [lost]: 1 (50.00%)
53 [percent_lost]: 50.00
54
55 Description of argument(s):
56 output The output obtained by running an nping
57 command.
58 """
59
60 lines = output.split("\n")
61 # Obtain only the lines of interest.
62 lines = list(filter(lambda x: re.match(r"(Max rtt|Raw packets)", x),
63 lines))
64
65 key_value_list = []
66 for line in lines:
67 key_value_list += line.split("|")
68 nping_result = vf.key_value_list_to_dict(key_value_list)
69 # Extract percent_lost value from lost field.
70 nping_result['percent_lost'] = \
71 float(nping_result['lost'].split(" ")[-1].strip("()%"))
72 return nping_result