fix: enhancement cp ssh key

This commit is contained in:
2024-05-22 14:51:15 +08:00
parent 607c1e4daf
commit 357ff9a650
2 changed files with 16 additions and 9 deletions

View File

@@ -3,7 +3,7 @@
tooldir=/usr/local/bin tooldir=/usr/local/bin
toollist="zip unzip" toollist="zip unzip"
#分发到各个节点,这里分发到nehosts文件中的主机中. # distribute to hosts in file nehosts
while read line while read line
do do
if [[ "$line" =~ ^[^[:space:]]*# || -z "$line" ]]; then if [[ "$line" =~ ^[^[:space:]]*# || -z "$line" ]]; then

View File

@@ -1,29 +1,36 @@
#!/bin/bash #!/bin/bash
# 判断id_rsa密钥文件是否存在 # if exist id_rsa
if [ ! -f ~/.ssh/id_rsa ];then if [ ! -f ~/.ssh/id_rsa ];then
ssh-keygen -t rsa -P "" -f ~/.ssh/id_rsa ssh-keygen -t rsa -P "" -f ~/.ssh/id_rsa
else else
echo "id_rsa has created ..." echo "id_rsa has created ..."
fi fi
#分发到各个节点,这里分发到nehosts文件中的主机中. # distribute to hosts in file nehosts
while read line while read line
do do
# ignore comment or null line
if [[ "$line" =~ ^[^[:space:]]*# || -z "$line" ]]; then if [[ "$line" =~ ^[^[:space:]]*# || -z "$line" ]]; then
continue continue
fi fi
user=`echo $line | cut -d " " -f 2`
ip=`echo $line | cut -d " " -f 1` ip=`echo $line | cut -d " " -f 1`
user=`echo $line | cut -d " " -f 2`
passwd=`echo $line | cut -d " " -f 3` passwd=`echo $line | cut -d " " -f 3`
# !!! to remove ~/.ssh, all old authorized key will be lost
expect <<EOF expect <<EOF
set timeout 10 set timeout 10
spawn ssh-copy-id -f $user@$ip spawn ssh $user@$ip "sudo rm -r ~/.ssh"
expect { expect {
"yes/no" { send "yes\n";exp_continue } "yes/no" { send "yes\n";exp_continue }
"password" { send "$passwd\n" } "password:" { send "$passwd\n" }
} }
expect "password" { send "$passwd\n" } spawn ssh-copy-id -f $user@$ip
expect {
"password:" { send "$passwd\n" }
}
expect eof
EOF EOF
done < nehosts done < nehosts