首页 > 其他分享 >ssh免密码登陆避免首次需要输入yes

ssh免密码登陆避免首次需要输入yes

时间:2024-04-12 22:44:33浏览次数:22  
标签:10.0 0.31 密码 ssh key yes root id

看下面,默认连接需要输入yes
服务器多了之后一台一台去输入yes肯定也不现实。太麻烦了

[root@ansible ~]# ssh-keygen 
[root@ansible ~]# ssh 10.0.0.31
[root@ansible ~]# ssh-copy-id 10.0.0.31
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '10.0.0.31 (10.0.0.31)' can't be established.
ECDSA key fingerprint is SHA256:h1TDNMhJRVbSfxNtVqz3uTBZBa/Dv3S9Hzxfn3kCFTM.
ECDSA key fingerprint is MD5:e6:57:8c:d7:87:13:e6:51:06:7d:fc:8a:06:c2:3c:b2.
Are you sure you want to continue connecting (yes/no)? 

写个脚本避免交互输入yes。无密钥登录的自动脚本实现

[root@ansible ~]# vim auto_ssh.sh 
#!/usr/bin/expect  
set timeout 10  

#执行该脚本传入进来的三个参数
set username [lindex $argv 0]  
set password [lindex $argv 1]  
set hostname [lindex $argv 2]

#此处为你想要传递给对端机器进行授权的密钥存放位置  
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub $username@$hostname
expect {
            #first connect, no public key in ~/.ssh/known_hosts
            "Are you sure you want to continue connecting (yes/no)?" { #第一次ssh匹配此处逻辑
            send "yes\r"
            expect "password:"
                send "$password\r"
            }
            #already has public key in ~/.ssh/known_hosts
            "password:" { #第二次匹配此处逻辑
                send "$password\r"
            }
            "Now try logging into the machine" {
                #it has authorized, do nothing! #已经授权无密钥登录,则匹配此处逻辑,do nothing
            }
        }
expect eof

授权

[root@ansible ~]# chmod 777 auto_ssh.sh 

执行如下命令
传入的三个参数,分别为对端机器的用户、密码、主机IP

[root@ansible ~]# yum install expect -y #需要安装expcet
[root@ansible ~]# ./auto_ssh.sh root 123456 10.0.0.31
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '10.0.0.31 (10.0.0.31)' can't be established.
ECDSA key fingerprint is SHA256:h1TDNMhJRVbSfxNtVqz3uTBZBa/Dv3S9Hzxfn3kCFTM.
ECDSA key fingerprint is MD5:e6:57:8c:d7:87:13:e6:51:06:7d:fc:8a:06:c2:3c:b2.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.

再次测试ssh登录成功

[root@ansible ~]# ssh 10.0.0.31
Last login: Mon Apr  3 15:32:27 2023 from 10.0.0.11
[root@web01 ~]# cat /root/.ssh/authorized_keys 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDZcZjBTYo8aAXflCOpXXkZsnnjyhWZttCPlatKAgPedLaDRSkkDQJby/DODGMLnCyB3lkh52EuG0UilQJRPqGoBxVxwnLx6ewSsVMN+CcSwGdTyG02YyIWoII8Y4G4JblPutKvfjZQ53f/8lYjpk3ylXteaGruL7WZpk2QNpBl7n/43cGXdTGAS+l7RoFYlkHTK7fqpxdOK6fT3Rzv64ZRTe1Ib1S+7RS8ng+562JQscdX/Y+I9dx/cSOPSD29zeV546kfLNtLQBLeHPs9lqdpcok1/8OPQC+WZRBqMm3TO3sfLHgrgttsIxKh5xZkuQGHbtiJPbEFnL8XV5vAMqT/ root@ansible

标签:10.0,0.31,密码,ssh,key,yes,root,id
From: https://www.cnblogs.com/zhangjianzi/p/18132283

