基于shell实现向多台服务器拷贝hosts文件
2017-10-28 12:38
507 查看
写这个脚本的目的是在生产环境中向多个服务器拷贝hosts文件,能满足自己的使用要求。
github: https://github.com/charnet1019/auto-scp-files.git
注意:
需要安装expect
github: https://github.com/charnet1019/auto-scp-files.git
注意:
需要安装expect
#!/bin/bash ########################################################## # # Copyright (2017-10-21, ) # # Author: charnet1019@163.com # Last modified: 2017-10-22 00:06 # Description: # ########################################################## # definition IP dictionary array declare -A IPListDict # 所有主机默认使用root帐号 USERNAME=root # 本地要拷贝的文件 LOCAL="/etc/hosts" # 拷贝到远程主机的位置 REMOTE="/tmp" # 主机IP地址和密码,每行一个主机 IPListDict=( [192.168.2.4]="secret" ) ssh_key_base_dir=~/.ssh ssh_known_hosts=$ssh_key_base_dir/known_hosts SCP=/usr/bin/scp DATETIME=`date "+%F %T"` success() { printf "\r$DATETIME [ \033[00;32mINFO\033[0m ]%s\n" "$1" } warn() { printf "\r$DATETIME [\033[0;33mWARNING\033[0m]%s\n" "$1" } fail() { printf "\r$DATETIME [ \033[0;31mERROR\033[0m ]%s\n" "$1" } usage() { echo "Usage: ${0##*/} {info|warn|err} MSG" } log() { if [ $# -lt 2 ]; then log err "Not enough arguments [$#] to log." fi __LOG_PRIO="$1" shift __LOG_MSG="$*" case "${__LOG_PRIO}" in crit) __LOG_PRIO="CRIT";; err) __LOG_PRIO="ERROR";; warn) __LOG_PRIO="WARNING";; info) __LOG_PRIO="INFO";; debug) __LOG_PRIO="DEBUG";; esac if [ "${__LOG_PRIO}" = "INFO" ]; then success " $__LOG_MSG" elif [ "${__LOG_PRIO}" = "WARNING" ]; then warn " $__LOG_MSG" elif [ "${__LOG_PRIO}" = "ERROR" ]; then fail " $__LOG_MSG" else usage fi } is_exist_expect() { if ! which expect &> /dev/null; then return 1 fi return 0 } install_expect() { yum -y install expect &> /dev/null } check_expect() { is_exist_expect if [ $? -eq 1 ]; then log warn "No expect command and try to install, please wait..." install_expect is_exist_expect if [ $? -eq 1 ]; then log err "Installation failed, please install the expect command manually." exit 1 else log info "Installation successed." fi fi } #is_exist_hosts() { # local FILE=$1 # # [ -f ${FILE} ] && return 0 || return 1 #} get_cipher() { local IP=$1 for key in ${!IPListDict[@]}; do if [[ X"$IP" == X"$key" ]]; then PASSWORD="${IPListDict[$key]}" fi done } exec_cp() { for ip in ${!IPListDict[@]}; do get_cipher $ip expect -c <<- EOF &> /dev/null " spawn $SCP -r $LOCAL $USERNAME@$ip:$REMOTE expect { \"(yes/no)?\" { send \"yes\r\" expect { "*assword" { send \"$PASSWORD\r\" } } } "*assword*" { send \"$PASSWORD\r\" } expect "100%" expect eof } catch wait retVal exit [lindex \$retVal 3]" EOF if [ $? -eq 0 ]; then log info "The file $LOCAL copy to the remote host $ip successed." else log err "The file $LOCAL copy to the remote host $ip failed." fi done } # main check_expect exec_cp
相关文章推荐
- [shell实例]——用脚本实现向多台服务器批量复制文件(nmap、scp)
- 每天学点linux--(用shell实现将一个文件夹下面的所有文件拷贝到另一个文件夹下/shell下如何重命名一个文件)
- Shell脚本实现线上服务器之间对比文件是否一致
- php中本地文件拷贝至远程服务器的实现方式
- Android开发 基于HttpComponent4.3.3实现文件上传至tomcat服务器(基于struts框架)
- shell脚本实现拷贝大文件显示百分比的代码分享
- windows基于阿帕奇服务器,实现vc++文件下载功能
- ubuntu修改hosts文件 跳过DNS解析实现服务器定向登录(google,FB,TW等)
- CMD实现本地文件(夹)拷贝到服务器
- 构建基于Nginx的文件服务器思路与实现
- 【Linux笔记】在后台执行scp,实现服务器间无密码文件拷贝。
- Python基于sftp及rsa密匙实现远程拷贝文件的方法
- javaCV开发详解之3:收流器实现,录制流媒体服务器的rtsp/rtmp视频文件(基于javaCV-FFMPEG)
- 构建基于Nginx的文件服务器思路与实现
- Shell脚本实现复制文件到多台服务器的代码分享
- 【甘道夫】拷贝文件到多台服务器的Shell脚本
- javaCV开发详解之3:收流器实现,录制流媒体服务器的rtsp/rtmp视频文件(基于javaCV-FFMPEG)
- shell编程实例四实现文件拷贝
- 通过python模块实现服务器和本地机器之间快速拷贝文件
- 基于jenkins结合svn、ansible、shell和mysql版本迁移实现多服务器批量发布 推荐