JAVA、PHP、前端、APP、网站开发 - 开发技术学习

开发技术学习 » 服务器 » shell脚本中使用expect

shell脚本中使用expect

此文被围观1842日期: 2018-10-08 分类 : 服务器  标签:  ···

shell更新集群文件,因为自动发布系统未完善,需要更新文件到所有集群主机上。于是使用shell expect,以下代码来自网络后修改运行的。

#!/bin/bash
help()
{
    cat << HELP
    --------------HELP------------------------
    This shell script can copy file to many computers.
    Useage:
        copytoall filename(full path form /home) targetpathfrom/ username ip1 ip2 ip3....
    Example:
        copytoall /root/log.log /roo casliyang 192.168.0.5 192.168.0.6 192.168.0.7 192.168.0.8
    ------------------------------------------
HELP
  exit 0
}
 
currentdate=$(date +%Y-%m)
 
echo $currentdate " execute copytoall"
 
if [ $1 = "-h" ] ; then
    help
    exit 0
fi
 
file=$1
shift
targetpath=$1
shift
user=$1
shift
tempip=0
hostListFile=/root/list.log
password="123456"
 
if [ -f $file ] ; then
   if [ ! -n "$1" ] ; then
       cat $hostListFile | while read line
       do 
	    echo $line
	    if [ -n "$line" ] ; then
	    expect -c "
	    spawn scp -r $file ${user}@${line}:${targetpath}
	    expect {
	       "yes/no" {send "yesn"; exp_continue;}
               "*assword:*" {set timeout 300; send "$passwordn";}
            }
            expect eof"
            #sleep 3
	    else

	    echo "none"

	    #fi & //这果可以实现多进程执行
	    fi
	done
	wait
	exit 0
    else
	while [ $# -gt 0 ] ; do
	    tempip=$1
	    shift
	    expect -c "
            spawn scp $file ${user}@${tempip}:${targetpath}
	    expect {
	       "yes/no" {send "yesn"; exp_continue;}
               "*assword:*" {set timeout 300; send "$passwordn";}
	    }
	    expect eof"
        done
    fi
 
else
 
    echo "wrong file!"
 
    exit 0
fi


shell批量复制文件到集群,expect

参考链接:

https://blog.csdn.net/lufeisan/article/details/53488395

https://blog.csdn.net/a11101171/article/details/47293109

https://blog.csdn.net/qq_36898043/article/details/79416795

多进程执行:

https://yq.aliyun.com/ziliao/65843


站点声明:部分内容源自互联网,为传播信息之用,如有侵权,请联系我们删除。

© Copyright 2011-2024 www.kfju.com. All Rights Reserved.
超级字帖 版权所有。蜀ICP备12031064号