我们在管理服务器的时候,很多时候是一台管理机器,多台放相同文件的服务器。有时候我们需要给这多台服务器传输文件,执行脚本或者命令,一台一台执行时间效率很低,这个脚本主要是不需要在客户端安装软件,使用方便,次脚本功能简单还在完善中,后面会慢慢修改完善功能。

    下图是所有文件

auto_expect:

 


  
  1. #!/usr/bin/expect -f 
  2.  
  3.  
  4. spawn rsync -avzP source root@$ip:dest 
  5.  
  6. expect { 
  7.     "(yes/no)?" {send "yes\r";exp_continue} 
  8.     "password:" {send  "$pwd\r"
  9. expect eof 

主要是通过调用实现批量自动与多台服务器交互。 

Expect中,有三个重要的主命令,分别是spawnexpectexp_send,这三个命令几乎存在于所有Expect脚本中,除了这些之外,还有很多Expect所特有的参数、变量,它们也作用与Expect的方方面面。

spawn激活,send发送到交互命令。

 

iplist_config:

 

存放ip和密码的配置文件,ip在前,密码和ip用tab隔开。

 

 

T_file.sh:
 

实现功能的脚本

 

 


  
  1. #!/usr/bin/expect -f 
  2.  
  3. ################define variables############## 定义变量
  4.  
  5. path=`pwd` 
  6.  
  7.  
  8. ################begin of main ################# 
  9.  
  10.  
  11. show_usage() 
  12.         echo "USAGE:$0" 
  13.         echo "          [-s | -source]" 
  14.         echo "          [-d | -des   ]" 
  15.         echo "          [-h | -help  ]" 
  16.  
  17. auto_rsync() 
  18.         while read iplist 
  19.         do 
  20.                 ip=`echo $iplist | awk '{print $1}'
  21.                 pwd=`echo $iplist | awk '{print $2}'
  22.                 echo -e "$iplist\n" 
  23.                 echo "rsync -avzp $source_file root@$ip:$des_file" 
  24.                 sed -e "s;source;$source_file;g" auto_expect>auto_expect_rsync 
  25.                 sed -i "s;dest;$des_file;g" auto_expect_rsync 
  26.                 sed -i "s;\$ip;$ip;g" auto_expect_rsync 
  27.                 sed -i "s;\$pwd;$pwd;g" auto_expect_rsync 
  28.                 chmod +x $path/auto_expect_rsync 
  29.                 $path/auto_expect_rsync 
  30.         done < $path/iplist_config 
  31. auto_ssh() 
  32.         while read iplist 
  33.         do 
  34.                 ip=`echo $iplist | awk '{print $1}'
  35.                 pwd=`echo $iplist | awk '{print $2}'
  36.                 echo -e "$iplist\n" 
  37.                 echo "ssh root@$ip" 
  38.                 sed -e "s;source;;g" auto_expect>auto_expect_ssh 
  39.                 sed -i "s;rsync -avzP;ssh;g" auto_expect_ssh 
  40.                 sed -i "s;:dest;;g" auto_expect_ssh 
  41.                 sed -i "s;\$ip;$ip;g" auto_expect_ssh 
  42.                 sed -i "s;\$pwd;$pwd;g" auto_expect_ssh 
  43.                 chmod +x $path/auto_expect_ssh 
  44.         done < $path/iplist_config 
  45.         exit 1 
  46. if [ $# -ne 4 ];then 
  47.         echo -e "\e[32;5;1m please input current!! \e[0m\n" 
  48.         show_usage; 
  49.         exit 1; 
  50. fi 
  51.  
  52. while true 
  53. do 
  54.         case "$1" in 
  55.         -h | -help) 
  56.         show_usage 
  57.         ;; 
  58.         -s | -source) source_file=$2 
  59.         echo $soutce_file 
  60.         shift 2 
  61.         ;; 
  62.         -d | -dest) des_file=$2 
  63.         echo $des_file 
  64.         auto_rsync 
  65.         auto_ssh 
  66.         ;; 
  67.         *) 
  68.         show_usage 
  69.         exit 1 
  70.         ;; 
  71.         esac 
  72. done 

 

 

执行的时候,把所以服务器的ip和密码存放在iplist_config文件中去。

sh T_file.sh -s source_path -d des_path

-s后面加源文件地址,-d后面加需要传输到的目的地址。