一个shell脚本实现linux集群所有机器免密钥登录

1,先安装expect
yum install expect

2.生成密钥
ssh-keygen(注,一路回车,不用管)

3.修改host文件 /etc/hosts

4.编写shell脚本

#!/bin/bash
SERVERS="flux01 flux02 flux03 flux04 flux05  flux06  flux07  flux08  flux09"
PASSWORD=root
auto_ssh_copy_id() {
    expect -c "set timeout -1;
        spawn ssh-copy-id $1;
        expect {
            *(yes/no)* {send -- yes\r;exp_continue;}
            *assword:* {send -- $2\r;exp_continue;}
            eof        {exit 0;}
        }";
}

ssh_copy_id_to_all() {
    for SERVER in $SERVERS
    do
        auto_ssh_copy_id $SERVER $PASSWORD
    done
}

ssh_copy_id_to_all

你可能感兴趣的:(linux)