poorX

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
  117 随笔 :: 0 文章 :: 11 评论 :: 16万 阅读

Linux信任登录,免密码登录的脚本:

需要安装expect,可以yum也可以编译安装,编译后注意脚本中的expect路径。

rsa.exp

复制代码
#!/usr/bin/expect
##################################################
#                                                #
# Author:                               #
# Date: 2011-06-20                               #
# Descrition: SSH auto create rsa key at local   #
#                                                #
##################################################

# Get parameter
set dbip [lindex $argv 0]
set pass [lindex $argv 1]


# create local rsa key
spawn ssh-keygen -t rsa;
expect "*(/root/.ssh/id_rsa):";
send "\r";
expect "*(empty for no passphrase):";
send "\r";
expect "*Enter same passphrase again:";
send "\r";
expect eof
复制代码

 pubKey.exp

复制代码
#!/usr/bin/expect
##################################################
#                                                #
# Author:                    #
# Date: 2011-07-29                               #
# Descrition: SSH auto add accredit to server    #
#                                                #
##################################################

# Get parameter
set dbip [lindex $argv 0]
set pass [lindex $argv 1]
set timeout 30

# create .ssh dir
spawn ssh root@$dbip
expect {
"yes/no" { send "yes\r"; exp_continue}
"password:" {
  send "$pass\r";
  expect {
    "Last login" {
       send "mkdir -p /root/.ssh \r";
       send "chmod 700 /root/.ssh \r"
     }
  }
}
"Last login" { exit }
}
expect ""


# scp rsa key to remote server
spawn scp /root/.ssh/id_rsa.pub root@$dbip:/root/.ssh/id_rsa.pub.tmp;
expect "*password:";
send "$pass\r";
expect eof


spawn ssh root@$dbip cat /root/.ssh/id_rsa.pub.tmp >> /root/.ssh/authorized_keys
expect "*password:";
send "$pass\r";
expect eof

spawn ssh root@$dbip rm -f /root/.ssh/id_rsa.pub.tmp
复制代码

 

在登录时系统提示信息,例如提示:

Address 10.1.1.1 maps to localhost, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!

可以通过修改sshd配置取消输出,以免影响自动化部署脚本允许:

[root@zabbix ~]# sed -i 's/GSSAPIAuthentication yes/GSSAPIAuthentication no/g' /etc/ssh/sshd_config ; service sshd restart

 

posted on   poorX  阅读(325)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
点击右上角即可分享
微信分享提示