推密钥脚本

#!/bin/bash
source /etc/init.d/functions

function push_public_key(){
yum install -y sshpass &>/dev/null
if [ ! -f ~/.ssh/id_dsa.pub ];then
        ssh-keygen -t dsa -P '' -f ~/.ssh/id_dsa >/dev/null 2>&1
fi
for ip in $@;do
        ping -W1 -c1 $ip &>/dev/null
        if [ $? -eq 0 ];then
                sshpass -p '1' ssh-copy-id -o StrictHostKeyChecking=no -i ~/.ssh/id_dsa.pub root@$ip &>/dev/null
                action "主机:$ip" /bin/true
        else
                action "主机:$ip" /bin/false
        fi
done
}
function hhh(){
for ipa in $@;do
scp /root/ssh.sh $ipa:/root/ssh.sh
ssh $ipa "bash /root/ssh.sh $master_ip ${slave_ip[*]}"
done
}
read -p "请输入master的IP地址:" master_ip
read -p "请输入slave的IP地址(多台使用空格开):" -a slave_ip
push_public_key $master_ip ${slave_ip[*]}
hhh $master_ip ${slave_ip[*]}
exit
source /etc/init.d/functions

命令action

食用方法

#!/bin/bash
source /etc/init.d/functions

function push_public_key(){
yum install -y sshpass &>/dev/null
if [ ! -f ~/.ssh/id_dsa.pub ];then
        ssh-keygen -t dsa -P '' -f ~/.ssh/id_dsa >/dev/null 2>&1
fi
for ip in $@;do
        ping -W1 -c1 $ip &>/dev/null
        if [ $? -eq 0 ];then
                sshpass -p '1' ssh-copy-id -o StrictHostKeyChecking=no -i ~/.ssh/id_dsa.pub root@$ip &>/dev/null
                action "主机:$ip" /bin/true
        else
                action "主机:$ip" /bin/false
        fi
done
}
function hhh(){
for ipa in $@;do
scp /root/ssh.sh $ipa:/root/ssh.sh
ssh $ipa "bash /root/ssh.sh ${slave_ip[*]}"
done
}
read -p "请输入slave的IP地址(多台使用空格开):" -a slave_ip
push_public_key ${slave_ip[*]}
hhh ${slave_ip[*]}
exit
[root@db01 ~]# cat ssh.sh 
#!/bin/bash
source /etc/init.d/functions

yum install -y sshpass &>/dev/null
if [ ! -f ~/.ssh/id_dsa.pub ];then
        ssh-keygen -t dsa -P '' -f ~/.ssh/id_dsa >/dev/null 2>&1
fi
for ip in $@;do
        ping -W1 -c1 $ip &>/dev/null
        if [ $? -eq 0 ];then
                sshpass -p '1' ssh-copy-id -o StrictHostKeyChecking=no -i ~/.ssh/id_dsa.pub root@$ip &>/dev/null
                action "主机:$ip" /bin/true
        else
                action "主机:$ip" /bin/false
        fi
done
[root@db01 ~]# sh ttt.sh 
请输入master的IP地址:172.16.1.51
请输入slave的IP地址(多台使用空格开):172.16.1.52 172.16.1.53 172.16.1.54
主机:172.16.1.51                                          [  OK  ]
主机:172.16.1.52                                          [  OK  ]
主机:172.16.1.53                                          [  OK  ]
主机:172.16.1.54                                          [  OK  ]
ssh.sh                                                 100%  501     1.3MB/s   00:00    
主机:172.16.1.51 [  OK  ]
主机:172.16.1.52 [  OK  ]
主机:172.16.1.53 [  OK  ]
主机:172.16.1.54 [  OK  ]
ssh.sh                                                 100%  501   691.7KB/s   00:00    
主机:172.16.1.51 [  OK  ]
主机:172.16.1.52 [  OK  ]
主机:172.16.1.53 [  OK  ]
主机:172.16.1.54 [  OK  ]
ssh.sh                                                 100%  501   774.8KB/s   00:00    
主机:172.16.1.51 [  OK  ]
主机:172.16.1.52 [  OK  ]
主机:172.16.1.53 [  OK  ]
主机:172.16.1.54 [  OK  ]
ssh.sh                                                 100%  501   882.2KB/s   00:00    
主机:172.16.1.51 [  OK  ]
主机:172.16.1.52 [  OK  ]
主机:172.16.1.53 [  OK  ]
主机:172.16.1.54 [  OK  ]

加强版

#!/bin/bash
source /etc/init.d/functions

function push_public_key(){
yum install -y sshpass &>/dev/null
if [ ! -f ~/.ssh/id_dsa.pub ];then
        ssh-keygen -t dsa -P '' -f ~/.ssh/id_dsa >/dev/null 2>&1
fi
for ip in $@;do
        ping -W1 -c1 $ip &>/dev/null
        if [ $? -eq 0 ];then
                sshpass -p '1' ssh-copy-id -o StrictHostKeyChecking=no -i ~/.ssh/id_dsa.pub root@$ip &>/dev/null
                action "主机:$ip" /bin/true
        else
                action "主机:$ip" /bin/false
        fi
done
}
function hhh(){
for ipa in $@;do
scp ~/.ssh/* $ipa:~/.ssh/
done
}
slave_ip=("172.16.1.51" "172.16.1.52" "172.16.1.53" "172.16.1.54")
#read -p "请输入slave的IP地址(多台使用空格开):" -a slave_ip
push_public_key ${slave_ip[*]}
hhh ${slave_ip[*]}
exit
posted @ 2023-10-09 08:33  普里莫  阅读(4)  评论(0编辑  收藏  举报