为什么80%的码农都做不了架构师?>>>
- 使用说明: 1.ssh.sh为单节点无密码登录脚本; 2.ssh1.sh为集群无密码登录脚本; 3.将ssh与ssh1放到同一节点上,运行ssh2脚本; 4.整体脚本中expect需要提前安装tcl-与expect-,目前此脚本在外网部署已经写好;在内网安装时,需要手动下载expect-*进行安装,命令为;yum install expect -y 5.所有节点需要有/root/Desktop文件夹
##ssh.sh
#!/bin/bash
#fn_install_ssh(){
#HOST_NAME="cloudera-08"
/usr/bin/expect <<-EOF
spawn ssh-keygen -t rsa -P ""
expect "*rsa):" {
send "\n"
expect "*y/n)?" {send "y\n"}
}
expect eof
EOF
sleep 1
cd /root/.ssh/
cat id_rsa.pub >> authorized_keys
chmod 700 authorized_keys
#}
##ssh1.sh
#!/bin/bash
HOST_NAME=("ab-01" "ab-02" "ab-03" "ab-04" "ab-05" "ab-06" "ab-07")
user="root"
file_need_dir="/root/Desktop/"
file_ssh="ssh.sh"
PASSWORD="123456"
ssh_dir="/root/.ssh/"
author="authorized_keys"
keys="authorized_keysab*"
for hostname in ${HOST_NAME[@]}
do
yum install expect -y
sleep 2
/usr/bin/expect <<-EOF
spawn scp /root/Desktop/ssh.sh $user@$hostname:/root/Desktop/
expect {
"*s/no)?" {send "yes\r"
expect "*ssword:" {send "$PASSWORD\r"}
}
"*ssword:" {send "$PASSWORD\n\r"}
}
expect eof
EOF
done
for hostname in ${HOST_NAME[@]}
do
/usr/bin/expect <<-EOF
spawn ssh $hostname
expect {
"*yes/no)?" {send "yes\n\r"
expect "*ssword:" {send "$PASSWORD\n\r"}
}
"*ssword:" {send "$PASSWORD\n\r"}
}
expect "*#"
send "cd /root/Desktop\n\r"
expect "*#"
send "./ssh.sh\n\r"
expect eof
EOF
done
for hostname in ${HOST_NAME[@]}
do
/usr/bin/expect <<-EOF
spawn ssh $hostname
expect {
"*yes/no)?" {send "yes\n\r"
expect "*ssword:" {send "$PASSWORD\n\r"}
}
"*ssword:" {send "$PASSWORD\n\r"}
}
expect "*#" {send "scp -r $ssh_dir$author $user@${HOST_NAME[0]}:$ssh_dir$author${hostname}\n\r"
expect {
"*yes/no)?" {send "yes\n\r"
expect "*ssword:" {send "$PASSWORD\n\r"}
}
"*ssword:" {send "$PASSWORD\n\r"}
}
}
expect eof
EOF
done
echo "*****************************·Ö·¢Íê³É**********************************"
/usr/bin/expect <<-EOF
spawn ssh ${HOST_NAME[0]}
expect {
"*no)?" {send "yes\n"
expect "*word:" {send "$PASSWORD\n"}
}
"*word:" {send "$PASSWORD\n\r"}
}
expect "*#"
send "cat $ssh_dir$keys >> $ssh_dir$author\n\r"
interact
expect eof
EOF
sleep 3
echo "********************ÈÚºÏÍê³É***************************"
for hostname in ${HOST_NAME[@]}
do
/usr/bin/expect <<-EOF
spawn ssh ${HOST_NAME[0]}
expect {
"*no)?" {send "yes\n\r"
expect "*word:" {send "$PASSWORD\n\r"}
}
"*word:" {send "$PASSWORD\n\r"}
}
expect {
"*#" { send "scp -r $ssh_dir$author $user@$hostname:$ssh_dir\n\r"
expect {
"*no)?"
{send "yes\n\r"
expect "*ssword:" {send "$PASSWORD\n\r"}
}
"*ssword:" {send "$PASSWORD\n\r"}
}
}
}
expect eof
EOF
done