{"id":237,"date":"2016-09-21T04:50:26","date_gmt":"2016-09-20T23:20:26","guid":{"rendered":"http:\/\/www.bhuvankrishna.in\/blog\/?p=237"},"modified":"2016-09-21T04:50:26","modified_gmt":"2016-09-20T23:20:26","slug":"monitor-network-speed-shell-script","status":"publish","type":"post","link":"https:\/\/www.bhuvankrishna.in\/blog\/2016\/09\/21\/monitor-network-speed-shell-script\/","title":{"rendered":"monitor network speed shell script"},"content":{"rendered":"<p>No need to install any tool to monitor the network speed. Just run this script and it will show the correct speed. Feel free to modify the code.<\/p>\n<p><code><br \/>\n#!\/bin\/sh<\/p>\n<p>usage(){<br \/>\necho \"Usage: $0 [-i INTERFACE] [-s INTERVAL] [-c COUNT]\"<br \/>\necho<br \/>\necho \"-i INTERFACE\"<br \/>\necho \"    The interface to monitor, default is eth0.\"<br \/>\necho \"-s INTERVAL\"<br \/>\necho \"    The time to wait in seconds between measurements, default is 3 seconds.\"<br \/>\necho \"-c COUNT\"<br \/>\necho \"    The number of times to measure, default is 10 times.\"<br \/>\nexit 3<br \/>\n}<\/p>\n<p>readargs(){<br \/>\nwhile [ \"$#\" -gt 0 ] ; do<br \/>\n  case \"$1\" in<br \/>\n   -i)<br \/>\n    if [ \"$2\" ] ; then<br \/>\n     interface=\"$2\"<br \/>\n     shift ; shift<br \/>\n    else<br \/>\n     echo \"Missing a value for $1.\"<br \/>\n     echo<br \/>\n     shift<br \/>\n     usage<br \/>\n    fi<br \/>\n   ;;<br \/>\n   -s)<br \/>\n    if [ \"$2\" ] ; then<br \/>\n     sleep=\"$2\"<br \/>\n     shift ; shift<br \/>\n    else<br \/>\n     echo \"Missing a value for $1.\"<br \/>\n     echo<br \/>\n     shift<br \/>\n     usage<br \/>\n    fi<br \/>\n   ;;<br \/>\n   -c)<br \/>\n    if [ \"$2\" ] ; then<br \/>\n     counter=\"$2\"<br \/>\n     shift ; shift<br \/>\n    else<br \/>\n     echo \"Missing a value for $1.\"<br \/>\n     echo<br \/>\n     shift<br \/>\n     usage<br \/>\n    fi<br \/>\n   ;;<br \/>\n   *)<br \/>\n    echo \"Unknown option $1.\"<br \/>\n    echo<br \/>\n    shift<br \/>\n    usage<br \/>\n   ;;<br \/>\n  esac<br \/>\ndone<br \/>\n}<\/p>\n<p>checkargs(){<br \/>\nif [ ! \"$interface\" ] ; then<br \/>\n  interface=\"eth0\"<br \/>\nfi<br \/>\nif [ ! \"$sleep\" ] ; then<br \/>\n  sleep=\"3\"<br \/>\nfi<br \/>\nif [ ! \"$counter\" ] ; then<br \/>\n  counter=\"10\"<br \/>\nfi<br \/>\n}<\/p>\n<p>printrxbytes(){<br \/>\n\/sbin\/ifconfig \"$interface\" | grep \"RX bytes\" | cut -d: -f2 | awk '{ print $1 }'<br \/>\n}<\/p>\n<p>printtxbytes(){<br \/>\n\/sbin\/ifconfig \"$interface\" | grep \"TX bytes\" | cut -d: -f3 | awk '{ print $1 }'<br \/>\n}<\/p>\n<p>bytestohumanreadable(){<br \/>\nmultiplier=\"0\"<br \/>\nnumber=\"$1\"<br \/>\nwhile [ \"$number\" -ge 1024 ] ; do<br \/>\n  multiplier=$(($multiplier+1))<br \/>\n  number=$(($number\/1024))<br \/>\ndone<br \/>\ncase \"$multiplier\" in<br \/>\n  1)<br \/>\n   echo \"$number Kb\"<br \/>\n  ;;<br \/>\n  2)<br \/>\n   echo \"$number Mb\"<br \/>\n  ;;<br \/>\n  3)<br \/>\n   echo \"$number Gb\"<br \/>\n  ;;<br \/>\n  4)<br \/>\n   echo \"$number Tb\"<br \/>\n  ;;<br \/>\n  *)<br \/>\n   echo \"$1 b\"<br \/>\n  ;;<br \/>\nesac<br \/>\n}<\/p>\n<p>printresults(){<br \/>\nwhile [ \"$counter\" -ge 0 ] ; do<br \/>\n  counter=$(($counter - 1))<br \/>\n  if [ \"$rxbytes\" ] ; then<br \/>\n   oldrxbytes=\"$rxbytes\"<br \/>\n   oldtxbytes=\"$txbytes\"<br \/>\n  fi<br \/>\n  rxbytes=$(printrxbytes)<br \/>\n  txbytes=$(printtxbytes)<br \/>\n  if [ \"$oldrxbytes\" -a \"$rxbytes\" -a \"$oldtxbytes\" -a \"$txbytes\" ] ; then<br \/>\n   echo \"RXbytes = $(bytestohumanreadable $(($rxbytes - $oldrxbytes))) TXbytes = $(bytestohumanreadable $(($txbytes - $oldtxbytes)))\"<br \/>\n  else<br \/>\n   echo \"Monitoring $interface every $sleep seconds. (RXbyte total = $(bytestohumanreadable $rxbytes) TXbytes total = $(bytestohumanreadable $txbytes))\"<br \/>\n  fi<br \/>\n  sleep \"$sleep\"<br \/>\ndone<br \/>\n}<\/p>\n<p>readargs \"$@\"<br \/>\ncheckargs<br \/>\nprintresults<br \/>\n<\/code><\/p>\n<p>Thank you guys for the scripts.<br \/>\n<a href=\"http:\/\/meinit.nl\/shell-script-measure-network-throughput-linux-machines\">Original Post<\/a><br \/>\n<a href=\"http:\/\/xmodulo.com\/measure-packets-per-second-throughput-high-speed-network-interface.html\">Another Method<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>No need to install any tool to monitor the network speed. Just run this script and it will show the correct speed. Feel free to modify the code. #!\/bin\/sh usage(){ echo &#8220;Usage: $0 [-i INTERFACE] [-s INTERVAL] [-c COUNT]&#8221; echo echo &#8220;-i INTERFACE&#8221; echo &#8221; The interface to monitor, default is eth0.&#8221; echo &#8220;-s INTERVAL&#8221;\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.bhuvankrishna.in\/blog\/2016\/09\/21\/monitor-network-speed-shell-script\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-237","post","type-post","status-publish","format-standard","hentry","category-uncategorised"],"_links":{"self":[{"href":"https:\/\/www.bhuvankrishna.in\/blog\/wp-json\/wp\/v2\/posts\/237","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bhuvankrishna.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bhuvankrishna.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bhuvankrishna.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bhuvankrishna.in\/blog\/wp-json\/wp\/v2\/comments?post=237"}],"version-history":[{"count":1,"href":"https:\/\/www.bhuvankrishna.in\/blog\/wp-json\/wp\/v2\/posts\/237\/revisions"}],"predecessor-version":[{"id":238,"href":"https:\/\/www.bhuvankrishna.in\/blog\/wp-json\/wp\/v2\/posts\/237\/revisions\/238"}],"wp:attachment":[{"href":"https:\/\/www.bhuvankrishna.in\/blog\/wp-json\/wp\/v2\/media?parent=237"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bhuvankrishna.in\/blog\/wp-json\/wp\/v2\/categories?post=237"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bhuvankrishna.in\/blog\/wp-json\/wp\/v2\/tags?post=237"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}