批量分发 expect
今天说一说expect 非交互式语言。再说这门语言的时候,首先我的目的是对N台机器进行批量部署 批量执行脚本.
1 先确定分发机器,在机器上 安装 :yum -y install expect
2 干货,直接发命令
[[email protected] ~]$ cat fenfa_sshkey.exp
#!/usr/bin/expect
if { $argc != 2 } {
send_user "usage: expect fenfa_sshkey.exp file host\n"
exit
}
#define var
set file [lindex $argv 0]
set host [lindex $argv 1]
set password "123456"
spawn ssh-copy-id -i $file "-p 52113 [email protected]$host"
expect {
"yes/no" {send "yes\r";exp_continue}
"*password" {send "$password\r"}
}
expect eof
exit -onexit {
send_user "Oldboy say good bye to you!\n"
}
小结:
Expect需要用expect 命令执行,或者.执行。Sh不能执行。
$argv 0 是第一个参数 $argv 1 是第二个参数
[[email protected] ~]$ cat fenfa_sshkey.sh
#!/bin/sh
. /etc/init.d/functions
for ip in `cat iplist`
do
expect fenfa_sshkey.exp ~/.ssh/id_dsa.pub $ip
if [ $? -eq 0 ];then
action "$ip" /bin/true
else
action "$ip" /bin/false
fi
done
[[email protected] ~]$ cat iplist
192.168.74.128
192.168.74.130
小结:
执行fenfa_sshkey.sh看目标authorized_keys 是否已经有key文件。
3 执行./fenfa_sshkey.sh
文件,调用 fenfa_sshkey.exp 脚本
4 如遇故障,发生缓慢等情况:
81 GSSAPIAuthentication no
84 GSSAPIStrictAcceptorCheck no
122 UseDNS no
在/etc/hosts 把主机名加上。
测试连接慢: ssh -v [email protected]
要想到~~~用命令,找命令去测试!!! 关键是思想,我当时发生连接慢,查找了2天。
以后我会多多努力,多多总结。
转载于:https://blog.51cto.com/hexudong/1625872