linux mtu 自动检测配置脚本(解决inode拨号上网问题)

来源:互联网 发布:淘宝少发了一件怎么办 编辑:程序博客网 时间:2024/05/17 23:34

  学校宿舍里用的是inode客户端拨号上网(linux下的inode客户端网上很多,随便找找都有),在体验linux下天马行空的快感时,经常会碰到一些问题。我经常碰到的便是,打开电脑拨上号后,却只能进谷歌、百度等极少数网站(事实上,除了这两个网站,别的一个都没能进去)。你能想像一台不能上网的电脑还能干什么吗!!!(除非你的F盘装满了dota视频)

  上网查了下,主要有两种解决方法:

  第一种:关掉电脑,拔掉电源,扣掉电池,完全放电5分钟左右。在最初,偶还不太懂的时候,就是采用的这种方法。这样的办法虽然省脑,但太费时。继续找.....

  第二种:修改网卡的MTU。 MTU的英文全称为maximum transmission unit。 linux默认的值为1500,但如果本机的MTU比网关的大,就会导致上述的网络问题。通常的做法是通过命令检测出最佳的MTU,再配置网卡:

ping -c 3 -w 1 -M do -s 1472 192.168.1.1

  如果没有丢包,说明这个值(1472)是可行的,然后再配置网卡。然后由下述命令配置网卡。

sudo ifconfig eth0 mtu 1500 

注意:   1)应跟据个人电脑情况,选择eth0/eth1或是其它,它代表的是当前电脑上工作的网卡;

    2)这里的mtu值是1500=1472+28 ,不要打错了;

    3)至于上述命令和参数,能看懂我接下来的脚本程序的人,肯定都懂。

  可是问题又来了。按上述第二种方法,需要手动一个一个的值来测试,仍然比较费体力。当然,你也可以一开始就设定的一个很小的值,比如1000,但这会影响到网速。所以,我想到写一个脚本程序,自动测试最大的且可行的mtu,并完成网卡的配置工作(没办法,人就这么懒!!!)。有了想法,并捣鼓了一晚上,终于完成了,现拿出来,给有需要的新手(因为大牛们会有更好的方法)。

  下面是我写的脚本的具体内容。下载地址: http://download.csdn.net/detail/sir_zy/7650053

  用法:我将它的写权限去掉了,防止被误该。如果是老手,想修改并完善它的功能也很容易。先将脚本下载到本地,然后看看是否有可执行的权限,如果没有,请自行加上。打开终端,进入脚本文件所在路程径,输入命令

sudo ./test-mtu.sh IP [max_mtu]

其中,IP一般为网关,或是同一局域网内某台电脑的IP地址。max_mtu表示测试的最大的MTU值。需要说明的是,max_mtu选项不是必须的,。如果未指定此选项,则默认最大测试值为1500。但IP地址一般是必须的(如果实在是忘了指定IP,则默认以192.168.1.1为测试IP)。另外,因为涉及到网卡的配置,所以此脚本的的执行需要root权限。下面贴出全部代码:

#!/bin/bash# program:#this script is used to test the largest and useable mtu.#first, try to find the best mtu#second, configure network card with the best mtu# usage:#sudo ./test-mtu.sh IP [max_mtu]#choose a proper IP of you net, default route is a good choice# note: this script needs the root autorization.when max_mtu is #      not assigned, then default max_mtu=1500 is used.   # history:#2014/7/17 first released# author && mail#author: sir_zy mial: sir_zy@foxmail.comnum_package=2max_mtu_def=1500test_ip_default=192.168.1.1flag=not# get the locall mtu of this computerlocall_mtu=$(ifconfig | grep "MTU:" | sed -e '2d' | \     sed -e 's/^.*MTU://g' | sed -e 's/ Metr.*$//g')# get the current network cardnet_work_card=$(ifconfig | grep "eth[0-9]" | cut -d " " -f 1)# generally, your locall mtu is 1500, but the value of mtu in the # ping command should be 1500-28. if no argument is assigned,# then default value will be used.case $# in2)test_mtu=$(($2-28))test_ip=$1;;1)test_mtu=$(($max_mtu_def-28))test_ip=$1;;0)test_mtu=$(($max_mtu_def-28))test_ip=$test_ip_default;;*)echo "warning!!! the usage of this script:"echo "****************************************"echo "*   sudo ./test-mtu.sh IP [max_mtu]    *"echo "****************************************"echo "for more details,please read the usage of this script!!!"exit 1;;esac# testing...# at first, change the mtu to the max_mtu/max_mtu_defifconfig $net_work_card mtu $((test_mtu+28))while [ "$flag" != "yes" ]doecho "test mtu: $((test_mtu+28)), testing ..."result_temp=$(ping -q -M do -w 1 -c $num_package -s $test_mtu $test_ip | \      grep "packet loss" | cut -d " " -f 6 | sed -e 's/\%//g')[ "$result_temp" == "0" ] && flag=yes \  && echo "locall mtu: $locall_mtu" \  && echo "the best mtu: $((test_mtu+28))"[ "$flag" != "yes" ] && test_mtu=$(($test_mtu-1))done# configure network card...if [ $locall_mtu != $(($test_mtu+28)) ];thenifconfig $net_work_card mtu $((test_mtu+28))else echo "the mtu is OK, no need to change."fi# display the info of your network card configurationifconfig $net_work_card

        接下来,简单地对上述程序做个介绍:

17:num_package 表示每次ping的时候发送两个数据包

18-19:分别指定默认最大测试mtu和默认测试IP

20:定义一个标志变量。no表示当前mtu过大,yes表示当前mtu是最佳值

23:获得本机当前的mtu

27:获得本机当前使用的网络设备的名称,一般对应你的网卡,比如eth0 、eth1等。

32-53:对执行脚本程序时输入的参数。分别处理仅指定IP,max_mtu ,仅指定IP, 两者都未指定以及其它情况。

57:先将网卡的MTU配置到最大测试值。假如最初本机的MTU为1000,而实际网关的MTU为1500,那么你在测试1000-1500之前的任何值都会报错。故先将其配置成最大测试值。

58-70:从最大测试值开始,做递减测试,直到找到最佳值。

73-77:比较当前主机的mtu和通过测试找到的最佳值。如果两者相同,则无需对网卡进行配置,否则以最佳值对网卡作配置。

80:显示修改后的网卡配置。

       最后,在输入参数的处理上,仍有许多问题。例如,如果把IP和max_mtu两个参数倒过来,则会出问题。或者输入的IP不是规范的IP值等。看到此博客后,如果有兴趣,请完善它并共享给广大新手们。

******************并重原创,如需转载,请注明来源*********************

0 0
原创粉丝点击