Ethernet calculate in script
12/01/2015, 14:23 - Network
Ethernet calculate in script

bandwidth.sh
#!/bin/bash
#
# ${CALC} is package "ap${CALC}"
#
# Phy Speed
SPEED_LIST="10 100 1000 2500 10000"
# Transport Unit Size
MTU_LIST="46 1500 9000 16000"
# VLAN tag number
VLAN_TAG_LIST="0"
#
CALC=$(whereis calc | cut -d' ' -f2 )
#
Kb=1000
Mb=$( ${CALC} "1000 * ${Kb}" )
Gb=$( ${CALC} "1000 * ${Mb}" )
#
# 96bit time
ethInterFrameGap=96
# 7+1Byte
ethPreamble=8
# 6Byte
ethDestMAC=6
# 6Byte
ethSrcMAC=6
# 2Byte
ethType=2
# 4Byte
ethVLAN=4
# 46Byte
minMTU=46
# 4Byte
ethFCS=4
#
function frameNumFunc() {
local speed=$1 # First Argument as Physical SPEED
local mtu=$2 # Second Argument as MTU
local tag=$3 # Thrid Arhument as VLAN Tag number
# calculate in bit wide
local frameSizeNum=$( ${CALC} "${ethDestMAC}+${ethSrcMAC}+${ethType}+${mtu}+${
ethFCS}+(${tag}*${ethVLAN})" )
local frameNumRaw=$( echo $( ${CALC} "${speed}/(${ethInterFrameGap}+(${ethPrea
mble}+${frameSizeNum})*8)" ) | sed -e 's/^~//g' )
local frameNum=$( ${CALC} "int(${frameNumRaw})" )
echo ${frameNumRaw} ${frameNum} ${frameSizeNum} # Return Maximum Transmit Fram
e number
}
#
function efficiencyFunc () {
local speed=$1
local mtu=$2
local frameNumber=$3
#
local efficiencyUsage=$( ${CALC} "${mtu}*${frameNumber}*8" )
local efficiencyNum=$( ${CALC} "${efficiencyUsage}/${speed}" )
echo ${efficiencyNum} $( ${CALC} "${efficiencyUsage}/${Mb}" )
}
#
function bandwidthFunc () {
local speed=$1
local overheadRate=$2
#
bandwidthNum=$( ${CALC} "(${speed}*(1-${overheadRate}))/${Mb}" )
echo ${bandwidthNum}
}
#
function overheadFunc () {
local speed=$1
local frameNum=$2
#
local overheadRate=$( ${CALC} "(${ethInterFrameGap} * ${frameNum})/${speed}" )
echo ${overheadRate}
}
#
function calculateFunc () {
local speed=$( expr $1 + 0 )
local mtu=$( expr $2 + 0 )
local vlanTag=$( expr $3 + 0 )
#
local bandwidthNums=$( ${CALC} "${speed} * ${Mb}" )
#
local frameNumbers=$( frameNumFunc ${bandwidthNums} ${mtu} ${vlanTag} )
local frameNums=$( echo ${frameNumbers} | cut -d' ' -f2 )
local frameNumsRaw=$( echo ${frameNumbers} | cut -d' ' -f1 )
local frameSizeNums=$( echo ${frameNumbers} | cut -d' ' -f3 )
#
local frameOverheadNums=$( overheadFunc ${bandwidthNums} ${frameNums} )
#
local efficiencyNumbers=$( efficiencyFunc ${bandwidthNums} ${mtu} ${frameNums
} )
local effNums=$( echo ${efficiencyNumbers} | cut -d' ' -f1 )
local effBandwidthNums=$( echo ${efficiencyNumbers} | cut -d' ' -f2 )
local bandwidth=$( bandwidthFunc ${bandwidthNums} ${frameOverheadNums} )
#
echo ${frameNums} ${frameOverheadNums} ${effNums} ${effBandwidthNums} ${bandwi
dth} ${frameSizeNums}
}
#
printf "===============================================================\n"
#
for i in ${SPEED_LIST} ; do
echo -e "Bandwidth:\t\t\t\t\t${i} Mbit"
#
for j in ${MTU_LIST} ; do
for k in ${VLAN_TAG_LIST} ; do
result=$( calculateFunc $i $j $k )
#
frames=$( echo ${result} | cut -d' ' -f 1 )
frameSize=$( echo ${result} | cut -d' ' -f 6 )
overheadRate=$( echo ${result} | cut -d' ' -f 2 )
overheadRatePercent=$( ${CALC} "($overheadRate+0)*100" )
efficiencyRate=$( echo ${result} | cut -d' ' -f 3 )
efficiencyRatePercent=$( ${CALC} "($efficiencyRate+0)*100" )
efficiencyBandwidthSpeed=$( echo ${result} | cut -d' ' -f 4 )
bandwidthSpeed=$( echo ${result} | cut -d' ' -f 5 )
#
echo -e "---------------------------------------------------------------"
echo -e "\tMTU Size:\t\t\t\t${j} Byte"
#
if [ "${k}" -eq "0" ]; then
T="None"
else
T=${k}
fi
#
echo -e "\t\tVLAN Tag:\t\t\t${T} Tag"
echo -e "\t\tFrame Size:\t\t\t${frameSize} Byte"
echo -e "\t\tMax Frame per second:\t\t${frames}"
echo -e "\t\tOverhead Rate:\t\t${overheadRatePercent}%"
echo -e "\t\tEfficiency Rate:\t${efficiencyRatePercent}%"
#
if [ "$( ${CALC} "${i} * ${Mb}" )" -gt "${Gb}" ]; then
bandwidthSpeed=$( ${CALC} "${bandwidthSpeed}/1000" )
efficiencyBandwidthSpeed=$( ${CALC} "${efficiencyBandwidthSpeed}/1000" )
#
echo -e "\t\tTransport Bandwidth:\t${bandwidthSpeed} Gbit"
echo -e "\t\tEfficiency Bbandwidth.sh
#!/bin/bash
#
# ${CALC} is package "ap${CALC}"
#
# Phy Speed
SPEED_LIST="10 100 1000 2500 10000"
# Transport Unit Size
MTU_LIST="46 1500 9000 16000"
# VLAN tag number
VLAN_TAG_LIST="0"
#
CALC=$(whereis calc | cut -d' ' -f2 )
#
Kb=1000
Mb=$( ${CALC} "1000 * ${Kb}" )
Gb=$( ${CALC} "1000 * ${Mb}" )
#
# 96bit time
ethInterFrameGap=96
# 7+1Byte
ethPreamble=8
# 6Byte
ethDestMAC=6
# 6Byte
ethSrcMAC=6
# 2Byte
ethType=2
# 4Byte
ethVLAN=4
# 46Byte
minMTU=46
# 4Byte
ethFCS=4
#
function frameNumFunc() {
local speed=$1 # First Argument as Physical SPEED
local mtu=$2 # Second Argument as MTU
local tag=$3 # Thrid Arhument as VLAN Tag number
# calculate in bit wide
local frameSizeNum=$( ${CALC} "${ethDestMAC}+${ethSrcMAC}+${ethType}+${mtu}+${
ethFCS}+(${tag}*${ethVLAN})" )
local frameNumRaw=$( echo $( ${CALC} "${speed}/(${ethInterFrameGap}+(${ethPrea
mble}+${frameSizeNum})*8)" ) | sed -e 's/^~//g' )
local frameNum=$( ${CALC} "int(${frameNumRaw})" )
echo ${frameNumRaw} ${frameNum} ${frameSizeNum} # Return Maximum Transmit Fram
e number
}
#
function efficiencyFunc () {
local speed=$1
local mtu=$2
local frameNumber=$3
#
local efficiencyUsage=$( ${CALC} "${mtu}*${frameNumber}*8" )
local efficiencyNum=$( ${CALC} "${efficiencyUsage}/${speed}" )
echo ${efficiencyNum} $( ${CALC} "${efficiencyUsage}/${Mb}" )
}
#
function bandwidthFunc () {
local speed=$1
local overheadRate=$2
#
bandwidthNum=$( ${CALC} "(${speed}*(1-${overheadRate}))/${Mb}" )
echo ${bandwidthNum}
}
#
function overheadFunc () {
local speed=$1
local frameNum=$2
#
local overheadRate=$( ${CALC} "(${ethInterFrameGap} * ${frameNum})/${speed}" )
echo ${overheadRate}
}
#
function calculateFunc () {
local speed=$( expr $1 + 0 )
local mtu=$( expr $2 + 0 )
local vlanTag=$( expr $3 + 0 )
#
local bandwidthNums=$( ${CALC} "${speed} * ${Mb}" )
#
local frameNumbers=$( frameNumFunc ${bandwidthNums} ${mtu} ${vlanTag} )
local frameNums=$( echo ${frameNumbers} | cut -d' ' -f2 )
local frameNumsRaw=$( echo ${frameNumbers} | cut -d' ' -f1 )
local frameSizeNums=$( echo ${frameNumbers} | cut -d' ' -f3 )
#
local frameOverheadNums=$( overheadFunc ${bandwidthNums} ${frameNums} )
#
local efficiencyNumbers=$( efficiencyFunc ${bandwidthNums} ${mtu} ${frameNums
} )
local effNums=$( echo ${efficiencyNumbers} | cut -d' ' -f1 )
local effBandwidthNums=$( echo ${efficiencyNumbers} | cut -d' ' -f2 )
local bandwidth=$( bandwidthFunc ${bandwidthNums} ${frameOverheadNums} )
#
echo ${frameNums} ${frameOverheadNums} ${effNums} ${effBandwidthNums} ${bandwi
dth} ${frameSizeNums}
}
#
printf "===============================================================\n"
#
for i in ${SPEED_LIST} ; do
echo -e "Bandwidth:\t\t\t\t\t${i} Mbit"
#
for j in ${MTU_LIST} ; do
for k in ${VLAN_TAG_LIST} ; do
result=$( calculateFunc $i $j $k )
#
frames=$( echo ${result} | cut -d' ' -f 1 )
frameSize=$( echo ${result} | cut -d' ' -f 6 )
overheadRate=$( echo ${result} | cut -d' ' -f 2 )
overheadRatePercent=$( ${CALC} "($overheadRate+0)*100" )
efficiencyRate=$( echo ${result} | cut -d' ' -f 3 )
efficiencyRatePercent=$( ${CALC} "($efficiencyRate+0)*100" )
efficiencyBandwidthSpeed=$( echo ${result} | cut -d' ' -f 4 )
bandwidthSpeed=$( echo ${result} | cut -d' ' -f 5 )
#
echo -e "---------------------------------------------------------------"
echo -e "\tMTU Size:\t\t\t\t${j} Byte"
#
if [ "${k}" -eq "0" ]; then
T="None"
else
T=${k}
fi
#
echo -e "\t\tVLAN Tag:\t\t\t${T} Tag"
echo -e "\t\tFrame Size:\t\t\t${frameSize} Byte"
echo -e "\t\tMax Frame per second:\t\t${frames}"
echo -e "\t\tOverhead Rate:\t\t${overheadRatePercent}%"
echo -e "\t\tEfficiency Rate:\t${efficiencyRatePercent}%"
#
if [ "$( ${CALC} "${i} * ${Mb}" )" -gt "${Gb}" ]; then
bandwidthSpeed=$( ${CALC} "${bandwidthSpeed}/1000" )
efficiencyBandwidthSpeed=$( ${CALC} "${efficiencyBandwidthSpeed}/1000" )
#
echo -e "\t\tTransport Bandwidth:\t${bandwidthSpeed} Gbit"
echo -e "\t\tEfficiency Bandwidth:\t${efficiencyBandwidthSpeed} Gbit"
else
echo -e "\t\tTransport Bandwidth:\t\t${bandwidthSpeed} Mbit"
echo -e "\t\tEfficiency Bandwidth:\t\t${efficiencyBandwidthSpeed} Mbit"
fi
#
done
done
#
printf "===============================================================\n"
done
#
andwidth:\t${efficiencyBandwidthSpeed} Gbit"
else
echo -e "\t\tTransport Bandwidth:\t\t${bandwidthSpeed} Mbit"
echo -e "\t\tEfficiency Bandwidth:\t\t${efficiencyBandwidthSpeed} Mbit"
fi
#
done
done
#
printf "===============================================================\n"
done
#


