{"id":645,"date":"2014-07-19T14:37:46","date_gmt":"2014-07-19T14:37:46","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=645"},"modified":"2014-07-19T14:37:46","modified_gmt":"2014-07-19T14:37:46","slug":"linux-shell%e5%ae%9e%e6%97%b6%e7%9b%91%e6%8e%a7%e7%bd%91%e5%8d%a1%e6%b5%81%e9%87%8f","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/645.html","title":{"rendered":"Linux Shell\u5b9e\u65f6\u76d1\u63a7\u7f51\u5361\u6d41\u91cf"},"content":{"rendered":"<p>\u4eca\u5929\u78b0\u5230\u4e00\u4e2a\u95ee\u9898\uff1a\u5982\u4f55\u7528Shell\u5b9e\u73b0\u5b9e\u65f6\u76d1\u63a7\u7f51\u5361\u6d41\u91cf\uff1f<\/p>\n<p>\u4e0d\u6e05\u695a\uff0c\u4e8e\u662f\u641c\u7d22\uff1a{<a href=\"http:\/\/cn.bing.com\/search?q=linux+real+time+network+speed+shell\" target=\"_blank\">http:\/\/cn.bing.com\/search?q=linux+real+time+network+speed+shell<\/a>}<\/p>\n<h5># 1.\u901a\u8fc7ifconfig\u547d\u4ee4<\/h5>\n<pre class=\"lang:default decode:true\">#!\/bin\/bash\nwhile : ; do\n      time=`date +%Y\"-\"%m\"-\"%d\" \"%k\":\"%M`\n      nic=`ifconfig |grep eth|awk '{print $1}'`\n      rx_before=`ifconfig $nic|sed -n \"8\"p|awk '{print $2}'|cut -c7-`\n      tx_before=`ifconfig $nic|sed -n \"8\"p|awk '{print $6}'|cut -c7-`\n      sleep 2\n      rx_after=`ifconfig $nic|sed -n \"8\"p|awk '{print $2}'|cut -c7-`\n      tx_after=`ifconfig $nic|sed -n \"8\"p|awk '{print $6}'|cut -c7-`\n      rx_result=$[(rx_after-rx_before)\/256]\n      tx_result=$[(tx_after-tx_before)\/256]\n      echo \"$time In_Speed: \"$rx_result\"kbps Out_Speed: \"$tx_result\"kbps\"\n      sleep 2\ndone<\/pre>\n<h5># 2.\u901a\u8fc7\u8bfb\u53d6&#8221;\/proc\/net\/dev&#8221;\u6587\u4ef6<\/h5>\n<pre class=\"lang:default decode:true\">awk 'BEGIN{\nOFMT=\"%.3f\";\ndevf=\"\/proc\/net\/dev\";\nwhile((\"cat \"devf) | getline)\n{\n    if($0 ~ \/:\/ &amp;&amp; ($10+0) &gt; 0)\n    {\n        split($1,tarr,\":\");\n        net[tarr[1]]=$10+tarr[2];\n        print tarr[1],$10+tarr[2];\n    }\n}\nclose(devf);\nwhile((system(\"sleep 1 \")) &gt;=0)\n{\n    system(\"clear\");\n    while( getline &lt; devf )\n    {\n        if($0 ~ \/:\/ &amp;&amp; ($10+0) &gt; 0)\n            {\n                split($1,tarr,\":\");\n                if(tarr[1] in net)\n                {\n                    print tarr[1],\":\",($10+tarr[2]-net[tarr[1]])*8\/1024,\"kb\/s\";\n                    net[tarr[1]]=$10+tarr[2];\n                }\n            }\n    }\n    close(devf);\n}\n}'<\/pre>\n<h5># 3.\u901a\u8fc7\u8bfb\u53d6&#8221;\/sys\/class\/net\/eth0\/statistics\/rx_bytes&#8221;\u7b49\u6587\u4ef6\u5185\u5bb9{\u5728\/sys\/class\/net\/eth0\/statistics\/\u76ee\u5f55\u4e0b\u8fd8\u6709\u66f4\u591a\u7684\u7edf\u8ba1\u4fe1\u606f}<\/h5>\n<pre class=\"lang:default decode:true  \">#!\/bin\/bash\nif [ -z \"$1\" ]; then\n        echo\n        echo Usage:\t.\/$0 network-interface\n        echo\n        echo Example:\t.\/$0 eth0\n        echo\n        exit\nfi\nIF=$1\nwhile true\ndo\n        R1=`cat \/sys\/class\/net\/$1\/statistics\/rx_bytes`\n        T1=`cat \/sys\/class\/net\/$1\/statistics\/tx_bytes`\n        sleep 1\n        R2=`cat \/sys\/class\/net\/$1\/statistics\/rx_bytes`\n        T2=`cat \/sys\/class\/net\/$1\/statistics\/tx_bytes`\n        TBPS=`expr $T2 - $T1`\n        RBPS=`expr $R2 - $R1`\n        TKBPS=`expr $TBPS \/ 1024`\n        RKBPS=`expr $RBPS \/ 1024`\n        echo \"tx $1: $TKBPS kb\/snrx $1: $RKBPS kb\/s\"\ndone<\/pre>\n<h5># 4.\u901a\u8fc7\u7b2c\u4e09\u65b9\u5de5\u5177&#8221;nload\/ifstat\/iftop&#8221;<\/h5>\n<p>&nbsp;<\/p>\n<h6>\u53c2\u8003\u7f51\u5740\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/www.cnblogs.com\/chengmo\/archive\/2010\/10\/09\/1846826.html\" target=\"_blank\">shell awk\u5b9e\u73b0\u5b9e\u65f6\u76d1\u63a7\u7f51\u5361\u6d41\u91cf\u811a\u672c(\u5e38\u89c1\u5e94\u7528\u4e8c\uff09 &#8211; \u7a0b\u9ed8<\/a><\/li>\n<li><a href=\"http:\/\/www.cyberciti.biz\/faq\/check-network-connection-linux\/\" target=\"_blank\">Linux: Check Network Connection Command<\/a><\/li>\n<li><a href=\"http:\/\/www.cyberciti.biz\/networking\/nload-linux-command-to-monitor-network-traffic-bandwidth-usage\/\" target=\"_blank\">http:\/\/www.cyberciti.biz\/networking\/nload-linux-command-to-monitor-network-traffic-bandwidth-usage\/<\/a><\/li>\n<li><a href=\"http:\/\/www.apinglai.com\/linux-%e7%9b%91%e6%8e%a7%e7%bd%91%e5%8d%a1%e5%ae%9e%e6%97%b6%e6%b5%81%e9%87%8f%e5%b8%a6%e5%ae%bd-%e8%84%9a%e6%9c%ac\/\" target=\"_blank\">http:\/\/www.apinglai.com\/linux-%e7%9b%91%e6%8e%a7%e7%bd%91%e5%8d%a1%e5%ae%9e%e6%97%b6%e6%b5%81%e9%87%8f%e5%b8%a6%e5%ae%bd-%e8%84%9a%e6%9c%ac\/<\/a><\/li>\n<li><a href=\"http:\/\/www.cnblogs.com\/ggjucheng\/archive\/2013\/01\/13\/2858923.html\" target=\"_blank\">Linux\u7f51\u7edc\u6d41\u91cf\u5b9e\u65f6\u76d1\u63a7ifstat iftop\u547d\u4ee4\u8be6\u89e3 &#8211; ggjucheng<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u5929\u78b0\u5230\u4e00\u4e2a\u95ee\u9898\uff1a\u5982\u4f55\u7528Shell\u5b9e\u73b0\u5b9e\u65f6\u76d1\u63a7\u7f51\u5361\u6d41\u91cf\uff1f \u4e0d\u6e05\u695a\uff0c\u4e8e\u662f\u641c\u7d22\uff1a{http:\/\/cn.bing.c [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,6,12],"tags":[246,247],"class_list":["post-645","post","type-post","status-publish","format-standard","hentry","category-linux","category-other","category-tools","tag-eth0","tag-ifconfig"],"views":3845,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/645","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=645"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/645\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=645"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=645"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=645"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}