Upgrade RF syntax for v3.2 compatibility
[integration/test.git] / csit / libraries / Tcpdump.robot
1 *** Settings ***
2 Documentation     Library to catch traffic/packets using linux tcpdump command
3 Library           SSHLibrary
4 Resource          SSHKeywords.robot
5 Resource          Utils.robot
6 Resource          RemoteBash.robot
7 Variables         ../variables/Variables.py
8
9 *** Variables ***
10 ${dumpalias}      tcpdump
11 ${dumppcap}       dump.pcap
12 ${dumppcappath}    /tmp/${dumppcap}
13 ${dumpcmd}        sudo tcpdump -s 0 -w ${dumppcappath}
14 ${dump_default_name}    tcpDump
15
16 *** Keywords ***
17 Start Tcpdumping
18     [Arguments]    ${system}=${TOOLS_SYSTEM_IP}    ${user}=${TOOLS_SYSTEM_USER}    ${password}=${TOOLS_SYSTEM_PASSWORD}    ${prompt}=${TOOLS_SYSTEM_PROMPT}    ${timeout}=5s    ${eth}=eth0
19     ...    ${more_params}=${None}
20     [Documentation]    Connects to the remote machine via ssh and starts tcpdump linux command
21     ${currentcon}=    SSHLibrary.Get Connection    index=True
22     SSHLibrary.Open Connection    ${system}    prompt=${prompt}    timeout=${timeout}    alias=${dumpalias}
23     SSHKeywords.Flexible SSH Login    ${user}    password=${password}    delay=${timeout}
24     SSHLibrary.Write    ${dumpcmd} -i ${eth} ${more_params}
25     Run Keyword If    ${currentcon}==${None}    Return From Keyword
26     SSHLibrary.Switch Connection    ${currentcon}
27
28 Stop Tcpdumping And Download
29     [Arguments]    ${filename}=${dumppcap}.xz
30     [Documentation]    Stops catching packets with tcpdump and download the saved file
31     ${oldcon}=    SSHLibrary.Switch Connection    ${dumpalias}
32     RemoteBash.Write_Bare_Ctrl_C
33     SSHLibrary.Read
34     ${stdout}=    SSHLibrary.Execute Command    xz -9ekvv ${dumppcappath}
35     Log    ${stdout}
36     ${stdout}=    SSHLibrary.Execute Command    ls -la /tmp
37     Log    ${stdout}
38     SSHLibrary.Get File    ${dumppcappath}.xz    ${filename}
39     SSHLibrary.Close Connection
40     Run Keyword If    ${oldcon}==${None}    Return From Keyword
41     SSHLibrary.Switch Connection    ${oldcon}
42
43 Start Packet Capture On Node
44     [Arguments]    ${node_ip}    ${file_Name}=${dump_default_name}    ${network_Adapter}=eth0    ${user}=${DEFAULT_USER}    ${password}=${EMPTY}    ${prompt}=${DEFAULT_LINUX_PROMPT}
45     ...    ${prompt_timeout}=${DEFAULT_TIMEOUT}    ${filter}=${EMPTY}
46     [Documentation]    Connects to the remote machine and starts tcpdump
47     ${current_ssh_connection}=    SSHLibrary.Get Connection
48     ${conn_id}=    SSHLibrary.Open Connection    ${node_ip}    prompt=${prompt}    timeout=${prompt_timeout}
49     SSHKeywords.Flexible SSH Login    ${user}    ${password}
50     ${cmd} =    Set Variable    sudo /usr/sbin/tcpdump -vvv -ni ${networkAdapter} ${filter} -w /tmp/${file_Name}.pcap
51     ${stdout}    ${stderr} =    SSHLibrary.Start Command    ${cmd}
52     Log    ${stderr}
53     Log    ${stdout}
54     [Teardown]    SSHKeywords.Restore_Current_SSH_Connection_From_Index    ${current_ssh_connection.index}
55     [Return]    ${conn_id}
56
57 Stop Packet Capture on Node
58     [Arguments]    ${conn_id}
59     [Documentation]    This keyword will list the running processes looking for tcpdump and then kill the process with the name tcpdump
60     SSHLibrary.Switch Connection    ${conn_id}
61     ${stdout} =    SSHLibrary.Execute Command    sudo ps -elf | grep tcpdump
62     Log    ${stdout}
63     ${stdout}    ${stderr} =    SSHLibrary.Execute Command    sudo pkill -f tcpdump    return_stderr=True
64     Log    ${stderr}
65     Log    ${stdout}
66     ${stdout} =    SSHLibrary.Execute Command    sudo xz -9ekvv /tmp/*.pcap
67     Log    ${stdout}
68     ${stdout} =    SSHLibrary.Execute Command    sudo ls -ls /tmp
69     Log    ${stdout}
70
71 Start Packet Capture on Nodes
72     [Arguments]    ${tag}=${EMPTY}    ${filter}=${EMPTY}    ${ips}=@{EMPTY}
73     [Documentation]    Start packet captures on the given list of node ips.
74     ...    The captures will be named with the tag and ip.
75     @{conn_ids} =    BuiltIn.Create List    @{EMPTY}
76     FOR    ${ip}    IN    @{ips}
77         ${fname} =    BuiltIn.Catenate    SEPARATOR=__    ${tag}    ${ip}
78         ${conn_id} =    Tcpdump.Start Packet Capture on Node    ${ip}    file_Name=${fname}    filter=${filter}
79         Collections.Append To List    ${conn_ids}    ${conn_id}
80     END
81     [Return]    @{conn_ids}
82
83 Stop Packet Capture on Nodes
84     [Arguments]    ${conn_ids}=@{EMPTY}
85     [Documentation]    Stop the packet captures on the given list of node connection ids
86     FOR    ${conn_id}    IN    @{conn_ids}
87         Stop Packet Capture on Node    ${conn_id}
88     END