Category: Linux

  • shell下通过ping判断网络是否断开

     

    #!/bin/bash
    #for use:
    #author: [email protected]
    #modified by xx at date
    #version:
    #history:
    #relate on:
    PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
    export PATH
    netstat=$(ping -c3 g.cn|grep transmitted |awk '{print $4}')
    if((netstat==0))
    #time out
    then
            date >> network.down.log
    else
    #ok
            echo "network is ok"
    fi

     

     

    ping 命令学习

    ping 默认是每秒钟一次,如果需要更改时间,可以 -i interval, 只有 root 可以写小于 0.2 秒的时间。

    ping -i 5 223.5.5.5
    ping -i 0.1 223.5.5.5

    ping 本地回环地址有三种方式

    ping 0
    ping localhost
    ping 127.0.0.1

    ping 具体次数结束,如 100 次

    ping -c 100 jpuyy.com

    使用 flood 方式 ping

    ping -f 0

    只显示 ping 的结果

    ping -c 5 -q 127.0.0.1

    设置发送的 ping 包大小 -s 单位是 bytes, ping 自带的 header 头大小 28 bytes

    ping -s 1000 jpuyy.com
  • svn log 不会显示刚刚提交的内容

     

    If you make a commit and immediately type svn log with no arguments, you may notice that your most recent commit doesn’t show up in the list of log messages. This is due to a combination of the behavior of svn commit and the default behavior of svn log. First, when you commit changes to the repository, svn bumps only the revision of files (and directories) that it commits, so usually the parent directory remains at the older revision (See 第 3.5.1 节 “更新和提交是分开的” for an explanation of why). svn log then defaults to fetching the history of the directory at its current revision, and thus you don’t see the newly committed changes. The solution here is to either update your working copy or explicitly provide a revision number to svn log by using the –revision (-r) option.

    如果刚刚提交就用svn log命令,则不会显示刚刚提交的内容。因为svn commit操作与svn log操作之间的联系,当提交的时候,只会改变当前文件或目录的版本号,svn log会抓取当前版本,而不会管新提交的。可以先svn update 再查看或用类似svn log -r 8 -v来查看。

  • ubuntu 官方源 | sources.list

    这里只记录官方源,就是以 .ubuntu.com 形式的源,其他源就不用了,还是官方的靠谱

    2012年3月追加11.10官方源

    deb http://security.ubuntu.com/ubuntu oneiric-security main restricted
    deb-src http://security.ubuntu.com/ubuntu oneiric-security main restricted
    deb http://security.ubuntu.com/ubuntu oneiric-security universe
    deb-src http://security.ubuntu.com/ubuntu oneiric-security universe
    deb http://security.ubuntu.com/ubuntu oneiric-security multiverse
    deb-src http://security.ubuntu.com/ubuntu oneiric-security multiverse

     

    2012年3月4日追加12.04官方源

    deb http://archive.ubuntu.com/ubuntu/ precise main restricted universe multiverse
    deb http://archive.ubuntu.com/ubuntu/ precise-security main restricted universe multiverse
    deb http://archive.ubuntu.com/ubuntu/ precise-updates main restricted universe multiverse
    deb http://archive.ubuntu.com/ubuntu/ precise-proposed main restricted universe multiverse
    deb http://archive.ubuntu.com/ubuntu/ precise-backports main restricted universe multiverse
    deb-src http://archive.ubuntu.com/ubuntu/ precise main restricted universe multiverse
    deb-src http://archive.ubuntu.com/ubuntu/ precise-security main restricted universe multiverse
    deb-src http://archive.ubuntu.com/ubuntu/ precise-updates main restricted universe multiverse
    deb-src http://archive.ubuntu.com/ubuntu/ precise-proposed main restricted universe multiverse
    deb-src http://archive.ubuntu.com/ubuntu/ precise-backports main restricted universe multiverse

    如提示:
    W: GPG 错误:http://archive.ubuntu.com precise Release: 由于没有公钥,无法验证下列签名: NO_PUBKEY 40976EAF437D05B5
    则执行:sudo apt-key adv –keyserver keyserver.ubuntu.com –recv 40976EAF437D05B5

    清理 apt deb 缓存文件,释放磁盘空间

    apt-get clean
    
  • 用PM等调整过分区后出现grub rescue解决办法

    本文是ubuntu没存在问题,调整过windows的分区后出现的

    grub rescue>

    解决方法:

    grub rescue> ls

    grub rescue> ls (hd0,x) /boot/grub   尝试x,确定装有grub的分区,x为数字

    我的ubuntu原来为(hd0,8),现在为(hd0,9)

    grub rescue >set root=(hd0,9)

    grub rescue >set prefix=(hd0,9)/boot/grub

    grub rescue >insmod /boot/grub/normal.mod

    grub rescue >normal

    回车以后便是熟悉的grub 1.99界面,这样可以进入ubuntu

    当然不能每次开机都这样2B一样输命令,所以进入到系统后,用root执行

    #update-grub

    把grub的配置信息更新好

    #grub-install /dev/sda

    在sda这块硬盘上安装grub2,而不是在某分区。

    :)EOT

  • Linux下计算MD5和SHA1

    计算MD5 – md5sum

    # md5sum QQ2011.exe
     2b68bc86016a6204096db7f0f8d06541  QQ2011.exe

    计算SHA1 – sha1sum

    # sha1sum QQ2011.exe
     73229f7b44368e8f468f91b1ad13959e0cfa405e  QQ2011.exe

    如果要计算admin的sha1sum值,使用如下命令,其中-n表示不输出换行符

    echo -n 'admin' | sha1sum

    php计算md5值

    $str='20141016hoopchina.com.cnhoopchina_P5ea8hoopchina_MsY#5';
    echo md5($str);
    
  • linux工具之Screen/byobu

    安装screen

    #apt-get install screen

    安装byobu

    #apt-get install byobu

    接下来主要讲screen Detached表示会话处于挂起状态,Attached表示有终端在连接会话,Dead状态为死掉状态 清理screen的Dead会话,可以查看到此会话的进程号,然后kill -9,之后再screen -wipe

    [detached from 10054.pts-0.www]
    jpuyy@www:~$ kill -9 10054
    jpuyy@www:~$ screen -ls
    There is a screen on:
     10054.pts-0.www (2012年03月03日 21时09分03秒) (Dead ???)
    Remove dead screens with 'screen -wipe'.
    1 Socket in /var/run/screen/S-jpuyy.
    jpuyy@www:~$ screen -wipe
    There is a screen on:
     10054.pts-0.www (2012年03月03日 21时09分04秒) (Removed)
    1 socket wiped out.
    No Sockets found in /var/run/screen/S-jpuyy.

    screen 新建screen并登陆到其中 screen -t win 创建一个以win作为默认窗口标题的screen并登陆到其中 screen -list 列出screen列表 screen -wipe 清理僵死的screen窗口 screen -r [pid] 连接到当前无用户在连接的对应pid的screen,pid可以通过screen -list获得 screen -x [pid] 连接到对应pid的screen,不论当前有无用户已连接其中 screen窗口内部常用命令: 先按CTRL+a,然后松开再按下面的键: c 创建新的screen窗口并切换到其中 K 杀死当前screen窗口 ” 列出当前screen窗口列表 ‘ 输入screen窗口号码或名称的提示符 0 选择0号窗口 … 9 选择9号窗口 A 设置当前screen窗口的标题 d 断开与当前scree终端的连接 DD 断开与当前screen终端的连接并注销 –added in 20121204 用无线连接时,正在使用screen,突然网断了,然后重新连到服务器上

    root@ibm:~# screen -r
    There is a screen on:
     24643.pts-0.ibm (11/27/2012 07:50:06 PM) (Attached)
    There is no screen to be resumed.

    发现不能回到原来的screen,从网上查得使用如下命令,踢掉上次中断的用户,并接管screen:

    screen -D -r <session-id>

    如上面的例子就是 :

    screen -D -r 24643

    使用node js版的shadowsocks时,会自动挂掉,所以把它放到screen中,用crontab每45分钟重开启一下

    */45 * * * * /usr/bin/killall screen; /usr/bin/screen -dmS node /usr/local/bin/node /usr/local/shadowsocks-nodejs/server.js

    参考文章: http://www.ibm.com/developerworks/cn/linux/l-cn-screen/

    使用screen翻屏

    Ctrl+A, Esc 进入 C/P 模式,然后用Page Up/Down 翻页
    还能用?<string> 查找

    使用screen的时候,通用配置文件在/etc/screenrc,可以新建~/.screenrc从而使用定制化的screen配置。 add @2014-05-15 启动screen的时候,直接连接多个ssh,用tab区分

    screen -dmS sshmulti -c sshmulti.conf

    原理是将ssh的命令和名称写到sshmulti.conf,启动时指定配置文件,配置文件如下

    escape ^Xx
    autodetach      on      # default: on
    crlf         off      # default: off
    hardcopy_append   on      # default: off
    startup_message   off      # default: on
    termcap  xterm   'AF=\E[3%dm:AB=\E[4%dm'
    terminfo xterm   'AF=\E[3%p1%dm:AB=\E[4%p1%dm'
    vbell         off      # default: ???
    defscrollback      1000      # default: 100
    silencewait      15      # default: 30
    
    caption always "%{wk}%?%-Lw%?%{bw}%n*%f %t%?(%u)%?%{wk}%?%+Lw%? %= %{= kw}%110`%109`%111`"
    hardstatus string '%99`%{= kw} %100`%112`%= %102`%101`%114`%115`%108`%113`%119`%117`%118`%116`%106`%104`%103`%105`%107`%Y-%m-%d %0c:%s'
    
    activity      "activity in %n (%t) [%w:%s]~"
    bell         "bell     in %n (%t) [%w:%s]~"
    pow_detach_msg   "BYE"
    vbell_msg      " *beep* "
    
    screen -t server88 ssh 192.168.1.88
    screen -t server99 ssh 192.168.1.99
    

    参考:http://askubuntu.com/questions/49245/start-multiple-apps-running-in-shells-split-in-one-console

    有时在 screen 里会要看很长的历史,则需要把 buffer 加大,要看的时候按 ctrl-a 再按 {,再按 u 上翻, d 下翻

    对 session 重命名

    screen  -list
    There are screens on:
            2580.pts-0.centos7-work (Detached)
            2478.pts-0.centos7-work (Detached)
    2 Sockets in /var/run/screen/S-vagrant.
    # 重命名 -S 当前名 -X sessionname 要修改的名字
    screen -S 2580.pts-0.centos7-work -X sessionname backend-service
    

    创建新的 tab

    press Ctrl+A+c to create a new virtual console
    

    切换

    press Ctrl+A+n to switch to next tab
    press Ctrl+A+p to switch to previous tab