Refactor suite to use RemoteBash, clean Utils
[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          Utils.robot
5 Resource          RemoteBash.robot
6 Variables         ../variables/Variables.py
7
8 *** Variables ***
9 ${dumpalias}      tcpdump
10 ${dumppcap}       dump.pcap
11 ${dumppcappath}    /tmp/${dumppcap}
12 ${dumpcmd}        sudo tcpdump -s 0 -w ${dumppcappath}
13 ${dump_default_name}    tcpDump
14
15 *** Keywords ***
16 Start Tcpdumping
17     [Arguments]    ${system}=${TOOLS_SYSTEM_IP}    ${user}=${TOOLS_SYSTEM_USER}    ${password}=${TOOLS_SYSTEM_PASSWORD}    ${prompt}=${TOOLS_SYSTEM_PROMPT}    ${timeout}=5s    ${eth}=eth0
18     ...    ${more_params}=${None}
19     [Documentation]    Connects to the remote machine via ssh and starts tcpdump linux command
20     ${currentcon}=    SSHLibrary.Get Connection    index=True
21     SSHLibrary.Open Connection    ${system}    prompt=${prompt}    timeout=${timeout}    alias=${dumpalias}
22     Utils.Flexible SSH Login    ${user}    password=${password}    delay=${timeout}
23     SSHLibrary.Write    ${dumpcmd} -i ${eth} ${more_params}
24     Run Keyword If    ${currentcon}==${None}    Return From Keyword
25     SSHLibrary.Switch Connection    ${currentcon}
26
27 Stop Tcpdumping And Download
28     [Arguments]    ${filename}=${dumppcap}.xz
29     [Documentation]    Stops catching packets with tcpdump and download the saved file
30     ${oldcon}=    SSHLibrary.Switch Connection    ${dumpalias}
31     RemoteBash.Write_Bare_Ctrl_C
32     SSHLibrary.Read
33     ${stdout}=    SSHLibrary.Execute Command    xz -9ekvv ${dumppcappath}
34     Log    ${stdout}
35     ${stdout}=    SSHLibrary.Execute Command    ls -la /tmp
36     Log    ${stdout}
37     SSHLibrary.Get File    ${dumppcappath}.xz    ${filename}
38     SSHLibrary.Close Connection
39     Run Keyword If    ${oldcon}==${None}    Return From Keyword
40     SSHLibrary.Switch Connection    ${oldcon}
41
42 Start Packet Capture On Node
43     [Arguments]    ${node_ip}    ${file_Name}=${dump_default_name}    ${network_Adapter}=eth0    ${user}=${DEFAULT_USER}    ${password}=${EMPTY}    ${prompt}=${DEFAULT_LINUX_PROMPT}
44     ...    ${prompt_timeout}=${DEFAULT_TIMEOUT}
45     [Documentation]    Connects to the remote machine and starts tcpdump
46     ${current_ssh_connection}=    SSHLibrary.Get Connection
47     ${conn_id}=    SSHLibrary.Open Connection    ${node_ip}    prompt=${prompt}    timeout=${prompt_timeout}
48     Flexible SSH Login    ${user}    ${password}
49     ${cmd} =    Set Variable    sudo /usr/sbin/tcpdump -vvv -ni ${networkAdapter} -w /tmp/${file_Name}.pcap
50     ${stdout}    ${stderr} =    SSHLibrary.Start Command    ${cmd}
51     Log    ${stderr}
52     Log    ${stdout}
53     [Teardown]    SSHKeywords.Restore_Current_SSH_Connection_From_Index    ${current_ssh_connection.index}
54     [Return]    ${conn_id}
55
56 Stop Packet Capture on Node
57     [Arguments]    ${conn_id}
58     [Documentation]    This keyword will list the running processes looking for tcpdump and then kill the process with the name tcpdump
59     SSHLibrary.Switch Connection    ${conn_id}
60     ${stdout} =    SSHLibrary.Execute Command    sudo ps -elf | grep tcpdump
61     Log    ${stdout}
62     ${stdout}    ${stderr} =    SSHLibrary.Execute Command    sudo pkill -f tcpdump    return_stderr=True
63     Log    ${stderr}
64     Log    ${stdout}
65     ${stdout} =    SSHLibrary.Execute Command    sudo xz -9ekvv /tmp/*.pcap
66     Log    ${stdout}
67     ${stdout} =    SSHLibrary.Execute Command    sudo ls -ls /tmp
68     Log    ${stdout}
69     SSHLibrary. Close Connection