<pre name="code" class="python">if [ ! $# -eq 2 ] ;then
echo "请输入用户密码以空格离开"
exit
else
ssh-keygen -t rsa
#用户名
uname="
<pre name="code" class="python">if [ ! $# -eq 2 ] ;then
echo "请输入用户密码以空格离开"
exit
else
ssh-keygen -t rsa
#用户名
uname="$1"
#密码
passwd="$2"
fi
#履行检测并安装expect模块
ep=`rpm -qa | grep expect`
if [ -z $ep ] ; then
echo "检测到你的体系,没有安装expect模块,预备履行安装"
sleep 2
yum install -y expect
else
echo "已安装expect模块,3秒后,开端履行ssh文件同步"
sleep 3
fi
#公钥无ssh认证模块
cat hosts | while read host
do
echo "当前正在向$host上传输ssh文件"
expect <<EOF
spawn scp -r .ssh/ $host:/$uname
expect {
"*yes/no" {send "yes\r" ;exp_continue}
"*password:" {send "$passwd\r" ;exp_continue }
}
EOF
echo "当前正在$host上进行公钥认证....."
sleep 2
expect <<EOF
spawn ssh-copy-id -i .ssh/id_rsa.pub $host
expect {
"*yes/no" {send "yes\r" ;exp_continue}
"*password:" {send "$passwd\r" ;exp_continue }
}
EOF
echo "认证胜利...."
done
#切换root权限进行hosts文件分发
echo "同步本机的hosts文件到各个机器上"
sleep 1
#同步本机的hosts文件到其他各个机器上
host文件内容:
192.168.32.84上履行这个文件,
demo:/root# cat hosts
192.168.32.142
192.168.32.143
此时从192.168.32.84上发起,可以远程ssh :
反过来在192.168.32.143上履行就不可以:
wze-haproxy:/root# ssh [email protected] date
[email protected]"s password:
"
#密码
passwd=""
fi
#履行检测并安装expect模块
ep=`rpm -qa | grep expect`
if [ -z $ep ] ; then
echo "检测到你的体系,没有安装expect模块,预备履行安装"
sleep 2
yum install -y expect
else
echo "已安装expect模块,3秒后,开端履行ssh文件同步"
sleep 3
fi
#公钥无ssh认证模块
cat hosts | while read host
do
echo "当前正在向$host上传输ssh文件"
expect <<EOF
spawn scp -r .ssh/ $host:/$uname
expect {
"*yes/no" {send "yes\r" ;exp_continue}
"*password:" {send "$passwd\r" ;exp_continue }
}
EOF
echo "当前正在$host上进行公钥认证....."
sleep 2
expect <<EOF
spawn ssh-copy-id -i .ssh/id_rsa.pub $host
expect {
"*yes/no" {send "yes\r" ;exp_continue}
"*password:" {send "$passwd\r" ;exp_continue }
}
EOF
echo "认证胜利...."
done
#切换root权限进行hosts文件分发
echo "同步本机的hosts文件到各个机器上"
sleep 1
#同步本机的hosts文件到其他各个机器上
host文件内容:
192.168.32.84上履行这个文件,
demo:/root# cat hosts
192.168.32.142
192.168.32.143
此时从192.168.32.84上发起,可以远程ssh :
反过来在192.168.32.143上履行就不可以:
wze-haproxy:/root# ssh [email protected] date
[email protected]"s password:
<pre name="code" class="python">if [ ! $# -eq 2