shell批量互信脚本

来源:互联网 发布:火线精英礼包软件 编辑:程序博客网 时间:2024/05/08 07:14

通常情况下,有很多服务器,如果全部手动配置互信,无疑是毫无含金量的重复性工作,这里写了一个脚本,完美的solve了这个问题。

测试环境信息:hosts表

[root@ceph-node1 ~]# yum install expect -y

[root@ceph-node1 ~]#cat /etc/hosts| grep -v 127 | grep -v :: | awk "{print \$1}" > ip.list

 

建立一个auto_ssh.sh

脚本如下:

#! /usr/bin/expect 

spawn ssh-keygen  ####主要的功能是给ssh运行进程加个壳,用来传递交互指令,spawn后面加上需要 执行的shell命令,比如说spawn sudo touch testfile

expect "id_rsa"    #####expect 命令的意思是判断上次输出结果里是否包含“XXXX”的字符串

send "\r"             #####执行交互动作,与手工输入密码的动作等效。 命令字符串结尾加上“\r”,如果出现异常等待的状态可以核查一下。

expect "Overwrite"####如果执行错误,第二次执行脚本的时候会有overwrite

send "yes\r"

expect "phrase"

send "\r"

expect "again"

send "\r"

Interact    ########执行完成后保持交互状态,把控制权交给控制台,这个时候就可以手工操作了。如果没有这一句登录完成后会退出,而不是留在远程终端上。如果你只是登录过去执行

 

set f [open ip.list r]   ###set 定义变量  open函数打开文件,【r】参数是以只读的形式,但是文件必须存在

while { [gets $f ip]>=0} { 

        spawn ssh-copy-id $ip 

        expect { 

            "*yes/no" {send "yes\r";exp_continue} 

            "*password:" {send "redhat\r";exp_continue} 

            } 

    } 

close $f 

 

[root@ceph-node1 ~]# ./auto_ssh.sh  ##############脚本执行

脚本运行截图:

 

成功免密登陆:

参考至:http://blog.csdn.net/hellokidss/article/details/50146847

  http://blog.csdn.net/catoop/article/details/48289991

原创粉丝点击