Linux 星状SSH免密脚本

来源:互联网 发布:pubmed数据库 编辑:程序博客网 时间:2024/05/10 05:47


    话不多说,直接贴脚本。

    使用前提:1.manager内的 /etc/hosts文件已包含当前网段需要做免密的所有主机;2.所有主机需先安装expect和scp工具,安装可使用yum -y install expect openssh-clients。

    主机数目较多的话建议执行两次以上脚本。

    附加:将网段内存活主机命名写入/etc/hosts文件脚本。


#!/bin/bash

#获取局域网存活主机
NAME=sp
IP_D=192.168.1.0
namp() {
n=1;
for i in $(nmap -oG - -sP $IP_D/24|grep `ip a|grep eth0$|awk -F '[ /]+' '{print $3}'|awk -F '.' '{print $1"."$2}'`|awk 'NR>2 {print $2}')
    do echo -e "$i\t$NAME$n" >>/etc/hosts
       ((n++))
done
};namp

执行后效果如下

注:管理机需如图所示放置在首行(第三行)。博主的管理机IP是 192.168.1.111


以下为脚本,建议放置在用户家目录下执行。执行前需根据自身环境修改IP_D(网段,前三位),password(用户密码)。

PS:所有主机需先切换为相同用户,且登录密码相同才可使用。

PPS:脚本执行成功后会在每台主机的/tmp下产生脚本文件,可自行清理。

#!/bin/bash
[ -f /etc/init.d/functions ] && . /etc/init.d/functions
USER=`whoami`
IP_D=192.168.1
password="123456"

function INSTALL_0() {
rpm -qa|grep expect
[ $? -ne 0 ] && yum -y install expect
rpm -qa|grep openssh-clients
[ $? -ne 0 ] && yum -y install openssh-clients
return
};INSTALL_0

