Hello,
I am trying to make a script that can show the data transfer rate just like what you got when running the command
I gather some information on the internet but the result my script shows has nothing to do with reality.
In order to simplified things for now I am only focus on the incoming traffic side, when I'll understand the logic and where my error is I'll do the rest after.
According to this site, the formula is:
_ the amount of data collected is obtained by using the
_ transfer time is one second because it is simpler(divided by one is like doing nothing), so the while loop runs the operation every second.
In theory it looks like an easy thing to do, but still I am struggling, the results are far from okay.
My incoming internet speed is around 1.5MB/s the script shows something like 2749.
I am pretty bad at math so I probably missed an important point here, can you guys guide me on this?
I have no merit for the following script I've been largely inspired by this video on youtube and this question on stackoverflow, I just try to understand and convert it to FreeBSD for a personal usage.
Thank you.
I am trying to make a script that can show the data transfer rate just like what you got when running the command
systat -ifstat 1
I gather some information on the internet but the result my script shows has nothing to do with reality.
In order to simplified things for now I am only focus on the incoming traffic side, when I'll understand the logic and where my error is I'll do the rest after.
According to this site, the formula is:
Speed = amount of data / transfer time
_ the amount of data collected is obtained by using the
nestat
command, the result is in bytes which is converted to Megabytes to be more human readable._ transfer time is one second because it is simpler(divided by one is like doing nothing), so the while loop runs the operation every second.
In theory it looks like an easy thing to do, but still I am struggling, the results are far from okay.
My incoming internet speed is around 1.5MB/s the script shows something like 2749.
I am pretty bad at math so I probably missed an important point here, can you guys guide me on this?
I have no merit for the following script I've been largely inspired by this video on youtube and this question on stackoverflow, I just try to understand and convert it to FreeBSD for a personal usage.
Thank you.
Bash:
tmpfile=$(mktemp)
while true
do
# store previous result
bytes_previous=$(cat "$tmpfile")
# get current bytes value
bytes_current=$(netstat -4 -i -b -I igb0 | awk '{print $8}' | sed 1d)
# get difference in MegaBytes
bytes_diff=$(( (bytes_current - bytes_previous) / (1024*1024) ))
echo "$bytes_diff" | tee "$tmpfile"
# repeat the operation every second
sleep 1
done