SSH無パスワード登録認証スクリプトを一括実現します.
5161 ワード
SSH無パスワード登録認証スクリプトを一括実現します.
問題の背景
linux間でsshを使用するために暗号を必要としないために、デジタル署名RSAまたはDSAを使用して実行することができる.主にssh-key-genを使って実現します.
1.ssh-key-genを通じてpublic and prvate keysを作成する
2.ssh-copy-idを使ってpublic keyをコピーしてリモートホストに行く.
3.パスワードなしでリモートホストに登録する
スクリプトの例
しかし、大規模なクラスタに対しては、人工的にssh-key-genを使用してkeyを生成し、ssh-copy-indを使用すると、時間がかかります.N台の本体については、N回のssh-key-gen、N*N回のssh-copy-i-dが必要です.
このために、SSH key-genシナリオを書きました.脚本は4つのファイルを含みます.master.sh、keygen_slaave.sh、hosts.com、slaaves.com nf
使い方
使い方は簡単です.これらの4つのファイルをメインノードにコピーして、Hosts.co nfとslaaves.com nfを設定して、keygen_を実行します.マスター.shで結構です
keygen.マスターノードでmaster.shを実行します.
問題の背景
linux間でsshを使用するために暗号を必要としないために、デジタル署名RSAまたはDSAを使用して実行することができる.主にssh-key-genを使って実現します.
1.ssh-key-genを通じてpublic and prvate keysを作成する
2.ssh-copy-idを使ってpublic keyをコピーしてリモートホストに行く.
3.パスワードなしでリモートホストに登録する
スクリプトの例
しかし、大規模なクラスタに対しては、人工的にssh-key-genを使用してkeyを生成し、ssh-copy-indを使用すると、時間がかかります.N台の本体については、N回のssh-key-gen、N*N回のssh-copy-i-dが必要です.
このために、SSH key-genシナリオを書きました.脚本は4つのファイルを含みます.master.sh、keygen_slaave.sh、hosts.com、slaaves.com nf
使い方
使い方は簡単です.これらの4つのファイルをメインノードにコピーして、Hosts.co nfとslaaves.com nfを設定して、keygen_を実行します.マスター.shで結構です
keygen.マスターノードでmaster.shを実行します.
[root@localhost ~]# cat keygen_master.sh
#!/bin/sh
this="$0"
while [ -h "$this" ]; do
ls=`ls -ld "$this"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '.*/.*' > /dev/null; then
this="$link"
else
this=`dirname "$this"`/"$link"
fi
done
# init base path
base=`dirname "$this"`
script=`basename "$this"`
base=`cd "$base"; pwd`
this="$base/$script"
slavesh="keygen_slave.sh"
slavescript="$base/$slavesh"
slaves="$base/slaves.conf"
hosts="$base/hosts.conf"
# install ssh
yum install -y openssh* expect
eval `ssh-agent`
if [ ! -s ~/.ssh/id_dsa ]; then
expect -c "
spawn ssh-keygen -t dsa
expect {
\"*y/n*\" {send \"y\r\"; exp_continue}
\"*key*\" {send \"\r\"; exp_continue}
\"*passphrase*\" {send \"\r\"; exp_continue}
\"*again*\" {send \"\r\";}
}
"
fi
ssh-add $HOME/.ssh/id_dsa # Add private key
# batch ssh
if [ -s $hosts ]; then
for p in $(cat $hosts) #
do
username=$(echo "$p"|cut -f1 -d":") # Get username
ip=$(echo "$p"|cut -f2 -d":") # Get ip
password=$(echo "$p"|cut -f3 -d":") # Get password
id=$HOME/.ssh/id_dsa.pub
echo "ssh-copy-id -i $id $username@$ip -P $password"
# ssh-copy-id
expect -c "
spawn ssh-copy-id -i $id $username@$ip
expect {
\"*yes/no*\" {send \"yes\r\"; exp_continue}
\"*password*\" {send \"$password\r\"; exp_continue}
\"*Password*\" {send \"$password\r\";}
}
"
done
fi
# dispath
if [ -s $slaves ]; then
for p in $(cat $slaves) #
do
username=$(echo "$p"|cut -f1 -d":") # Get username
ip=$(echo "$p"|cut -f2 -d":") # Get ip
password=$(echo "$p"|cut -f3 -d":") # Get password
id=$HOME/.ssh/id_dsa.pub
ssh $username@$ip 'yum install -y openssh*'
echo "scp $slavescript $hosts $username@$ip:~/ -P $password"
# Dispath to clients
expect -c "
spawn scp $slavescript $hosts $username@$ip:~/
expect {
\"*yes/no*\" {send \"yes\r\"; exp_continue}
\"*password*\" {send \"$password\r\"; exp_continue}
\"*Password*\" {send \"$password\r\";}
}
"
# ssh to clients
echo "ssh $username@$ip 'sh $HOME/keygen_slave.sh'"
ssh $username@$ip 'sh $HOME/keygen_slave.sh'
done
fi
keygen.slaave.shはすべてノードから実行されます.[root@localhost ~]# cat keygen_slave.sh
#!/bin/sh
this="$0"
while [ -h "$this" ]; do
ls=`ls -ld "$this"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '.*/.*' > /dev/null; then
this="$link"
else
this=`dirname "$this"`/"$link"
fi
done
# init base path
base=`dirname "$this"`
script=`basename "$this"`
base=`cd "$base"; pwd`
this="$base/$script"
hosts="$base/hosts.conf"
echo $base
echo $script
echo $this
echo $hosts
# install ssh
yum install -y openssh* expect
eval `ssh-agent`
if [ ! -s ~/.ssh/id_dsa ]; then
expect -c "
spawn ssh-keygen -t dsa
expect {
\"*y/n*\" {send \"y\r\"; exp_continue}
\"*key*\" {send \"\r\"; exp_continue}
\"*passphrase*\" {send \"\r\"; exp_continue}
\"*again*\" {send \"\r\";}
}
"
fi
ssh-add $HOME/.ssh/id_dsa # Add private key
# batch ssh
if [ -s $hosts ]; then
for p in $(cat $hosts) #
do
username=$(echo "$p"|cut -f1 -d":") # Get username
ip=$(echo "$p"|cut -f2 -d":") # Get ip
password=$(echo "$p"|cut -f3 -d":") # Get password
id=$HOME/.ssh/id_dsa.pub
echo $username
echo $ip
echo $password
echo $id
# ssh-copy-id
expect -c "
spawn ssh-copy-id -i $id $username@$ip
expect {
\"*yes/no*\" {send \"yes\r\"; exp_continue}
\"*password*\" {send \"$password\r\"; exp_continue}
\"*Password*\" {send \"$password\r\";}
}
"
done
fi
Hosts.comでは、ホスト(メインノード+スレーブノード)をすべて設定し、フォーマットはユーザ名:ホストIP:ユーザパスワードusername:master_ip:passwd
username:client1_ip:passwd
username:client2_ip:passwd
#root:localhost:000000
slaaves.comでは、ホストからのすべてのユーザー名を設定します.ホストIP:ユーザパスワードusername:client1_ip:passwd
username:client2_ip:passwd
#root:192.168.1.12:000000