简单的SSH免密码验证批量分发脚本

 1 [zhangly@CN-BJ-720-1-Hadoop-Master script]$ cat ssh_key_v1.5.sh 
 2 #!/bin/bash
 3 #Date:2013-08-29
 4 #Author:ZhangLuYa
 5 #Version 1.5
 6   
 7    #define var
 8    work="/work/script"
 9    ip_list="${work}/ip_all.txt"
10    if [ ! -d ${work} ];then
11    mkdir -p ${work}
12    echo "Dir ${work} is created!"
13    fi  
14 
15    #CHECK USER
16    is_root=`id |awk '{print $1}'|awk -F'[=(]' '{print $2}'`
17    if [ $is_root = 0 ]
18    then
19    echo "Current user is `whoami`!"
20    Current_user="/root/"
21    echo -e "\033[32;49;1m---------------------------------------------------------------------\033[39;49;0m"
22    else
23    echo "Current user is `whoami`!"
24    Current_user="/home/`/usr/bin/whoami`"
25    echo $Current
26    echo -e "\033[32;49;1m---------------------------------------------------------------------\033[39;49;0m"
27    #exit 1
28    fi
29 
30    #Check ssh_key
31    cd ${Current_user}
32    SSH_Key=`/bin/ls -al|grep '.ssh$'|wc -l`
33 
34    if [ ${SSH_Key} -eq 1 ]
35    then
36    echo "ssh_key is exist!!!......"
37    echo -e "\033[32;49;1m---------------------------------------------------------------------\033[39;49;0m"
38    #sleep 5
39    #/bin/mv .ssh /tmp/.ssh$(date +%Y-%m-%d-%H-%M)
40    #echo|/usr/bin/ssh-keygen -t dsa
41    else
42    echo "ssh_key is not exist,start create ssh_key"
43    echo -e "\033[32;49;1m---------------------------------------------------------------------\033[39;49;0m"
44    echo|/usr/bin/ssh-keygen -t dsa
45    fi
46 
47    #Send ssh_key to client
48    if [ ! -e ${ip_list} ];then
49    touch ${ip_list}
50    echo "Please input ip address,then go on......."
51    echo -e "\033[32;49;1m---------------------------------------------------------------------\033[39;49;0m"
52    exit 1
53    fi
54    
55    is_ip=`nl ${ip_list}|wc -l`
56    if [ ${is_ip} -eq 0 ]
57    then
58    echo "${ip_list} is empty!"
59    exit 1
60    fi
61 
62    Cur_u=`/usr/bin/whoami`
63    while read Remote_host
64    do
65    ssh-copy-id -i .ssh/id_dsa.pub "-p 51022 ${Cur_u}@${Remote_host}"
66    echo "$Remote_host is send ok!!!"
67    echo -e "\033[32;49;1m---------------------------------------------------------------------\033[39;49;0m"
68    done < ${ip_list}

 

 执行效果图如下:

 

posted @ 2013-08-07 16:55  study-notes  阅读(245)  评论(0)    收藏  举报