相关文章

  • ansible 统计 ssh 登录信息
    hosts[centos-root]192.168.174.129ansible_ssh_port=22192.168.174.130ansible_ssh_port=22192.168.174.131ansible_ssh_port=22AnsibleVault文件创建AnsibleVault文件#ansible-vaultcreatepasswords.ymlNewVaultpassword:#123456......
  • mysql修改密码报错:Your password does not satisfy the current policy requirements
    参考https://blog.csdn.net/u013449046/article/details/106455041这是mysql初始化时,使用临时密码,修改自定义密码时,由于自定义密码比较简单,就出现了不符合密码策略的问题。密码策略问题异常信息:ERROR1819(HY000):Yourpassworddoesnotsatisfythecurrentpolicyrequ......
  • ansible 通过密码登录主机
    hosts[centos-root]192.168.174.129ansible_ssh_port=22ansible_ssh_user=rootansible_ssh_pass=host1192.168.174.130ansible_ssh_port=22ansible_ssh_user=root192.168.174.131ansible_ssh_port=22ansible_ssh_user=rootAnsibleVault文件创建AnsibleVault......
  • 实验一-密码引擎-3-加密API研究
    任务详情密码引擎API的主要标准和规范包括:1.微软的CryptoAPI2.RAS公司的PKCS#11标准3.中国商用密码标准:GMT0016-2012智能密码钥匙密码应用接口规范,GMT0018-2012密码设备应用接口规范等研究以上API接口,总结他们的异同,并以龙脉GM3000Key为例,写出调用不同接口的代码,提交博客......
  • js中密码或邮箱等..加密及解密方法
    在JavaScript中,密码和邮箱信息通常需要在客户端加密后再发送到服务器,保证信息的安全性。以下是一个使用CryptoJS库进行加密和解密的例子:首先,需要引入CryptoJS库:<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/crypto-js.min.js"></script>然后,可以使用......
  • 修改云镜像密码
    修改云镜像密码镜像已经上传到glance服务。创建机器后发现密码没有修改,现记录修改机器密码过程。环境及镜像查看镜像:openstackimagelist#+--------------------------------------+-------------------------+--------+#|ID|Name......
  • rhel7和centos7以上版本忘记root密码处理方法
    目录1.开机进入grub菜单2.找到linux16一行进行配置3.挂载文件系统并重置密码1.开机进入grub菜单进入grub菜单,选择默认第一个启动项,按e进行编辑2.找到linux16一行进行配置找到linux16一行,摁ctrl+e跳转到行尾,输入\rd.break,再按ctrl+x进入急救模式3.挂载文件系统并重......
  • 实验一-密码引擎-3-加密API研究
    任务详情密码引擎API的主要标准和规范包括:1微软的CryptoAPI2RAS公司的PKCS#11标准3中国商用密码标准:GMT0016-2012智能密码钥匙密码应用接口规范,GMT0018-2012密码设备应用接口规范等研究以上API接口,总结他们的异同,并以龙脉GM3000Key为例,写出调用不同接口的代码,提交博客......
  • 实验一-密码引擎-3-加密API研究
    一、任务详情密码引擎API的主要标准和规范包括:1微软的CryptoAPI2RAS公司的PKCS#11标准3中国商用密码标准:GMT0016-2012智能密码钥匙密码应用接口规范,GMT0018-2012密码设备应用接口规范等研究以上API接口,总结他们的异同,并以龙脉GM3000Key为例,写出调用不同接口的代码,提交......
  • 密码学基础--搞清RFC和PKCS(1)
    目录1.CryptoDriver里KeyElement格式2.挖掘RFC标准3.小结昨天从生成密钥对开始逐步了解了公钥、证书等各种编码方式,今天继续趁热打,做一个理论知识汇总。Ps:我只是标准的翻译搬运工。1.CryptoDriver里KeyElement格式在 CryptoKeyElement配置项里,我们会发现有form......