基于expect命令实现
1.安装expect
[root@logstash ~]# yum install -y expect
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Package expect-5.45-14.el7_1.x86_64 already installed and latest version
Nothing to do
[root@logstash ~]#
2.撰写脚本
expect_command.sh
#!/bin/bash
command=$*
host_info=/root/host.info
for ip in $(awk '/^[^#]/{print $1}' $host_info)
do
user=$(awk -v ip=$ip 'ip==$1{print $2}' $host_info)
port=$(awk -v ip=$ip 'ip==$1{print $3}' $host_info)
pass=$(awk -v ip=$ip 'ip==$1{print $4}' $host_info)
expect -c "
spawn ssh -p $port $user@$ip
expect {
\"(yes/no)\" {send \"yes\r\";exp_continue}
\"password:\" {send \"$pass\";exp_continue}
\"$user@*\" {send \"$command\r exit\r\";exp_continue}
}
"
echo "------Execute Successful!------"
done
linux exp_conti

最低0.47元/天 解锁文章
4483

被折叠的 条评论
为什么被折叠?



