Linux中批量配置SSH免密

写在前面


傍晚时分,你坐在屋檐下,看着天慢慢地黑下去,心里寂寞而凄凉,感到自己的生命被剥夺了。当时我是个年轻人,但我害怕这样生活下去,衰老下去。在我看来,这是比死亡更可怕的事。--------王小波


expect 安装

expect 是由Don Libes基于Tcl(Tool Command Language )语言开发的,主要应用于自动化交互式操作的场景,借助Expect处理交互的命令,可以将交互过程如:ssh登录,ftp登录等写在一个脚本上,使之自动化完成。尤其适用于需要对多台服务器执行相同操作的环境中,可以大大提高系统管理人员的工作效率

在内网环境下,无法连接yum源,我们可以找一台有网的机器,把rpm包下载下来,然后上传到内网环境

[root@vms152 ~]# yum -y install expect --downloadonly --downloaddir=/root/soft
...
[root@vms152 ~]# cd soft/;ls
expect-5.45-14.el7_1.x86_64.rpm  tcl-8.5.13-8.el7.x86_64.rpm

通过rpm -ivh 的方式来安装

[root@vms152 soft]# rpm -ivh /root/soft/*
准备中...                          ################################# [100%]
正在升级/安装...
   1:tcl-1:8.5.13-8.el7               ################################# [ 50%]
   2:expect-5.45-14.el7_1             ################################# [100%]
[root@vms152 soft]#
[root@vms152 soft]# which expect
/usr/bin/expect

在可以连接yum源的情况下,我们直接通过包管理器yum来下载

[root@vms152 soft]# yum -y install expect

expect 基础用法

这里以root用户为例配置免密

Expect作为一种重要的TCL扩展包,主要有以下几个命令:

我们来看一个简单的Demo,命令行的方式来运行,通过SSH登录一台机器,并退出。这个交互式到的命令通过expect如何处理。

[root@vms152 soft]# expect  <<- EOF
> spawn ssh root@127.0.0.1
> expect "*pass*" { send "redhatr"}
> expect "*connecting*" { send "yesr"}
> expect "#"
> send "exit r"
> expect eof
> EOF
spawn ssh root@127.0.0.1
root@127.0.0.1's password:
Last login: Sat Aug 20 15:22:06 2022 from 127.0.0.1
[root@vms152 ~]# exit
登出
Connection to 127.0.0.1 closed.
[root@vms152 soft]#

如果是第一次登录,或者没有保存公钥,那么会有一个保存公钥的提示,我们可以这样处理,这次我们通过ctl脚本的方式

[root@vms153 ~]# cat su.ctl
#!/usr/bin/expect

spawn ssh root@127.0.0.1

expect {
       "*connecting*" { send "yesr"}
       "*pass*" { send "redhatr"}
       }

expect {
       "#" {send "r"}
       "*pass*" { send "redhatr"}
       }
expect {
       "#" {send "exit r"}
       }
expect eof

清空SHSH存放公钥的文件,给脚本授权,执行

[root@vms153 ~]# cat /dev/null  > /root/.ssh/known_hosts
[root@vms154 ~]# chmod +x su.ctl 
[root@vms153 ~]# ./su.ctl
spawn ssh root@127.0.0.1
The authenticity of host '127.0.0.1 (127.0.0.1)' can't be established.
ECDSA key fingerprint is SHA256:rQokINjVDeZOfyKKcLlhIe92bgkN8xZ13QiPwr/0cxo.
ECDSA key fingerprint is MD5:35:83:98:1d:76:b8:33:b0:b6:ba:d5:0f:34:2f:ba:b9.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '127.0.0.1' (ECDSA) to the list of known hosts.
root@127.0.0.1's password:
Last login: Sat Aug 20 17:25:03 2022 from 127.0.0.1
[root@vms153 ~]# exit
登出
Connection to 127.0.0.1 closed.
[root@vms153 ~]#

当然,也可以交互式的使用,下面的脚本实现通过SHH远程到一台机器,并查看主机名,然后把终端交给标准输入

[root@vms153 ~]# cat su.ctl
#!/usr/bin/expect

spawn ssh root@192.168.26.152

expect {
       "*connecting*" { send "yesr"}
       "*pass*" { send "redhatr"}
       }

expect {
       "#" {send "r"}
       "*pass*" { send "redhatr"}
       }

expect "#"

send "hostname r"
expect "#"
interact

运行脚本,我们可以直接操作SSH机器

[root@vms153 ~]# ./su.ctl
spawn ssh root@192.168.26.152
root@192.168.26.152 s password:
Last login: Sat Aug 20 17:31:07 2022 from 192.168.26.153
[root@vms152 ~]#
[root@vms152 ~]# hostname
vms152.rhce.cc
[root@vms152 ~]# ls
anaconda-ks.cfg  calico_3_14.tar  calico.yaml  one-client-install.sh  set.sh  soft
[root@vms152 ~]# exit
登出
Connection to 192.168.26.152 closed.
[root@vms153 ~]#

expect 配置root免密

[root@vms152 soft]# cat host_list
192.168.26.153
192.168.26.154
[root@vms152 soft]# cat mianmi.sh
#!/bin/bash

#@File    :   mianmi.sh
#@Time    :   2022/08/20 17:45:53
#@Author  :   Li Ruilong
#@Version :   1.0
#@Desc    :   None
#@Contact :   1224965096@qq.com


/usr/bin/expect <<-EOF
spawn ssh-keygen
expect "(/root/.ssh/id_rsa)" {send "r"}
expect {
       "(empty for no passphrase)" {send "r"}
       "already" {send "yr"}
       }

expect {
       "again" {send "r"}
       "(empty for no passphrase)" {send "r"}
       }

expect {
       "again" {send "r"}
       "#" {send "r"}
       }
expect "#"
expect eof
EOF

for IP in $( cat host_list )
do

if [ -n IP ];then

/usr/bin/expect <<-EOF
spawn ssh-copy-id root@$IP

expect {
       "*yes/no*"   { send "yesr"}
       "*password*" { send "redhatr" }
       }
expect {
       "*password" { send "redhatr"}
       "#"         { send "r"}
       }
expect "#"
expect eof
EOF
fi

done

这里需要说明的是 expect 中有些特殊关键字用于匹配过程,代表某些特殊的含义或状态,可能会和shell 脚本中的冲突,所以一般只用于expect命令中而不能在expect命令单独使用,比如 eof,timeout等,如果密码中存在特殊字符,且在脚本里冲突,那么可以使用一些python模块来处理,比如Paramiko,fabric 等。

博文参考

linux expect 详解:https://blog.csdn.net/zxycyj1989/article/details/125837697

展开阅读全文

页面更新:2024-06-17

标签:批量   字符串   变量   脚本   进程   命令   机器   操作   方式   环境

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号

Top