deletefile 
自动删除/record_file/下面目录内,31天以前的文件的脚本。放在某台服务器上
 
  
  1. #!/bin/bash  
  2. #to delete files with is 30 day ago  
  3. cd /record_file/  
  4. for i in `ls`   
  5. do  
  6.     cd /record_file/$i  
  7.     echo `pwd`     
  8.     find ./ -mtime +31 > filelist  
  9.     sleep 2  
  10.     rm -rf `cat filelist`  
  11.     sleep 10  
  12. done 
expect_run.exp
从某台服务器上把deletefile文件批量拷到远端服务器并运行的脚本。
 
  
  1. #!/usr/tcl/bin/expect  
  2. set timeout 2  
  3. set host [lindex $argv 0]  
  4. set user [lindex $argv 1]  
  5. set password [lindex $argv 2]    
  6. spawn ssh $user@$host  
  7. # Enable this and Disable the "spawn ssh ..." above if you are using ssh-key.  
  8. #spawn ssh -i ssh-key/Identity.ppk -p $port $user@$server  
  9. expect {  
  10.         "yes/no)?\ " {send "yes\r";exp_continue}  
  11.         "*assword:\ " {send "$password\r"}  
  12.         }  
  13. expect "]*"  
  14. send "scp root@100.2.3.91:/home/deletefile /home\r"  
  15. expect {  
  16.         "yes/no)?\ " {send "yes\r";exp_continue}  
  17.         "*assword:\ " {send "123456\r"}  
  18.         }  
  19. expect "]*"  
  20. send  "cd /home/\r"  
  21. send  "nohup ./deletefile &\r"  
  22. expect eof 
 run.sh   调用expect_run.exp的主脚本
 
  
  1. #!/bin/sh  
  2. cat ip.txt | while read ip user password  
  3. do  
  4.           ./expect_run.exp $ip $user $password  
  5. done 
ip.txt   不解释,主机列表
 
  
  1. 100.2.3.92  root   123456  
  2. 100.2.3.94  root    123456  
  3. 100.2.3.93  root    123456  
  4. 100.2.3.89  root    123456  
  5. 100.2.3.88  root    123456  
  6. 100.2.3.87  root    123456  
  7. 100.2.3.90  root    123456  
  8. 。。。。 
Expect的安装及使用(提醒一点tcl8.5.10编译有可能不成功最好用 tcl8.4.11这个版本)
A. Tcl 安装
主页 http://www.tcl.tk
下载地址 http://www.tcl.tk/software/tcltk/downloadnow84.tml
2.解压缩源码包
tar xfvz tcl8.4.11-src.tar.gz
tar xfvz tk8.4.11-src.tar.gz
3.安装配置
cd tcl8.4.11
cd unix
./configure --prefix=/usr/tcl --enable-shared
make
make install
安装完毕以后,进入tcl源代码的根目录,把子目录unix下面的tclUnixPort.h copy到子目录generic中。
暂时不要删除tcl源代码,因为expect的安装过程还需要用。
B. expect 安装 (Tcl的库)
主页 http://expect.nist.gov/
1. 下载源码包
Wget   http://nchc.dl.sourceforge.net/project/expect/Expect/5.45/expect5.45.tar.gz
2.解压缩源码包
tar xfvz expect 5.45 .tar.gz
3.安装配置
cd expect-5.4 5
./configure --prefix=/usr/expect --with-tcl=/usr/tcl/lib  --with-tclinclude=
/tcl源代码路径/generic
make
make install