Hadoop学习_配置互信
作者:互联网
一、配置数据库互信脚本
- 脚本名称auto_auth.sh
[root@allsql01 ~]# cat auto_auth.sh
DEST_USER=$1
PASSWORD=$2
HOSTS_FILE=$3
if [ $# -ne 3 ]; then
echo "Usage:"
echo "$0 remoteUser remotePassword hostsFile"
exit 1
fi
SSH_DIR=~/.ssh
SCRIPT_PREFIX=./tmp
echo ===========================
# 1. prepare directory .ssh
mkdir $SSH_DIR
chmod 700 $SSH_DIR
# 2. generat ssh key
TMP_SCRIPT=$SCRIPT_PREFIX.sh
echo "#!/usr/bin/expect">$TMP_SCRIPT
echo "spawn ssh-keygen -b 1024 -t rsa">>$TMP_SCRIPT
echo "expect *key*">>$TMP_SCRIPT
echo "send \r">>$TMP_SCRIPT
if [ -f $SSH_DIR/id_rsa ]; then
echo "expect *verwrite*">>$TMP_SCRIPT
echo "send y\r">>$TMP_SCRIPT
fi
echo "expect *passphrase*">>$TMP_SCRIPT
echo "send \r">>$TMP_SCRIPT
echo "expect *again:">>$TMP_SCRIPT
echo "send \r">>$TMP_SCRIPT
echo "interact">>$TMP_SCRIPT
chmod +x $TMP_SCRIPT
/usr/bin/expect $TMP_SCRIPT
rm $TMP_SCRIPT
# 3. generat file authorized_keys
cat $SSH_DIR/id_rsa.pub>>$SSH_DIR/authorized_keys
# 4. chmod 600 for file authorized_keys
chmod 600 $SSH_DIR/authorized_keys
echo ===========================
# 5. copy all files to other hosts
for ip in $(cat $HOSTS_FILE)
do
if [ "x$ip" != "x" ]; then
echo -------------------------
TMP_SCRIPT=${SCRIPT_PREFIX}.$ip.sh
# check known_hosts
val=`ssh-keygen -F $ip`
if [ "x$val" == "x" ]; then
echo "$ip not in $SSH_DIR/known_hosts, need to add"
val=`ssh-keyscan $ip 2>/dev/null`
if [ "x$val" == "x" ]; then
echo "ssh-keyscan $ip failed!"
else
echo $val>>$SSH_DIR/known_hosts
fi
fi
echo "copy $SSH_DIR to $ip"
echo "#!/usr/bin/expect">$TMP_SCRIPT
echo "spawn scp -r $SSH_DIR $DEST_USER@$ip:~/">>$TMP_SCRIPT
echo "expect *assword*">>$TMP_SCRIPT
echo "send $PASSWORD\r">>$TMP_SCRIPT
echo "interact">>$TMP_SCRIPT
chmod +x $TMP_SCRIPT
#echo "/usr/bin/expect $TMP_SCRIPT" >$TMP_SCRIPT.do
#sh $TMP_SCRIPT.do&
/usr/bin/expect $TMP_SCRIPT
rm $TMP_SCRIPT
echo "copy done."
fi
done
echo done.
- 在相同目录下编写host脚本,并填写ip,每一行一个ip
[root@allsql01 ~]# cat host
10.10.10.11
10.10.10.12
10.10.10.13
- 语句执行
运行可执行脚本ssh_auth.sh文件,ssh_auth.sh接受三个参数,远程机器用户名、密码和host文件名(相对路径或绝对路径均可)
[root@allsql01 ~]# sh auto_auth.sh root redhat host
===========================
mkdir: cannot create directory ‘/root/.ssh’: File exists
spawn ssh-keygen -b 1024 -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:yXzdlYjlgysWAZlmw8VM4OsKV6EzlNQB6J3HOuG5MY4 root@allsql01
The key's randomart image is:
+---[RSA 1024]----+
| .o++@+ . |
| .. oO o. = . .|
| . .o=o.. o + ..|
| ..+.=o....... |
| .+=oSo... . |
| B= ... |
| .o.=. |
| Eoo. |
| . |
+----[SHA256]-----+
===========================
-------------------------
do_known_hosts: hostkeys_foreach failed: No such file or directory
10.10.10.11 not in /root/.ssh/known_hosts, need to add
copy /root/.ssh to 10.10.10.11
spawn scp -r /root/.ssh root@10.10.10.11:~/
id_rsa 100% 887 1.4MB/s 00:00
id_rsa.pub 100% 227 448.3KB/s 00:00
authorized_keys 100% 227 410.5KB/s 00:00
known_hosts 100% 659 1.3MB/s 00:00
send: spawn id exp6 not open
while executing
"send redhat\r"
(file "./tmp.10.10.10.11.sh" line 4)
copy done.
-------------------------
10.10.10.12 not in /root/.ssh/known_hosts, need to add
copy /root/.ssh to 10.10.10.12
spawn scp -r /root/.ssh root@10.10.10.12:~/
root@10.10.10.12's password:
id_rsa 100% 887 700.4KB/s 00:00
id_rsa.pub 100% 227 183.6KB/s 00:00
authorized_keys 100% 227 182.2KB/s 00:00
known_hosts 100% 1318 1.0MB/s 00:00
copy done.
-------------------------
10.10.10.13 not in /root/.ssh/known_hosts, need to add
copy /root/.ssh to 10.10.10.13
spawn scp -r /root/.ssh root@10.10.10.13:~/
root@10.10.10.13's password:
id_rsa 100% 887 704.2KB/s 00:00
id_rsa.pub 100% 227 154.6KB/s 00:00
authorized_keys 100% 227 175.8KB/s 00:00
known_hosts 100% 1977 1.5MB/s 00:00
copy done.
done.
[root@allsql01 ~]#
[root@allsql01 ~]#
[root@allsql01 ~]#
[root@allsql01 ~]#
[root@allsql01 ~]# ssh 10.10.10.11 date; ssh 10.10.10.12 date; ssh 10.10.10.13 date;
Sat Mar 6 21:28:16 CST 2021
Sat Mar 6 21:28:16 CST 2021
Sat Mar 6 21:28:16 CST 2021
参考文档:
https://blog.csdn.net/lotomer/article/details/8814032
二、编辑脚本,在所有机器上执行
- 编辑脚本在本地执行的脚本boot.sh
[root@allsql01 ~]# cat boot.sh
#!/bin/bash
SERVERS="10.10.10.12 10.10.10.13"
PASSWORD=redhat
BASE_SERVER=10.10.10.11
for SERVER in $SERVERS
do
scp install.sh root@$SERVER:/root
ssh root@$SERVER /root/install.sh
done
- 在远端执行的脚本
注意,可以在10.10.10.11机器上配置自己的局域网网络yum源,然后通过自己的yum源来下载安装包。
[root@allsql01 ~]# cat install.sh
#!/bin/bash
BASE_SERVER=10.10.10.11
#yum install -y wget httpd
yum install -y expect
#wget $BASE_SERVER/soft/jdk-7u45-linux-x64.tar.gz
#tar -zxvf jdk-7u45-linux-x64.tar.gz -C /usr/local
#cat >> /etc/profile << EOF
#export JAVA_HOME=/usr/local/jdk1.7.0_45
#export PATH=\$PATH:\$JAVA_HOME/bin
#EOF
ssh 10.10.10.11 date; ssh 10.10.10.12 date; ssh 10.10.10.13 date;
[root@allsql01 ~]#
- 执行结果:
root@allsql01 ~]# sh boot1.sh install.sh 100% 364 302.0KB/s 00:00 Loaded plugins: fastestmirror, langpacks Loading mirror speeds from cached hostfile Package expect-5.45-14.el7_1.x86_64 already installed and latest version Nothing to do Sat Mar 6 21:41:18 CST 2021 Sat Mar 6 21:41:18 CST 2021 Sat Mar 6 21:41:18 CST 2021 install.sh 100% 364 160.5KB/s 00:00 Loaded plugins: fastestmirror, langpacks Loading mirror speeds from cached hostfile Package expect-5.45-14.el7_1.x86_64 already installed and latest version Nothing to do Sat Mar 6 21:41:18 CST 2021 Sat Mar 6 21:41:18 CST 2021 Sat Mar 6 21:41:19 CST 2021
标签:TMP,00,SCRIPT,配置,Hadoop,echo,互信,root,ssh 来源: https://blog.51cto.com/lishiyan/2649797