Shell bulk SSH free Interactive login host

Source: Internet
Author: User

Scripting features: Batch or single SSH-free login authentication

Script scenario: When deploying a cluster, most implementations want to configure the management node with SSH-free login from the node, writing the following script to simplify the task.

#!/bin/bash# blog:http://lizhenliang.blog.51cto.com color_echo ()  {    if  [ $1 ==  "Green"  ]; then        echo  -e  "\033[32;40m$2\033[0m"     elif [ $1 ==  "Red"  ];  then        echo -e  "\033[31;40m$2\033[0m"      fi}check_ssh_auth ()  {    ssh  $INFO   ' Echo yes >/dev /null '     if [ $? -eq 0 ]; then         color_echo green  "host  $IP  SSH authentication  Successfully. "     else        color_echo red  "Host   $IP  ssh authentication failure! "          exit    &Nbsp;fi}os_version ()  {    os_v=$ (cat /etc/issue |awk  ' NR==1{print  $1} ')     if [  $OS _v ==  "\s"  -o  $OS _v ==  " CentOS " ]; then        echo " CentOS "     elif [  $OS _v ==  "Ubuntu"  ]; then         echo  "Ubuntu"     fi}if ! $ (which expect >/dev/null);  then    if [ $ (os_version)  ==  "CentOS"  ]; then         yum install expect -y    [ $ ? -ne 0 ] && color_echo red  "The expect installation  failure! "  && exit 1    elif [ $ (os_version)  ==  "Ubuntu"  ]; then        apt-get install expect -y         if [ $? -ne 0 ]; then             apt-get install expect --force-yes -y             [ $? -ne 0 ]  && color_echo red  "the expect installation failure!"  && exit 1        fi     Fifiif [ ! -e ~/.ssh/id_rsa.pub ]; then     color_echo  green  "the public/private rsa key pair not exist, start  Generating ... "    expect -c "          spawn ssh-keygen     &Nbsp;  expect {            \ "Ssh/id _RSA): \ " {send \" \r\ ";exp_continue}             \ "(empty for no passphrase): \"  {send \ "\r\";exp_continue}             \ "Again:\"  {send \ "\r\"; Exp_continue}         }      " >/dev/null 2> &1    if [ -e ~/.ssh/id_rsa.pub ]; then         color_echo green  "Generating public/private rsa key  pair successfully. "     else        color_echo red  " generating public/private rsa key pair failure! "     fifiif [[ $1 =~ ^[a-z][email protected][0-9]{1,3}\. [0-9] {1,3}\. [0-9] {1,3}\. [0-9] {1,3}@.* ]]; then    for i in [email protected]; do         user=$ (echo  $i |cut [email protected] -f1)         ip=$ (echo  $i |cut [email protected] -f2)         pass=$ (echo  $i |cut [email protected] -f3)         [email protected] $IP          expect -c  "            spawn  ssh-copy-id  $INFO             expect  {                \ "(yes/ NO)? \ " {send \" yes\r\ "; exp_continue}&nbsP;               \ "Password:\"  {send \ "$PASS \r\";exp_continue}             }         " >/dev/null 2>&1         check_ssh_auth    doneelif [[ $1 =~ ^[ A-z][email protected][0-9]{1,3}\. [0-9] {1,3}\. [0-9] {1,3}\. [0-9] {1,3}-[0-9]{1,3}@.* ]]; then    start_ip_num=$ (echo $1|sed -r  ' s/ .*\. (. *)-(. *) @.*/\1/')     end_ip_num=$ (echo $1|sed -r  ' s/.*\. *)-(. *) @.*/\2/')     for  ((i= $START _ip_num;i<= $END _ip_num;i++)); do         user=$ (ECHO&NBSP;$1|CUT&NBSP;[EMAIL&NBSP;PROTECTED]&NBSP;-F1)          pass=$ (ECHO&NBSP;$1|CUT&NBSP;[EMAIL&NBSP;PROTECTED]&NBSP;-F3)         ip_range=$ (echo $1|sed -r   ' s/.*@ (. *\.). */\1/')         ip= $IP _range$i         [email protected] $IP _range$i        expect -c   "            spawn ssh-copy-id $ info            expect {                 \ "(yes/no)? \"  {send \ " Yes\r\ ";exp_continue}                 \ "Password:\"  {send \ "$PASS \r\";exp_continue}             }         " >/dev/null 2> &1        check_ssh_auth    doneelse     echo  "example1: $0 <[email protected" @password > "    echo " Example2: $0 [[email protected] @password  [email protected] @password  [email  Protected] @password &nbsp ...] " Fi

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/79/D7/wKiom1ack4yzKz-qAAA95iiI1LE087.png "title=" QQ picture 20160118152324.png "alt=" Wkiom1ack4yzkz-qaaa95iii1le087.png "/>


This article is from the "Li Zhenliang Technology Blog" blog, make sure to keep this source http://lizhenliang.blog.51cto.com/7876557/1736179

Shell bulk SSH free Interactive login host

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.