Result:

===============================================================
Bandwidth: 10 Mbit
---------------------------------------------------------------
MTU Size: 46 Byte
VLAN Tag: None Tag
Frame Size: 64 Byte
Max Frame per second: 14880
Overhead Rate: 14.2848%
Efficiency Rate: 54.7584%
Transport Bandwidth: 8.57152 Mbit
Efficiency Bandwidth: 5.47584 Mbit
---------------------------------------------------------------
MTU Size: 1500 Byte
VLAN Tag: None Tag
Frame Size: 1518 Byte
Max Frame per second: 812
Overhead Rate: 0.77952%
Efficiency Rate: 97.44%
Transport Bandwidth: 9.922048 Mbit
Efficiency Bandwidth: 9.744 Mbit
---------------------------------------------------------------
MTU Size: 9000 Byte
VLAN Tag: None Tag
Frame Size: 9018 Byte
Max Frame per second: 138
Overhead Rate: 0.13248%
Efficiency Rate: 99.36%
Transport Bandwidth: 9.986752 Mbit
Efficiency Bandwidth: 9.936 Mbit
---------------------------------------------------------------
MTU Size: 16000 Byte
VLAN Tag: None Tag
Frame Size: 16018 Byte
Max Frame per second: 77
Overhead Rate: 0.07392%
Efficiency Rate: 98.56%
Transport Bandwidth: 9.992608 Mbit
Efficiency Bandwidth: 9.856 Mbit
===============================================================
Bandwidth: 100 Mbit
---------------------------------------------------------------
MTU Size: 46 Byte
VLAN Tag: None Tag
Frame Size: 64 Byte
Max Frame per second: 148809
Overhead Rate: 14.285664%
Efficiency Rate: 54.761712%
Transport Bandwidth: 85.714336 Mbit
Efficiency Bandwidth: 54.761712 Mbit
---------------------------------------------------------------
MTU Size: 1500 Byte
VLAN Tag: None Tag
Frame Size: 1518 Byte
Max Frame per second: 8127
Overhead Rate: 0.780192%
Efficiency Rate: 97.524%
Transport Bandwidth: 99.219808 Mbit
Efficiency Bandwidth: 97.524 Mbit
---------------------------------------------------------------
MTU Size: 9000 Byte
VLAN Tag: None Tag
Frame Size: 9018 Byte
Max Frame per second: 1383
Overhead Rate: 0.132768%
Efficiency Rate: 99.576%
Transport Bandwidth: 99.867232 Mbit
Efficiency Bandwidth: 99.576 Mbit
---------------------------------------------------------------
MTU Size: 16000 Byte
VLAN Tag: None Tag
Frame Size: 16018 Byte
Max Frame per second: 779
Overhead Rate: 0.074784%
Efficiency Rate: 99.712%
Transport Bandwidth: 99.925216 Mbit
Efficiency Bandwidth: 99.712 Mbit
===============================================================
Bandwidth: 1000 Mbit
---------------------------------------------------------------
MTU Size: 46 Byte
VLAN Tag: None Tag
Frame Size: 64 Byte
Max Frame per second: 1488095
Overhead Rate: 14.285712%
Efficiency Rate: 54.761896%
Transport Bandwidth: 857.14288 Mbit
Efficiency Bandwidth: 547.61896 Mbit
---------------------------------------------------------------
MTU Size: 1500 Byte
VLAN Tag: None Tag
Frame Size: 1518 Byte
Max Frame per second: 81274
Overhead Rate: 0.7802304%
Efficiency Rate: 97.5288%
Transport Bandwidth: 992.197696 Mbit
Efficiency Bandwidth: 975.288 Mbit
---------------------------------------------------------------
MTU Size: 9000 Byte
VLAN Tag: None Tag
Frame Size: 9018 Byte
Max Frame per second: 13830
Overhead Rate: 0.132768%
Efficiency Rate: 99.576%
Transport Bandwidth: 998.67232 Mbit
Efficiency Bandwidth: 995.76 Mbit
---------------------------------------------------------------
MTU Size: 16000 Byte
VLAN Tag: None Tag
Frame Size: 16018 Byte
Max Frame per second: 7793
Overhead Rate: 0.0748128%
Efficiency Rate: 99.7504%
Transport Bandwidth: 999.251872 Mbit
Efficiency Bandwidth: 997.504 Mbit
===============================================================
Bandwidth: 2500 Mbit
---------------------------------------------------------------
MTU Size: 46 Byte
VLAN Tag: None Tag
Frame Size: 64 Byte
Max Frame per second: 3720238
Overhead Rate: 14.28571392%
Efficiency Rate: 54.76190336%
Transport Bandwidth: 2.142857152 Gbit
Efficiency Bandwidth: 1.369047584 Gbit
---------------------------------------------------------------
MTU Size: 1500 Byte
VLAN Tag: None Tag
Frame Size: 1518 Byte
Max Frame per second: 203185
Overhead Rate: 0.7802304%
Efficiency Rate: 97.5288%
Transport Bandwidth: 2.48049424 Gbit
Efficiency Bandwidth: 2.43822 Gbit
---------------------------------------------------------------
MTU Size: 9000 Byte
VLAN Tag: None Tag
Frame Size: 9018 Byte
Max Frame per second: 34576
Overhead Rate: 0.13277184%
Efficiency Rate: 99.57888%
Transport Bandwidth: 2.496680704 Gbit
Efficiency Bandwidth: 2.489472 Gbit
---------------------------------------------------------------
MTU Size: 16000 Byte
VLAN Tag: None Tag
Frame Size: 16018 Byte
Max Frame per second: 19484
Overhead Rate: 0.07481856%
Efficiency Rate: 99.75808%
Transport Bandwidth: 2.498129536 Gbit
Efficiency Bandwidth: 2.493952 Gbit
===============================================================
Bandwidth: 10000 Mbit
---------------------------------------------------------------
MTU Size: 46 Byte
VLAN Tag: None Tag
Frame Size: 64 Byte
Max Frame per second: 14880952
Overhead Rate: 14.28571392%
Efficiency Rate: 54.76190336%
Transport Bandwidth: 8.571428608 Gbit
Efficiency Bandwidth: 5.476190336 Gbit
---------------------------------------------------------------
MTU Size: 1500 Byte
VLAN Tag: None Tag
Frame Size: 1518 Byte
Max Frame per second: 812743
Overhead Rate: 0.78023328%
Efficiency Rate: 97.52916%
Transport Bandwidth: 9.921976672 Gbit
Efficiency Bandwidth: 9.752916 Gbit
---------------------------------------------------------------
MTU Size: 9000 Byte
VLAN Tag: None Tag
Frame Size: 9018 Byte
Max Frame per second: 138304
Overhead Rate: 0.13277184%
Efficiency Rate: 99.57888%
Transport Bandwidth: 9.986722816 Gbit
Efficiency Bandwidth: 9.957888 Gbit
---------------------------------------------------------------
MTU Size: 16000 Byte
VLAN Tag: None Tag
Frame Size: 16018 Byte
Max Frame per second: 77939
Overhead Rate: 0.07482144%
Efficiency Rate: 99.76192%
Transport Bandwidth: 9.992517856 Gbit
Efficiency Bandwidth: 9.976192 Gbit
===============================================================


回應

發表回應

填寫下面來發表回應。









插入項目:


遊覽已上傳的圖片








回應需經過管理人員認可後才會出現在網頁上.