免密登录批量添加ssh-key

新建一个.sh文件
    vi /root/batch_ssh.sh
复制下面内容(SERVERS服务器名称或者地址,PASSWORD服务器密码)
#!/bin/bash

SERVERS="hadoop01 hadoop02"
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


赋予权限该脚本执行权限
    chmod 711 /root/batch_ssh.sh
执行脚本
    ./root/batch_ssh.sh

你可能感兴趣的:(Linux)