批量操作SSH賬戶信任

#!/bin/bash


auto_ssh_key () {

expect -c "

        set timeout -1;

        spawn ssh-copy-id -i $path/.ssh/id_rsa $1;

        expect {

                *yes/no* {

                        send -- yes\r

                        expect *assword*

                        send -- $2\r;

                        expect { 

                                *denied* {exit 2;}

                                eof

                                }

                        }

                *assword* {

                        send -- $2\r;

                        expect { 

                                *denied* {exit 2;}

                                eof

                                }

                        }

                        eof

                }

                "

}


echo '

 -------------------- Directions for use --------------------

| Internal IP using examples:                                |

|       please input port: 10 15 29 21 21                    |

| External use IP example:                                   |

|       please input port:  8.8.8.8 google.xxx.com           |

|                                                            |

| please input port:                                         |

|       A null value as the default port 22                  |

|                                                            |

| please input username:                                     |

|       A null value as the current user                     |

 ------------------------------------------------------------    

'


# 獲取用戶家目錄路徑

path=`awk NR==$(echo $(awk -F ':' '{print $1}' /etc/passwd | grep -xn $(whoami)) | awk -F ':' '{print $1}') /etc/passwd | awk -F ':' '{print $6}'`


read -p "please input ip:" ip

read -p "please input port:" port 

read -p "please input username:" username

read -s -p "please input password:" password


# 判斷是否指定端口, 默認爲22

if [ -z $port ];then

        port='22'

fi


# 判斷是否指定用戶名, 默認爲當前賬號

if [ -z $username ];then

        username=$(whoami)

fi


# 檢測輸入的IP是內部還是外部機器

data_size=`echo $ip | awk -F " " '{print $1}'|wc -L`

for i in $ip

do

        if [ $data_size -lt 4 ];then

                auto_ssh_key '"-p '$port' '$username'@10.0.0.'$i'"' $password

        else

                auto_ssh_key '"-p '$port' '$username'@'$i'"' $password

        fi

done


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章