Cleanup 一个清理所有节点进程的小Script - XiZi's Blog

Cleanup 一个清理所有节点进程的小Script

Xizi posted @ 2014年3月18日 23:10 in 未分类 with tags bash expect cluster , 1593 阅读
#!/bin/bash
if [ x$1 = x ]; then
    echo "use $0 processname to kill all named process on all active nodes."
    exit
fi

activenodes=$(shownodes | sed -n 's/\(c[0-9]\{4\}-[0-9]\/8\)/\1/gp' | sed -n 's/ /\n/pg' | sed -n 's/.*\(c[0-9]\{4\}\)-[0-9]\/8.*/\1/gp')
#echo $activenodes

#run Expect
/usr/bin/expect -<<EOD
foreach node {
$activenodes
} {
#循环,可以多台相同密码的服务器同时处理,不同密码的需要改动一下,在下面再加一个密码的循环,
spawn rsh \$node
expect {
"*(yes/no)*" { send "yes\r"}
"password:" { send "password\r" }
}            #SSH第一次远程登录服务器
expect "username*"
#send "su -\r"        #SSH普通用户名密码登录
#expect "Password:"
#send "123456\r"        #SSH的root用户名密码登录
#expect -timeout 1
#expect "root"
send "ps aux | grep $1 \r"
send "killall -9 $1 \r"
#send "echo helloworld \r"
#expect "username"
send "exit \r"
}
EOD

echo "Completed"

updated but not tested one

#!/bin/bash
if [ x$1 = x ]; then
    echo "use $0 processname to kill all named process on all active nodes."
    exit
fi

activenodes=$(shownodes | sed -n 's/\(c[0-9]\{4\}-[0-9]\/8\)/\1/gp' | sed -n 's/ /\n/pg' | sed -n 's/.*\(c[0-9]\{4\}\)-[0-9]\/8.*/\1/gp')
#echo $activenodes
logfile="$HOME/bin/cleanup.log"
echo "" > $logfile
#run Expect
/usr/bin/expect -<<EOD
foreach node {
$activenodes
} {
#循环,可以多台相同密码的服务器同时处理,不同密码的需要改动一下,在下面再加一个密码的循环,
spawn rsh \$node
expect {
"*(yes/no)*" { send "yes\r"}
"password:" { send "password\r" }
}            #SSH第一次远程登录服务器
expect "usename*"
#send "su -\r"        #SSH普通用户名密码登录
#expect "Password:"
#send "123456\r"        #SSH的root用户名密码登录
#expect -timeout 1
#expect "root"
send "echo \\\$HOSTNAME >> $logfile \r"
send "ps aux | grep $1 | grep -v grep >> $logfile \r"
send "killall -9 $1 \r"
#send "echo helloworld \r"
#expect "sofair"
send "exit \r"
}
EOD

echo "Completed"
echo "Logs are Saved to $logfile"

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter
Host by is-Programmer.com | Power by Chito 1.3.3 beta | © 2007 LinuxGem | Design by Matthew "Agent Spork" McGee