栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

Linux高阶—shell远程批处理脚本(七)

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Linux高阶—shell远程批处理脚本(七)

目录

介绍:简单、实用的批处理脚本

案例一:批量下发远程执行命令

案例二:批量下发文件


介绍:实用、简单的批处理脚本

expect是处理交互式命令的命令,它可以将类似ssh登录,ftp登录等等需要交互式的场景自动输入登录账号密码, 使其完成自动化运行。

案例一:批量下发远程执行命令
#Remote execution tool
vim remote.sh 

#!/bin/bash
_work_dir=/root/tmp
#Build workdir directory
    if [ ! -d ${_work_dir} ]
    then
        mkdir ${_work_dir}
    fi
_log_dir=/root/tmp/log
#Build log directory
    if [ ! -d ${_log_dir} ]
    then
        mkdir ${_log_dir}
    fi
	
touch ${_log_dir}/check.log 
_check_msg=${_log_dir}/check.log

function info() {
    echo -e "33[32m [INFO] 33[0m$@ "
}
function warn() {
    echo -e "33[31m [WARN] 33[0m$@"
}

# LAN SERVERS
LANSERVER=(
192.168.31.214,test01
192.168.31.215,test02
) #填写IP与主机名
_REMOTE_UN="" #填写登入用户名
_REMOTE_PW="" #填写登入密码
_ROOT_PW=""  #填写root密码

echo -e "33[36m+++Please enter the shell command to be executed+++33[0m"
read -p "Command: " _CMD #输入需要执行的指令

# CHECK
for ((i=0; i< ${#LANSERVER[*]}; i++))
  do
   _REMOTE_IP=`echo "${LANSERVER[$i]}" |awk -F ',' '{print $1}'`
   _REMOTE_NAME=`echo "${LANSERVER[$i]}" |awk -F ',' '{print $2}'`
   
   if [ "$_ROOT_PW" = "" ]; then
      expect common_user.exp "${_REMOTE_NAME}" "${_REMOTE_IP}" "${_REMOTE_UN}" "${_REMOTE_PW}" "${_CMD}"
   else
      expect root_user.exp "${_REMOTE_NAME}" "${_REMOTE_IP}" "${_REMOTE_UN}" "${_REMOTE_PW}" "${_ROOT_PW}" "${_CMD}"
   fi
   
   if [ $? -eq 0 ];then
      info "`date +'[%Y-%m-%d %H:%M:%S]'` Check ${_REMOTE_IP} *************  Command ************** [success]" |tee -a ${_check_msg}
   else
      warn "`date +'[%Y-%m-%d %H:%M:%S]'` Check ${_REMOTE_IP} *************  Command ************** [failure]" |tee -a ${_check_msg}
      continue
   fi
  done

#remote common_user.exp
vim common_user.exp

#!/usr/bin/expect -f
set timeout 5

set host_name  [lindex $argv 0]
set host_ip    [lindex $argv 1]
set host_un    [lindex $argv 2]
set host_pw    [lindex $argv 3]
set CMD        [lindex $argv 4]

set login_flag 1

spawn ssh ${host_un}@${host_ip}

 expect {
   "yes/no" {send "yesr"; exp_continue}
   "password:" {send "${host_pw}r"}
   "Password:" {send "${host_pw}r"}
 }
 expect {
   "${host_name}" {set login_flag 0 ;send "${CMD}r"}
   expect eof
 }
send "exitr"
expect eof
exit $login_flag


#remote root_user.exp
vim root_user.exp

#!/usr/bin/expect -f
set timeout 5

set host_name  [lindex $argv 0]
set host_ip    [lindex $argv 1]
set host_un    [lindex $argv 2]
set host_pw    [lindex $argv 3]
set root_pw    [lindex $argv 4]
set CMD        [lindex $argv 5]

set login_flag 1

spawn ssh ${host_un}@${host_ip}

 expect {
   "yes/no" {send "yesr"; exp_continue}
   "password:" {send "${host_pw}r"}
   "Password:" {send "${host_pw}r"}
 }
 expect {
   "${host_name}" {set login_flag 0 ;send "su -r"; exp_continue}
   "password:" {send "${root_pw}r"}
   "Password:" {send "${root_pw}r"}
 }
 expect {
   "*${host_name}*#" {set login_flag 0 ;send "${CMD}r"}
   expect eof
 }
send "exitr"
expect eof
exit $login_flag

执行结果如下:

案例二:批量下发文件
vi scp_file.sh 
##################################################################
#!/bin/bash
_work_dir=/root/tmp
#Build workdir directory
    if [ ! -d ${_work_dir} ]
    then
        mkdir ${_work_dir}
    fi
_log_dir=/root/tmp/log
#Build log directory
    if [ ! -d ${_log_dir} ]
    then
        mkdir ${_log_dir}
    fi
	
touch ${_log_dir}/check.log 
_check_msg=${_log_dir}/check.log

function info() {
    echo -e "33[32m [INFO] 33[0m$@ "
}
function warn() {
    echo -e "33[31m [WARN] 33[0m$@"
}

# LAN SERVERS
LANSERVER=(
192.168.31.214,test01
192.168.31.215,test02
) #填写IP与主机名
_REMOTE_UN="luojie" #填写登入用户名
_REMOTE_PW="123456" #填写登入密码
_SRC_FILE="1.txt" #填写源文件
_DST_FILE="/tmp/1.txt" #填写目的文件

echo -e "33[36m+++ shell SCP to be executed+++33[0m"
# CHECK
for ((i=0; i< ${#LANSERVER[*]}; i++))
  do
   _REMOTE_IP=`echo "${LANSERVER[$i]}" |awk -F ',' '{print $1}'`
   _REMOTE_NAME=`echo "${LANSERVER[$i]}" |awk -F ',' '{print $2}'`
   
   expect myscp.exp "${_REMOTE_NAME}" "${_REMOTE_IP}" "${_REMOTE_UN}" "${_REMOTE_PW}" "${_SRC_FILE}" "${_DST_FILE}"
   
   if [ $? -eq 0 ];then
      info "`date +'[%Y-%m-%d %H:%M:%S]'` Check ${_REMOTE_IP} *************  SCP ************** [success]" |tee -a ${_check_msg}
   else
      warn "`date +'[%Y-%m-%d %H:%M:%S]'` Check ${_REMOTE_IP} *************  SCP ************** [failure]" |tee -a ${_check_msg}
      continue
   fi
  done

#################################################################
vi myscp.exp

#!/usr/bin/expect -f
set timeout 5

set host_name  [lindex $argv 0]
set host_ip    [lindex $argv 1]
set host_un    [lindex $argv 2]
set host_pw    [lindex $argv 3]
set src_file   [lindex $argv 4]
set dst_file   [lindex $argv 5]

spawn scp ${_src_file} ${host_un}@${host_ip}:${_dst_file}

 expect {
   "yes/no" {send "yesr"; exp_continue}
   "password:" {send "${host_pw}r"}
   "Password:" {send "${host_pw}r"}
   expect eof
 }
send "exitr"
expect eof

执行结果:

 

上一篇:Linux高阶—shell脚本编程基础(六)

下一篇:Linux高阶—shell脚本企业实战(八)

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/867656.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号