Blog

  • 测试所在网络MTU值的合理性

    个人电脑修改MTU无任何意义

    1、本地MTU值大于网络MTU值时,本地传输的数据包过大导致网络会拆包后传输,不但产生额外的数据包,而且消耗了“拆包、组包”的时间。

    2、本地MTU值小于网络MTU值时,本地传输的数据包可以直接传输,但是未能完全利用网络给予的数据包传输尺寸的上限值,传输能力未完全发挥。

    这样我们就知道,所谓合理的设置MTU值,就是让本地的MTU值与网络的MTU值一致,既能完整发挥传输性能,又不让数据包拆分。

    接下来最重要的就是要找出对于你的网络环境来说MTU多少才是合理的。

    方法如下:

    1、按Win+R组合键,调出“运行”菜单,输入“cmd”然后回车

    2、在出现的“命令提示符”窗口中输入“ping -l 1472 -f www.baidu.com”然后回车

    含义:

    ping:发起一个探测请求;

    -l(L的小写):限制探测包大小;

    1472:包大小为1472字节;

    -f:禁止路由器拆分数据包

    www.baidu.com:设百度为探测目标

    (你问我为什么不用Google做目标?考虑到Google时不时被墙,还是算了吧。。)

    3、这时有2种情况:

    (1)、如果收到了回复,那么说明你的网络允许最大MTU值就是1500字节,与系统默认值相同,只需要将路由器的MTU值也设置为1500即可;

    (2)、如果出现需要拆分数据包但是设置 DF。或是Packer needs to be fragmented but DF set.的提示,那就说明数据包大小超过了网络限定的MTU大小。需要减小探测包大小再次尝试。(为了截效果图,我将探测包改为1473了)

    4、按“上箭头”恢复刚才输入的命令,然后以5为跨度减小包大小为1467字节,再次回车探测。

    5、这时同样也有两种可能:

    (1)、如果有返回,说明数据包小于MTU限制,就将包大小+3再次探测,如果+3之后没有返回,那就以1为跨度降低数据包大小进行探测。

    (2)、如果还是没有返回,就继续以5为跨度减小包大小,直至有返回后进行5(1)中的操作。

    6、直至你发现数据包-1后,有了返回,就说明你探测到了MTU允许的准确数据包大小。(例如从1465降低到1464就有了返回,那么允许的数据包大小就是1464)

    7、不过上面得到的值还不能设置为操作系统或路由器的MTU,你找到的数据包大小需要加上28字节的“数据包报头”,才是完整的数据包尺寸。

    (例如:探测到的数据包大小是1464,那么加上28字节,最终MTU=1492字节)

    8、最后,只需要将路由器和操作系统中的MTU值设置为你得出的结果即可。

    路由器设置方法见路由器说明书!建议使用“Windows优化大师、超级兔子魔法设置、鲁大师”等软件修改操作系统的MTU。

    以下是较复杂的方法:

    (1)、XP操作系统设置方法:

    1、 按Win+R组合键,调出“运行”菜单,输入regedit,然后回车;

    2、 选择“HKEY_Local_Machine>SYSTEM>CurrentControlSet>Services>Tcpip>Parameters>interface”;

    3、在 interface 中下可能有很多项,需要逐个观察键值,会有一个项与你的网卡IP一致,选中该项;

    4、然后在该项上点击右键,选择“编辑>新建>DWORD值”,然后在右侧将其命名为“MTU”;

    5、右键点击MTU,选择“修改”,在弹出的窗口中选择“十进制”,填入你得出的合理MTU值即可。

    (2)、Vista、Win7操作系统设置方法:

    1、进入系统盘:\Windows\System32\找到cmd.exe,右键“以管理员身份运行”;

    2、在出现的“命令提示符”窗口中输入“netsh interface ipv4 show subinterfaces”并回车来查看当前的MTU值

    3、接下来输入“netsh interface ipv4 set subinterface “需修改的连接名” mtu=你得出的合理值 store=persistent”并回车即可

    例如:“netsh interface ipv4 set subinterface “本地连接” mtu=1492 store=persistent”

  • ubuntu11.10上安装rabbitvcs

    现在在ubuntu下有用svn的需求,那么google了以后

    发现rabbitvcs,于是在源里面搜了一下

    root@xx# apt-cache search rabbitvcs
    rabbitvcs-cli – Command line interface for RabbitVCS
    rabbitvcs-core – Easy version control
    rabbitvcs-gedit – Gedit extension for RabbitVCS
    rabbitvcs-nautilus – Nautilus extension for RabbitVCS

    下面开始安装

    apt-get install rabbitvcs-cli rabbitvcs-core rabbitvcs-gedit rabbitvcs-nautilus

    Reading package lists… Done
    Building dependency tree
    Reading state information… Done
    将会安装下列额外的软件包:
    gir1.2-nautilus-3.0 global ipython libapr1 libaprutil1 libsvn1 meld python-configobj python-foolscap python-nautilus python-svn python-wxgtk2.8
    python-wxversion subversion
    建议安装的软件包:
    doxygen apache httpd id-utils python-matplotlib python-numpy python-qt3 python-qt4 python-svn-dbg wx2.8-doc wx2.8-examples python-wxtools ruby wish
    tk8.5 tcsh csh octave3.0 mksh pdksh python-xml editra subversion-tools db4.8-util
    下列【新】软件包将被安装:
    gir1.2-nautilus-3.0 global ipython libapr1 libaprutil1 libsvn1 meld python-configobj python-foolscap python-nautilus python-svn python-wxgtk2.8
    python-wxversion rabbitvcs-cli rabbitvcs-core rabbitvcs-gedit rabbitvcs-nautilus subversion
    升级了 0 个软件包,新安装了 18 个软件包,要卸载 0 个软件包,有 23 个软件包未被升级。
    需要下载 19.9 MB 的软件包。
    解压缩后会消耗掉 79.4 MB 的额外空间。

    在教学区网速很快,一会儿就安好了

    下载 19.9 MB,耗时 52s (375 kB/s)

    然后就是

     

  • shell下12个for循环例子

    12个例子写的很全,转自

    12 Bash For Loop Examples for Your Linux Shell Scripting

    基本格式1,用for in:

    for varname in list
    do
     command1
     command2
     ..
    done

    基本格式2,c语言形式:

    for (( expr1; expr2; expr3 ))
    do
    command1
    command2 ..
    done

    1. Static values for the list after “in” keyword直接在”in”后面接值

    In the following example, the list of values (Mon, Tue, Wed, Thu and Fri) are directly given after the keyword “in” in the bash for loop.在下面的例子中,变量为day,在Mon,Tue,Wed,Thu和Fri中取值。

    $ cat for1.sh
    i=1
    for day in Mon Tue Wed Thu Fri
    do
     echo "Weekday $((i++)) : $day"
    done
    
    $ ./for1.sh
    Weekday 1 : Mon
    Weekday 2 : Tue
    Weekday 3 : Wed
    Weekday 4 : Thu
    Weekday 5 : Fri

    Caution: The list of values should not be separated by comma (Mon, Tue, Wed, Thu, Fri). The comma will be treated as part of the value. i.e Instead of “Mon”, it will use “Mon,” as value as shown in the example below.注意点:五个不同的值不能用逗号分隔,例如Mon,因为会自动把逗号算入到Mon之内。测试如下:

    $ cat for1-wrong1.sh
    i=1
    for day in Mon, Tue, Wed, Thu, Fri
    do
     echo "Weekday $((i++)) : $day"
    done
    
    $ ./for1-wrong1.sh
    Weekday 1 : Mon,
    Weekday 2 : Tue,
    Weekday 3 : Wed,
    Weekday 4 : Thu,
    Weekday 5 : Fri

    Caution: The list of values should not be enclosed in a double quote. (“Mon Tue Wed Thu Fri”). If you enclose in double quote, it will be treated as a single value (instead of 5 different values), as shown in the example below.注意:五个值不可以用双引号括起来,这会被认为只有一个值

    $ cat for1-wrong2.sh
    i=1
    for day in "Mon Tue Wed Thu Fri"
    do
     echo "Weekday $((i++)) : $day"
    done
    
    $ ./for1-wrong2.sh
    Weekday 1 : Mon Tue Wed Thu Fri

    2. Variable for the list after “in” keyword在”in”后面接变量

    Instead of providing the values directly in the for loop, you can store the values in a variable, and use the variable in the for loop after the “in” keyword, as shown in the following example.在使用前把值赋给变量,在”in”后面接变量。

    $ cat for2.sh
    i=1
    weekdays="Mon Tue Wed Thu Fri"
    for day in $weekdays
    do
     echo "Weekday $((i++)) : $day"
    done
    
    $ ./for2.sh
    Weekday 1 : Mon
    Weekday 2 : Tue
    Weekday 3 : Wed
    Weekday 4 : Thu
    Weekday 5 : Fri

    Caution: As a best practice, you should always quote the bash variables when you are referring it. There are few exceptions to this best practice rule. This is one of them. If you double quote the variable in this for loop, the list of values will be treated as single value. Lot of people fall into this trap. Be careful and do not double quote your variable in the for loop.注意:作为一个良好的习惯,请每次都将值用变量来代替。这里的变量不能用双引号括起来,它会将其视为一个值。测试如下:

    $ cat for2-wrong.sh
    i=1
    weekdays="Mon Tue Wed Thu Fri"
    for day in "$weekdays"
    do
     echo "Weekday $((i++)) : $day"
    done
    
    $ ./for2-wrong.sh
    Weekday 1 : Mon Tue Wed Thu Fri

    3. Don’t specify the list; get it from the positional parameters不指定列表

    If you don’t specify the keyword “in” followed by any list of values in the bash for loop, it will use the positional parameters (i.e the arguments that are passed to the shell script).如果in中不加变量,则在执行时加上即可,例如:

    $ cat for3.sh
    i=1
    for day
    do
     echo "Weekday $((i++)) : $day"
    done
    
    $ ./for3.sh Mon Tue Wed Thu Fri
    Weekday 1 : Mon
    Weekday 2 : Tue
    Weekday 3 : Wed
    Weekday 4 : Thu
    Weekday 5 : Fri

    Caution: Please be careful if you use this method. You should not include the keyword “in” in the for loop. If you leave the keyword “in” without any values, it will not use the positional parameter as shown below. It will not go inside the loop. i.e for loop will never get executed as shown in the example below.使用这种方法一定要小心,保证 in 这个字符都不出现,下面的例子就是没有输出任何信息。

    $ cat for3-wrong.sh
    i=1
    for day in
    do
     echo "Weekday $((i++)) : $day"
    done
    
    $ ./for3-wrong.sh Mon Tue Wed Thu Fri

    Note: Refer to our earlier article to understand more about bash positional parameters.可以查看bash位置参数。

    4. Unix command output as list values after “in” keyword以命令的输出做为列表

    You can use the output of any UNIX / Linux command as list of values to the for loop by enclosing the command in back-ticks ` ` as shown below.使用反引号将要执行的命令引起来,可使结果作为列表。

    $ cat for4.sh
    i=1
    for username in `awk -F: '{print $1}' /etc/passwd`
    do
     echo "Username $((i++)) : $username"
    done
    
    $ ./for4.sh
    Username 1 : ramesh
    Username 2 : john
    Username 3 : preeti
    Username 4 : jason
    ..

    5. Loop through files and directories in a for loop使用当前目录或文件做列表

    To loop through files and directories under a specific directory, just cd to that directory, and give * in the for loop as shown below.定位到指定目录下,用*代替列表。

    The following example will loop through all the files and directories under your home directory.下面的例子使用了/home目录。

    $ cat for5.sh
    i=1
    cd ~
    for item in *
    do
     echo "Item $((i++)) : $item"
    done
    
    $ ./for5.sh
    Item 1 : positional-parameters.sh
    Item 2 : backup.sh
    Item 3 : emp-report.awk
    Item 4 : item-list.sed
    Item 5 : employee.db
    Item 8 : storage
    Item 9 : downloads

    Usage of * in the bash for loop is similar to the file globbing that we use in the linux command line when we use ls command (and other commands).上面的例子显示类似于ls的执行结果。

    For example, the following will display all the files and directories under your home directory. This is the concept that is used in the above for5.sh example.

    cd ~
    ls *

    The following will display all the *.conf file that begins with either a, b, or, c or d under /etc directory.下面显示以a,b,c或d开头,以.conf结尾的文件

    $ ls -1 /etc/[abcd]*.conf
    /etc/asound.conf
    /etc/autofs_ldap_auth.conf
    /etc/cas.conf
    /etc/cgconfig.conf
    /etc/cgrules.conf
    /etc/dracut.conf

    The same argument that is used in the ls command above, can be used in a bash for loop, as shown in the example below.上面的可直接作为列表

    $ cat for5-1.sh
    i=1
    for file in /etc/[abcd]*.conf
    do
     echo "File $((i++)) : $file"
    done
    
    $ ./for5-1.sh
    File 1 : /etc/asound.conf
    File 2 : /etc/autofs_ldap_auth.conf
    File 3 : /etc/cas.conf
    File 4 : /etc/cgconfig.conf
    File 5 : /etc/cgrules.conf
    File 6 : /etc/dracut.conf

    6. Break out of the for loop(for循环中的break)

    You can break out of a for loop using ‘break’ command as shown below.可以使用如下的方式,中断for循环。

    $ cat for6.sh
    i=1
    for day in Mon Tue Wed Thu Fri
    do
     echo "Weekday $((i++)) : $day"
     if [ $i -eq 3 ]; then
       break;
     fi
    done
    
    $ ./for6.sh
    Weekday 1 : Mon
    Weekday 2 : Tue

    7. Continue from the top of the for loop(for循环中的continue)

    Under certain conditions, you can ignore the rest of the commands in the for loop, and continue the loop from the top again (for the next value in the list), using the continue command as shown below.在有些情况下,可以忽略某些之后再回到循环中,使用continue即可做到。

    The following example adds “(WEEKEND)” to Sat and Sun, and “(weekday)” to rest of the days.当循环遇到i=7 或i=8时,显示WEEKEND,然后继续。

    $ cat for7.sh
    i=1
    for day in Mon Tue Wed Thu Fri Sat Sun
    do
     echo -n "Day $((i++)) : $day"
     if [ $i -eq 7 -o $i -eq 8 ]; then
       echo " (WEEKEND)"
       continue;
     fi
     echo " (weekday)"
    done
    
    $ ./for7.sh
    Day 1 : Mon (weekday)
    Day 2 : Tue (weekday)
    Day 3 : Wed (weekday)
    Day 4 : Thu (weekday)
    Day 5 : Fri (weekday)
    Day 6 : Sat (WEEKEND)
    Day 7 : Sun (WEEKEND)

    8. Bash for loop using C program syntax使用C语言for循环语法

    This example uses the 2nd method of bash for loop, which is similar to the C for loop syntax. The following example generates 5 random number using the bash C-style for loop.在文章开头有使用c语言的for循环,下面的例子生成出1-5个数字

    $ cat for8.sh
    for (( i=1; i <= 5; i++ ))
    do
     echo "Random number $i: $RANDOM"
    done
    
    $ ./for8.sh
    Random number 1: 23320
    Random number 2: 5070
    Random number 3: 15202
    Random number 4: 23861
    Random number 5: 23435

    9. Infinite Bash for loop无限循环

    When you don’t provide the start, condition, and increment in the bash C-style for loop, it will become infinite loop. You need to press Ctrl-C to stop the loop.当不设定起始,条件和步长的时候,会变为无限循环,只能通过Ctrl-C来停止。

    $ cat for9.sh
    i=1;
    for (( ; ; ))
    do
       sleep $i
       echo "Number: $((i++))"
    done

    Like we said above, press Ctrl-C to break out of this bash infinite for loop example.

    $ ./for9.sh
    Number: 1
    Number: 2
    Number: 3

    10. Using comma in the bash C-style for loop在c语言类似的循环中使用逗号

    In the bash c-style loop, apart from increment the value that is used in the condition, you can also increment some other value as shown below.在c语言类似的循环中,可以将增长和变量分开。

    In the initialize section, and the increment section of the bash C-style for loop, you can have multiple value by separating with comma as shown below.

    The following for loop is executed a total of 5 times, using the variable i. However the variable j start with 5, and getting increment by 5 every time the loop gets executed.下面的例子将执行5次,但是目标是改变j的值,每一次加5

    $ cat for10.sh
    for ((i=1, j=10; i <= 5 ; i++, j=j+5))
    do
     echo "Number $i: $j"
    done
    
    $ ./for10.sh
    Number 1: 10
    Number 2: 15
    Number 3: 20
    Number 4: 25
    Number 5: 30

    11. Range of numbers after “in” keyword在in列表中使用数字区间

    You can loop through using range of numbers in the for loop “in” using brace expansion.

    The following example loops through 10 times using the values 1 through 10.下面的例子取值1到10,用大括号括起来。

    $ cat for11.sh
    for num in {1..10}
    do
     echo "Number: $num"
    done
    
    $ ./for11.sh
    Number: 1
    Number: 2
    Number: 3
    Number: 4
    Number: 5
    ...

    12. Range of numbers with increments after “in” keyword在in列表中使用数字区间,并以2做为步长。

    The following example loops through 5 times using the values 1 through 10, with an increment of 2. i.e It starts with 1, and keeps incrementing by 2, until it reaches 10.

    $ cat for12.sh
    for num in {1..10..2}
    do
     echo "Number: $num"
    done
    
    $ ./for12.sh
    Number: 1
    Number: 3
    Number: 5
    Number: 7
    Number: 9

    12个for循环的例子很基础很规范,多看优质代码才能更快成长。

  • ubuntu升级发行版本的两个方法

    1.使用菜单中的update-manager,或者 Alt+F2输入

    sudo update-manager -d

    2.完全使用命令

    sudo apt-get update

    安装update-manager-core软件包:

    apt-get install update-manager-core

    查看/etc/update-manager/release-upgrades文件:

    cat /etc/update-manager/release-upgrades

    确保里面包含了Prompt=normal

    然后运行如下命令启动系统升级:

    do-release-upgrade
  • pptpsetup命令行连接pptp vpn | 拨号

    一般会自带pptpsetup这个工具

    没有的话

    apt-get install pptp-linux

    添加一个vpn连接,执行

    pptpsetup -create vpnname -server xxx.xxx.xxx.xxx -username username -password passwd -encrypt (记得改一下)

    PS:更多选项运行”pptpsetup –help”

    这样其实是写入了/etc/ppp/peers/ 这个目录下面(网上有在这个目录新建文件的方法,看来不如这个方便)

    /etc/ppp/chap-secrets 用户名密码写入到了这个文件

    上线:

    pon vpnname

    下线:

    poff vpnname (全部下线poff -a)

    然后ifconfig查看一下多了ppp0这个网络

    现在还不能通过vpn来走

    因为并不知道数据该怎么走,很明显要配一下路由

    #route 查看现在的默认路由,下面两条操作将默认路由改为vpn的地址,删除原来的默认路由

    route add default gw 10.19.1.1
    route delete default gw xxx.xxx.xxx.yyy

    或者这样更简便

    route del default
    route add default dev ppp0

    这样就能通过vpn来做事了。

     

  • debian|linux 路由

    路由是什么

    路由routing)就是通过互联的网络信息从源地址传输到目的地址的活动。

    http://zh.wikipedia.org/wiki/%E8%B7%AF%E7%94%B1

    linux显示路由表

    #route 或

    # netstat -r

    内核 IP 路由表
    目标            网关            子网掩码        标志  跃点   引用  使用 接口
    into.dyndns.org *              255.255.255.255 UH    0      0        0 eth0
    into.dyndns.org *              255.255.255.255 UH    0      0        0 eth0
    10.19.1.1       *              255.255.255.255 UH    0      0        0 ppp0
    219.230.xxx.xxx *              255.255.255.192 U     1      0        0 eth0
    link-local      *              255.255.0.0     U     1000   0        0 eth0
    default         *              0.0.0.0         U     0      0        0 ppp0

    # route -n                #按照越精确越靠前。这个是路由的排序规则

    默认路由

    ip route add default dev eth0 via 192.168.0.254          #添加默认路由
    ip route del default dev eth0 via 192.168.0.254           #删除默认路由

    主机路由

    ip route add dev eth0 10.0.0.1/32                                 #添加主机路由
    ip route del dev eth0 10.0.0.1/32                                  #删除主机路由

    网段路由

    ip route add dev eth0 192.168.0.0/16                           #添加网段路由
    ip route del dev eth0 192.168.0.0/16                            #删除网段路由

    route add -net 目标网段 netmask 掩码 dev eth0

    route del -net 目标网段 netmask 掩码 dev eth0