diff options
author | Robin Krahl <guskraro@student.gu.se> | 2017-10-14 04:16:44 +0000 |
---|---|---|
committer | Robin Krahl <guskraro@student.gu.se> | 2017-10-14 04:16:44 +0000 |
commit | 9dcd19548b57e80726d53ec4f9253f1809a2d04a (patch) | |
tree | 2c6149935395caef4c9d353b29d052dc6477817d /broccoli/script | |
parent | 4cae9ed6e99755af6a42bfeaa042537458f18805 (diff) | |
download | midbro-9dcd19548b57e80726d53ec4f9253f1809a2d04a.tar.gz midbro-9dcd19548b57e80726d53ec4f9253f1809a2d04a.tar.bz2 |
script: measure-packets: Adapt for usage over SSH
Diffstat (limited to 'broccoli/script')
-rw-r--r-- | broccoli/script/measure-packets.sh | 45 |
1 files changed, 20 insertions, 25 deletions
diff --git a/broccoli/script/measure-packets.sh b/broccoli/script/measure-packets.sh index af38624..a141885 100644 --- a/broccoli/script/measure-packets.sh +++ b/broccoli/script/measure-packets.sh @@ -1,11 +1,21 @@ #!/bin/bash +# This function has to execute the given arguments on the target machine. +# For local execution, this could look like: +# sudo bash -c "$@" +# Or for remote execution: +# ssh -i ~/.ssh/id_rsa root@remote "$@" +# Make sure that the command is executed by root. + +function execute_command { + bash -c "$@" +} + function measure_packets { TCPREPLAY_SPEED=$1 TCPREPLAY_COUNT=$2 - bro -i "${BRO_INTERFACE}" -C -b Log::default_writer=Log::WRITER_NONE "${BRO_SCRIPT}" 2> bro-err.txt & - BRO_PID=$! + BRO_PID=$(execute_command "bro -i \"${BRO_INTERFACE}\" -C -b Log::default_writer=Log::WRITER_NONE \"${BRO_SCRIPT}\" > ${BRO_DIR}/bro-out.txt 2> ${BRO_DIR}/bro-err.txt & echo \$!") tcpreplay -i "${BRO_INTERFACE}" -M "${TCPREPLAY_SPEED}" -L "${TCPREPLAY_COUNT}" "${TCPREPLAY_DUMP}" > /dev/null 2> /dev/null @@ -13,13 +23,13 @@ function measure_packets { while [[ $(echo "${PCPU}>50" | bc) -eq 1 ]] do sleep 1 - PCPU=$(ps -q ${BRO_PID} -o pcpu --no-headers) + PCPU=$(execute_command "ps -q ${BRO_PID} -o pcpu --no-headers") done - kill -SIGINT "${BRO_PID}" - sleep 1 + execute_command "kill -SIGINT \"${BRO_PID}\"" + execute_command "while kill -0 ${BRO_PID} 2>/dev/null ; do sleep 0.1 ; done" - tail -1 bro-err.txt | awk -F' ' '{print $5}' + execute_command "tail -1 ${BRO_DIR}/bro-err.txt" | sed 's/.* \([0-9]\+\) packets received.*/\1/' } if [[ $# -ne 3 ]] @@ -36,24 +46,12 @@ then exit 1 fi -if [[ $(id -u) -ne 0 ]] -then - echo "Must be run as root. Aborting." - exit 1 -fi - BRO_SCRIPT=$1 BRO_INTERFACE=$2 TCPREPLAY_DUMP=$3 -SPEEDS=(400 200 100 50 25) -COUNTS=($(seq 400000 100000 2600000)) - -if [[ ! -r "${BRO_SCRIPT}" ]] -then - echo "The Bro script '${BRO_SCRIPT}' does not exist. Aborting." - exit 1 -fi +SPEEDS=(100 50 25) +COUNTS=(1000000 2000000 4000000) if [[ ! -r "${TCPREPLAY_DUMP}" ]] then @@ -61,12 +59,9 @@ then exit 1 fi -BRO_SCRIPT=$(realpath "${BRO_SCRIPT}") TCPREPLAY_DUMP=$(realpath "${TCPREPLAY_DUMP}") -BRO_DIR=$(mktemp --directory --tmpdir bro.XXX) - -cd "${BRO_DIR}" +BRO_DIR=$(execute_command "mktemp --directory --tmpdir bro.XXX") echo -ne "sent\t" for SPEED in ${SPEEDS[@]} @@ -86,4 +81,4 @@ do echo done -rm -rf "${BRO_DIR}" +execute_command "rm -rf \"${BRO_DIR}\"" |