linux脚本expect,自动化脚本实践(Shell + Expect)

实现的功能

批量登录目标主机(800+)并执行命令

拷贝文件到多台目标主机(800+)

1 准备主机信息

获取需要批量执行的主机IP信息,保存在server.list文件中。注意同一批次的服务器,账号密码要求一致。

# cps host-list | grep 2409:8080 | awk -F' ' '{print $12}'

# vim server.list

IP1

IP2

IP3

2 准备Shell + Expect脚本

The Unix "parameters":

$# => contains the number of parameters for this shell is started

$1 $2 ... => parameter 1 parameter 2 ...

$@ => includes all the parameters that this shell is started

2.1 批量登录目标主机并执行命令

涉及的文件

/home/fsp/login-execute.sh

/home/fsp/login-execute-batch.sh

/home/fsp/server.list

登录单个目标主机并执行命令:login-execute.sh:

#!/usr/bin/expect

# usage: login-execute.sh

set timeout 20

set ip [lrange $argv 0 0]

set fsppwd [lrange $argv 1 1]

set rootpwd [lrange $argv 2 2]

set command [lrange $argv 3 end]

spawn ssh -x -o StrictHostKeyChecking=no fsp@$ip

expect "assword:"

send "$fsppwd\r"

expect "fsp@"

send "su root\r"

expect "assword:"

send "$rootpwd\r"

expect "/home/fsp #"

send "$command\r"

expect "/home/fsp #"

send "####################################################\r"

send "exit\r"

expect "fsp@"

send "exit\r"

expect eof

exit

批量登录目标主机并执行命令:login-execute-batch.sh

#!/bin/bash

# usage: login-execute-batch.sh

if [ $# -eq 0 ]

then

echo usage: login-execute-batch.sh \\ \

exit 1

fi

for i in `cat server.list`

do

echo $i $1

./login-execute.sh $i $@

echo

done

用法

# sh -x login-execute-batch.sh

#示例

# sh -x login-execute-batch.sh Huawei@CLOUD8 Huawei@CLOUD8! "sed -i '/10.10.10.11/d' /etc/ntp.conf"

“-x”用来跟踪脚本的执行过程,使shell在执行脚本中把每一个命令行显示出来,并且在行首显示一个"+"号,"+"号后面显示的是经过了变量替换之后的命令行的内容。

2.2拷贝文件到多台目标主机

涉及的文件

/home/fsp/one-host-scp.sh

/home/fsp/batch-host-scp.sh

/home/fsp/server.list

拷贝文件到单个目标主机:one-host-scp.sh:

#!/usr/bin/expect

# usage: one-host-scp.sh

set timeout 20

set ip [lrange $argv 0 0]

set filepathnname [lrange $argv 1 1]

set fsppwd [lrange $argv 2 end]

spawn scp -o StrictHostKeyChecking=no $filepathnname fsp@\[$ip\]:/home/fsp

expect "s password:"

send "$fsppwd\r"

expect "100%"

send "####################################################\r"

expect eof

exit

拷贝文件到多台目标主机:batch-host-scp.sh:

#!/bin/bash

# usage: batch-host-scp.sh

if [ $# -eq 0 ]

then

echo usage: batch-host-scp.sh \ \

exit 1

fi

for i in `cat server.list`

do

echo $i $1

./one-host-scp.sh $i $@

echo

done

用法

# sh -x batch-host-scp.sh

#示例

# sh -x batch-host-scp.sh /home/fsp/file1 Huawei@CLOUD8

“-x”用来跟踪脚本的执行过程,使shell在执行脚本中把每一个命令行显示出来,并且在行首显示一个"+"号,"+"号后面显示的是经过了变量替换之后的命令行的内容。

3 上传脚本与输入数据

上传并修改文件权限

chmod 777 文件名

4 用法

server.list单台测试

拷贝进行测试

sh -x login-execute-batch.sh Huawei@CLOUD8 Huawei@CLOUD8! "cp /etc/ntp.conf /home/fsp/ntp.conf.test"

sh -x login-execute-batch.sh Huawei@CLOUD8 Huawei@CLOUD8! "ll /home/fsp/ntp.conf.test"

查询是否需要修改

sh -x login-execute-batch.sh Huawei@CLOUD8 Huawei@CLOUD8! "cat /home/fsp/ntp.conf.test | grep 'server 2409:8080:5a0a:500'"

sh -x login-execute-batch.sh Huawei@CLOUD8 Huawei@CLOUD8! "ntpq -p"

删除问题行

sh -x login-execute-batch.sh Huawei@CLOUD8 Huawei@CLOUD8! "sed -i '/server 2409:8080:5a0a:500/d' /home/fsp/ntp.conf.test"

确认是否删除

sh -x login-execute-batch.sh Huawei@CLOUD8 Huawei@CLOUD8! "cat /home/fsp/ntp.conf.test | grep 'server 2409:8080:5a0a:500'"

重启查看是否生效

sh -x login-execute-batch.sh Huawei@CLOUD8 Huawei@CLOUD8! "service ntpd restart"

sh -x login-execute-batch.sh Huawei@CLOUD8 Huawei@CLOUD8! "service ntpd status"

sh -x login-execute-batch.sh Huawei@CLOUD8 Huawei@CLOUD8! "ntpq -p"

正式执行

在首节点/home/fsp下执行 (777权限)

查询是否需要修改

sh -x login-execute-batch.sh Huawei@CLOUD8 Huawei@CLOUD8! "cat /etc/ntp.conf | grep 'server 2409:8080:5a0a:500'"

sh -x login-execute-batch.sh Huawei@CLOUD8 Huawei@CLOUD8! "ntpq -p"

先备份

sh -x login-execute-batch.sh Huawei@CLOUD8 Huawei@CLOUD8! "cp /etc/ntp.conf /etc/ntp.conf.bak"

删除问题行

sh -x login-execute-batch.sh Huawei@CLOUD8 Huawei@CLOUD8! "sed -i '/server 2409:8080:5a0a:5004/d' /etc/ntp.conf"

sh -x login-execute-batch.sh Huawei@CLOUD8 Huawei@CLOUD8! "sed -i '/server 2409:8080:5a0a:5005/d' /etc/ntp.conf"

确认是否删除

sh -x login-execute-batch.sh Huawei@CLOUD8 Huawei@CLOUD8! "cat /etc/ntp.conf | grep 'server 2409:8080:5a0a:500'"

重启查看是否生效

sh -x login-execute-batch.sh Huawei@CLOUD8 Huawei@CLOUD8! "service ntpd restart"

sh -x login-execute-batch.sh Huawei@CLOUD8 Huawei@CLOUD8! "service ntpd status"

sh -x login-execute-batch.sh Huawei@CLOUD8 Huawei@CLOUD8! "ntpq -p"

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值