技术头条 - 一个快速在微博传播文章的方式     搜索本站
您现在的位置首页 --> 系统运维 --> Little Tools: Killtree & Ssh_auto

Little Tools: Killtree & Ssh_auto

浏览:2491次  出处信息
    作为一个 IT 民工,在写代码的过程总是会遇到一些不尽如人意之事。这不,我在工作过程中也遇到一些很郁闷的事情!
  • 场景1

         终于写完了一个复杂的流程,流程里面一环套一环。于是乎,很兴奋的开始后台运行,然后在流程的中间文件有个错误之前没有发现。接下来的事情相信很多在非 Windows 环境下作开发的朋友都会遇到的闹心事情:

        先用

    pstree -ap

        获取了所有进程的情况(下面是节选了其中一部分)

    sh,19058 -c cd\\040/search/for_cacti/script;\\040sh\\040time.sh\\040../conf/cpc_performance.conf\\0401>>../log/perf.log\\0402>&1
      `-sh,19067 time.sh ../conf/cpc_performance.conf
          `-sh,19134 time.sh ../conf/cpc_performance.conf
              |-awk,19138 -F\\t -v check_time=20110509\\04015:30:01...
              `-rclog,19135 /opt/rclog/rclog -s contextserver --local --cat -l ie_log --last=5

        当然上面这个进程树,无论是深度还是广度已经算少了,有时候多的能烦死人。

         然后很郁闷的,从父到子一个一个的 kill。

    kill -9 19058; kill -9 19067; kill -9 19134; kill -9 19138; kill -9 19135;

        刚开始的时候,我总是可以耐心地这么一个一个的 kill ,但是时间久了,我就完全不能忍了,这也太崩溃了!为了发扬一下 IT 民工的 DIY 精神,我只得写了一个小脚本 killtree.sh 来帮我处理这个繁琐的过程。

    # Author: Bill Wung
    # url: http://www.iron-feet.com
    pidkill=$1
    pidlist=` ps -eo ppid,pid | sed 1d | awk \'{ if( $1!= $2 ) print $1"\\t"$2 }\' | sort | uniq `
     
    set -u
     
    killpid()
    {
        set -x
        kill -9 "$1"
        set +x
     
        for subpid in ` echo "$pidlist" | awk -F"\\t" \'{ if( $1==\'"$1"\' ) print $2 }\' `
        do
            killpid "$subpid" 
        done
     
    }
     
    killpid "$pidkill"

        以上面列举的进程情况为例,你只需要执行一下

    sh killtree.sh 19058

        就 OK 了

  • 场景2

         你需要对服务器做一些操作,并且这个操作是需要每天做一次的。如果做成每日流程放入 crontab 那该多好呀。

         可是无情的 OP 和我说:“这台机子不允许非 OP 搭建每日流程,也不允许 OP 以外的人为该机器建立 ssh 的信任关系。”

         我顿时崩溃

        我完全没法忍受,每天同一时间去手动地跑一个任务,太土鳖了!于是找了一下,发现了一个叫 expect 的东东!

        根据 Linux Man Page ,expect 自动自动实现人机交互的操作,例如:输入密码、敲个 yes 什么的。

         Expect is a program that “talks” to other interactive programs according to a script. Following the script, Expect knows what can be expected from a program and what the correct response should be. An interpreted language provides branching and high-level control structures to direct the dialogue. In addition, the user can take control and interact directly when desired, afterward returning control to the script.

        大致了解了一下 expect 的语法,写了一个小脚本 ssh_auto.expect 来解决自动登录和发送命令的问题。

    # Author: Bill Wung
    # url: http://www.iron-feet.com
    set host [lindex $argv 0]
    set cmd [lindex $argv 1]
    set usr [lindex $argv 2]
    set psw [lindex $argv 3]
     
    spawn ssh "$usr@$host" "$cmd"
     
    set timeout 5
    expect {
    	"(yes/no)" {
    		send "yes\\r";
    		exp_continue
    	}
    }
     
    set timeout 5
    expect {
    	"id_rsa\'" {
            	send "\\r";
            	exp_continue
        	}
    }
     
    set timeout 5
    expect {
    	"Password:" { 
    		send "$psw\\r";
    		exp_continue
    	}
    }
     
    set timeout 5
    expect {
    	"password:" {
            	send "$psw\\r";
            	exp_continue
    	}
    }
     
    exit

        由于该脚本是 expect 的脚本,所以执行的时候需要用 expect 调用。

    expect ssh_auto.expect 10.10.10.10 "cd /aa/bb; sh cc.sh; " root rootpassword
    # 第一个参数是目标IP 第二个参数是执行的命令 第三个参数是用户名 第四个就是密码了

        由于很多 Linux 的发行版都不自带 expect ,所以部分用户需要自行安装一下 expect 才能调用该脚本。

  •     以上两个脚本,只是我平时使用,并考虑到太多的需求。

         我会在 Google Code 开一个项目,将我平时随手写的一些小脚本扔进去。各位如果觉得有用的话,可以使用;如果有什么建议的话,更是不吝赐教。

        项目的地址是 http://code.google.com/p/iron-feet-tools/ ,目前里面就两个脚本以后会越来越多的。

        项目前几天刚建立, wiki 啊什么的改天再完善吧!

    建议继续学习:

    1. Linux shell脚本使用while循环执行ssh的注意事项    (阅读:6402)
    2. 在ssh服务里使用chroot    (阅读:4701)
    3. 为什么要用公钥/私钥而不是密码去做SSH身份验证    (阅读:4533)
    4. 懒人连ssh不输密码若干大法    (阅读:4361)
    5. ssh连接超时解决办法    (阅读:4325)
    6. 如何让ssh登录更加安全    (阅读:4333)
    7. ssh命令    (阅读:4064)
    8. SSH无密码登录    (阅读:4057)
    9. 共享会话的ssh连接配置    (阅读:3432)
    10. SSH下连接Oracle的方法    (阅读:3274)
    QQ技术交流群:445447336,欢迎加入!
    扫一扫订阅我的微信号:IT技术博客大学习
    © 2009 - 2024 by blogread.cn 微博:@IT技术博客大学习

    京ICP备15002552号-1