本文主要是介绍hadoop批量创建互信脚本,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1、hadoop安装的用户名和密码要统一
2、将IP地址写入一个文件:
[root@vmhost hadoop]# cat ip.txt
192.168.1.101
192.168.1.102
192.168.1.103
3、互信脚本:
#!/bin/sh
DEST_USER=$1
PASSWORD=$2
HOSTS_FILE=$3
if [ $# -ne 3 ]; thenecho "Usage:"echo "$0 remoteUser remotePassword hostsFile"exit 1
fiSSH_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 ]; thenecho "expect *verwrite*">>$TMP_SCRIPTecho "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_SCRIPTchmod +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)
doif [ "x$ip" != "x" ]; thenecho -------------------------TMP_SCRIPT=${SCRIPT_PREFIX}.$ip.sh# check known_hostsval=`ssh-keygen -F $ip`if [ "x$val" == "x" ]; thenecho "$ip not in $SSH_DIR/known_hosts, need to add"val=`ssh-keyscan $ip 2>/dev/null`if [ "x$val" == "x" ]; thenecho "ssh-keyscan $ip failed!"elseecho $val>>$SSH_DIR/known_hostsfifiecho "copy $SSH_DIR to $ip"echo "#!/usr/bin/expect">$TMP_SCRIPTecho "spawn scp -r $SSH_DIR $DEST_USER@$ip:~/">>$TMP_SCRIPTecho "expect *assword*">>$TMP_SCRIPTecho "send $PASSWORD\r">>$TMP_SCRIPTecho "interact">>$TMP_SCRIPTchmod +x $TMP_SCRIPT#echo "/usr/bin/expect $TMP_SCRIPT" >$TMP_SCRIPT.do#sh $TMP_SCRIPT.do&/usr/bin/expect $TMP_SCRIPTrm $TMP_SCRIPTecho "copy done." fi
doneecho done.
4、执行脚本:chmod +x auto_ssh.sh && ./auto_ssh.sh username passwd
这篇关于hadoop批量创建互信脚本的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!