function KEY_SH() {
cat <<EOF >/tmp/key.sh
#!/bin/bash
USER="$USER"
IP_D="$IP_D"
password="$password"
function KEY() {
SSH_DIR=\$HOME/.ssh
[ -d \$SSH_DIR ] && rm -rf \$SSH_DIR
echo -e "\n"|ssh-keygen -t rsa -N ""
};KEY

[ -e /tmp/keys_tem ] && cat /tmp/keys_tem>>\$HOME/.ssh/id_rsa.pub

function LOGIN() {
for IP in \`cat /etc/hosts|grep \$IP_D|awk '{print \$1}'\`;do
IPADDR=\`ip a|grep eth0\$|awk '{print \$2}'|awk -F '/' '{print \$1}'\`
if [ \$IP = \$IPADDR ];then
expect -c "
           set timeout 1;
           spawn ssh \$HOME@\$IP -p 22  ;
           expect {
               yes/no { send \"yes\r\"; exp_continue }
               *assword* { send \"\$password\r\" }
           } ;
           expect \$USER@* {send \"cat /tmp/keys_tem>>\$HOME/.ssh/id_rsa.pub \r\" } ;
           expect \$USER@* {send \"touch \$HOME/zyj \r\" } ;
           expect eof ;
       "
fi
done
}

function SCP_KEY() {
IP=\$(sed -n "/\<\`ip a|grep eth0\$|awk -F '[ /]+' '{print \$3}'\`\>/{n;p;}" /etc/hosts|awk '{print \$1}')
expect -c "
           set timeout 1;
           spawn scp \$HOME/.ssh/id_rsa.pub \$USER@\$IP:/tmp/keys_tem ;
           expect {
               yes/no { send \"yes\r\"; exp_continue }
               *assword* { send \"\$password\r\" }
           } ;
           expect 100% ;
           expect eof ;
       "
};SCP_KEY

while :
do [ -e \$HOME/.ssh ] && break || KEY;LOGIN;SCP_KEY
done
EOF
};KEY_SH

while :
do [ -e /tmp/key.sh ] && action "KEY_SH function status:" /bin/true ; break || KEY_SH
done

function CP_KEYS() {
cat<<EOF >/tmp/scp_keys.sh
#!/bin/bash
USER="$USER"
IP_D="$IP_D"
password="$password"
cp -f \$HOME/.ssh/id_rsa.pub \$HOME/.ssh/authorized_keys
function SCP_KEY() {
#for IP in \`cat /etc/hosts|grep \$IP_D|awk '{print \$1}'\`;do
IP=\`awk 'NR==3{print \$1}' /etc/hosts\`
#IPADDR=\`ip a|grep 255|awk '{print \$2}'|awk -F '/' '{print \$1}'\`
#if [ \$IP != \$IPADDR ];then
expect -c "
           set timeout 1;
           spawn scp \$HOME/.ssh/id_rsa.pub \$USER@\$IP:\$HOME/.ssh/authorized_keys ;
           expect {
               yes/no { send \"yes\r\"; exp_continue }
               *assword* { send \"\$password\r\" }
           } ;
           expect 100% ;
           expect eof ;
       "
};SCP_KEY
EOF
};CP_KEYS

while :
do [ -e /tmp/scp_keys.sh ] && action "CP_KEYS function status:" /bin/true ; break || SCP_KEY
done

function KEY() {
SSH_DIR=$HOME/.ssh
[ -d $SSH_DIR ] && rm -rf $SSH_DIR
echo -e "\n"|ssh-keygen -t rsa -N ""
};KEY

function SCP_hosts() {
IPADDR=`ip a|grep 255|awk '{print $2}'|awk -F '/' '{print $1}'`
for IP in `cat /etc/hosts|grep $IP_D|awk '{print $1}'`;do
if [ $IP != $IPADDR ];then
expect -c "
           set timeout 1;
           spawn scp /etc/hosts $USER@$IP:/etc/hosts ;
           expect {
               yes/no { send \"yes\r\"; exp_continue }
               *assword* { send \"$password\r\" }
           } ;
           expect 100% ;
           expect eof ;
   "
fi
done
};SCP_hosts

function SCP_SH() {
IPADDR=`ip a|grep 255|awk '{print $2}'|awk -F '/' '{print $1}'`
for IP in `cat /etc/hosts|grep $IP_D|awk '{print $1}'`;do
if [ $IP != $IPADDR ];then
expect -c "
           set timeout 1;
           spawn scp /tmp/key.sh $USER@$IP:/tmp/key.sh ;
           expect {
               yes/no { send \"yes\r\"; exp_continue }
               *assword* { send \"$password\r\" }
           } ;
           expect 100% ;
           expect eof ;
       "
fi
done
};SCP_SH

function SCP_KEY() {
IP=$(sed -n "/\<`ip a|grep eth0$|awk -F '[ /]+' '{print $3}'`\>/{n;p;}" /etc/hosts|awk '{print $1}')
expect -c "
           set timeout 1;
           spawn scp $HOME/.ssh/id_rsa.pub $USER@$IP:/tmp/keys_tem ;
           expect {
               yes/no { send \"yes\r\"; exp_continue }
               *assword* { send \"$password\r\" }
           } ;
           expect 100% ;
           expect eof ;
       "
};SCP_KEY

function LOGIN() {
IPADDR=`ip a|grep 255|awk -F '[ /]+' '{print $3}'`
SIG=0
for IP in `cat /etc/hosts|grep $IP_D|awk '{print $1}'`;do
if [ $IP != $IPADDR ];then
expect -c "
           set timeout 1;
           spawn ssh $USER@$IP -p 22  ;
           expect {
               yes/no { send \"yes\r\"; exp_continue }
               *assword* { send \"$password\r\" }
           } ;
           expect $USER@* {send \"sh /tmp/key.sh \r\" } ;
           expect 100% ;
           expect eof ;
       "
fi
done
};LOGIN

function SCP_sh() {
IP=`tail -1 /etc/hosts|awk '{print $1}'`
expect -c "
           set timeout 1;
           spawn scp /tmp/scp_keys.sh $USER@$IP:/tmp/scp_keys.sh ;
           expect {
               yes/no { send \"yes\r\"; exp_continue }
               *assword* { send \"$password\r\" }
           } ;
           expect 100% ;
           expect eof ;
       "
};SCP_sh

function LOGIN_key() {
IP=`tail -1 /etc/hosts|awk '{print $1}'`
expect -c "
           set timeout 1;
           spawn ssh $USER@$IP -p 22  ;
           expect {
               yes/no { send \"yes\r\"; exp_continue }
               *assword* { send \"$password\r\" }
           } ;
           expect $USER@* {send \"sh /tmp/scp_keys.sh\r\" } ;
           expect 100% ;
           expect eof ;
       "
};LOGIN_key

while :
do [ -e $HOME/.ssh/authorized_keys ] && break || LOGIN_key
done

function SCP_au() {
IPADDR=`ip a|grep 255|awk '{print $2}'|awk -F '/' '{print $1}'`
for IP in `cat /etc/hosts|grep $IP_D|awk '{print $1}'`;do
if [ $IP != $IPADDR ];then
expect -c "
           set timeout 1;
           spawn scp $HOME/.ssh/authorized_keys $USER@$IP:$HOME/.ssh/authorized_keys ;
           expect {
               yes/no { send \"yes\r\"; exp_continue }
               *assword* { send \"$password\r\" }
           } ;
           expect 100% ;
           expect eof ;
   "
fi
done
};SCP_au


转载请标明出处。

原创粉丝点击