expect批量远程基础资源统计脚本

生产环境:centos5.6    x86_64    expect-5.43.0-5.1

基础资源统计脚本:

 

  
  
  
  
  1. cat monitor_servers.sh
  2.  
  3. #!/bin/sh  
  4. cat <<EOF 
  5. +----------------------------------------------------------+  
  6. |        ===centos system monitor servers==                +  
  7. +       USAGE:scriptname remote_exec_command               +  
  8. +-------------------by iceeggplant 2012.8.2 ----------------------+  
  9. EOF  
  10.  
  11. # iplist.txt file is needs to be monitor servers list   
  12. # monitor.exp file need four argument,example:  
  13. # expect xx.exp local_transfer_file remote_server_ip remote_cmd remote_server_passwd  
  14.  
  15. echo "start remote_host to exec expect monitor script,please input remote_host passwd:"  
  16.  
  17. read passwd  
  18. for host in `cat /tmp/iplist.txt`  
  19. do   
  20.  expect /root/monitor-script/monitor.exp $host "$1" $passwd >>/tmp/monitor.log 2>&1   
  21.   if [ $? -eq 0 ];then  
  22.     echo "$host run ok"  
  23.   else  
  24.     echo "$host run false"  
  25.  fi  
  26. done 

 

  
  
  
  
  1. cat monitor.exp  
  2.  
  3. #!/usr/bin/expect -f  
  4. #check command line argument usage whether right  
  5. if { $argc!=3 } {  
  6. send_user "argemunt error:this script usage:expect script.filename  remote_ip exec_command remote_passwd\n"  
  7. exit  
  8. }  
  9.  
  10. #define variable,get argument from expect script command line  
  11. set ip [lindex $argv 0]  
  12. set comd [lindex $argv 1]  
  13. set passwd [lindex $argv 2]  
  14.  
  15. #start spawn a progress transfer file,timeout 60 exit...  
  16. spawn ssh root@$ip $comd  
  17. set timeout 60  
  18. expect {  
  19.   -timeout 20  
  20. #  "yes/no" { send "yes\r";exp_continue }  若非第一次登录不会返回此交互结果,可 
  21. 注释先
  22.   "*password:" { exp_send "$passwd\r" }  
  23.   timeout { puts "expect connect timeout";return }  
  24. }  
  25. expect eof  
  26.  
  27. exit -onexit {  
  28. send_user "$ip interaction has been completed-----------------------------\r"  

 

  
  
  
  
  1. 执行过程:  
  2. # sh monitor_servers.sh "df -h;free -m"  
  3. +----------------------------------------------------------+  
  4. |        ===centos system monitor servers==                +  
  5. +       USAGE:scriptname remote_exec_command               +  
  6. +-------------------by wyan 2012.8.2 ----------------------+  
  7. start remote_host to exec expect monitor script,please input remote_host passwd:  
  8. 123456  
  9. 192.168.7.2 run ok  
  10. 192.168.7.3 run ok  
  11. 192.168.7.4 run ok  
  12. .....  
  13.  
  14. 结果可查看/tmp/monitor.log 

 

你可能感兴趣的:(expect,监控脚本)