scalpels/scripts/port-input-traffic.sh
Kun Huang 698c248ca0 parse rpc data before write into db
sca start -a rpc
sleep 20
sca stop
sca report
[you will see two results here: pkts and bytes]

Change-Id: Ibab207c43ef1a5509c8f85c0003cee48c4b66625
2015-10-29 16:05:06 +08:00

43 lines
1.1 KiB
Bash
Executable File

#!/bin/bash
# Author: Kun Huang <academicgareth@gmail.com>
port=$1
protocol=${2:-tcp}
chain="PREROUTING"
if [ -z "$port" ]; then
echo "You must specify a port"
exit 1
fi
if ! [[ "$port" =~ ^[0-9]+$ ]] ; then
echo $port is not numberic
exit 1
fi
if [ "$port" -ge 65536 ] || [ "$port" -le 0 ] ; then
echo $port is not valid in 1~65535
exit 1
fi
rule="$chain -p $protocol --dport $port"
#XXX iptables -A INPUT -p tcp --dport 5672
#echo applying rule: $rule
sudo iptables -t mangle -A $rule
interval=3
packages=0
bytes=0
trap 'break' INT
while [ 1 -eq 1 ] ; do
sleep $interval
n_packages=`sudo iptables -t mangle -L $chain -n -v -x | grep $port | grep $protocol | tail -n 1 | awk '{print $1}'`
n_bytes=`sudo iptables -t mangle -L $chain -n -v -x | grep $port | grep $protocol | tail -n 1 | awk '{print $2}'`
python -c "print '%0.2f pkt/s %0.2f byte/s' % (float($n_packages-$packages)/int($interval), float($n_bytes-$bytes)/int($interval))"
packages=$n_packages
bytes=$n_bytes
done
#echo deleting rule: $rule
sudo iptables -t mangle -D $rule