码迷,mamicode.com
首页 > 其他好文 > 详细

批量操作SSH账户信任

时间:2015-12-12 08:23:26      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:批量ssh信任 expect ssh-copy-id

#!/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=`grep $(whoami) /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`

if [ $data_size -lt 4 ];then

        for i in $ip

        do

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

        done

else

for i in $ip

do

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

done

fi

本文出自 “、矿泉水” 博客,请务必保留此出处http://guwenqiang.blog.51cto.com/5462040/1722230

批量操作SSH账户信任

标签:批量ssh信任 expect ssh-copy-id

原文地址:http://guwenqiang.blog.51cto.com/5462040/1722230

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!