20181225-Linux Shell Bash环境下自动化创建ssh互信脚本

1. 我的Blog

博客园 https://www.cnblogs.com/piggybaba/

个人网站 http://piggybaba.cn

GitHub https://github.com/AndyYHM/Writing/

2. 简介信息

摘要:Linux下,自动化创建SSH互信脚本

Author: andy_yhm@yeah.net

Date: 20181225

关键字:Shell脚本, ssh, ssh trust ,auto,SSH互信,/bin/bash

3. 脚本输出效果

单一节点上,用户python,执行脚本后,输入三台节点python用户密码,自动化创建SSH互信关系

$ sh SSH_Trust.sh
/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
python@node11's password:
/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
python@node12's password:
/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
python@node13's password:
Transfer authorized_keys
authorized_keys 100% 1185 1.2KB/s 00:00
known_hosts 100% 537 0.5KB/s 00:00
authorized_keys 100% 1185 1.2KB/s 00:00
known_hosts 100% 537 0.5KB/s 00:00

4. 功能说明

  • 默认支持3节点自动化创建SSH互信关系
  • 支持多节点自动化创建SSH互信关系

5. 使用说明

  • 需要提前编辑好/etc/hosts文件
  • 用户名所有主机设置为一致
  • 使用前编辑脚本"config to do"部分,节点hostname和用户名
  • othernodes参数需以空格” “隔开;
  • 执行脚本后,需逐一输入节点用户的密码
  • 若主机节点数规模庞大,建议使用expect工具,另行编辑脚本;

6. 脚本内容

#!/usr/bin/env bash

#########################################
# Author: andy_yhm@yeah.net
# Date: 20181225
# Key Word : Shell脚本, ssh, ssh trust ,auto,SSH互信,/bin/bash
#########################################
#
## Config to do
#
node1=node11
node2=node12
node3=node13
othernodes=
user=test #
## Please Don't edit content below
#
ssh-keygen -q -P "" -f $HOME/.ssh/id_rsa > /dev/null
for node in ${node1} ${node2} ${node3} ${othernodes}
do
if [ "`hostname`" == "$node" ]; then
ssh-copy-id -o stricthostkeychecking=no $user@$node > /dev/null
else
ssh-copy-id -o stricthostkeychecking=no python@$node > /dev/null
ssh $node 'ssh-keygen -q -P "" -f $HOME/.ssh/id_rsa' > /dev/null
scp -rp $node:$HOME/.ssh/id_rsa.pub ./auth.$node > /dev/null
fi
done cat ./auth.* >> $HOME/.ssh/authorized_keys
rm -rf ./auth.* echo "Transfer authorized_keys"
for node in ${node1} ${node2} ${node3} ${othernodes}
do
if [ "`hostname`" != "$node" ]; then
scp -rp $HOME/.ssh/authorized_keys $node:$HOME/.ssh/authorized_keys
scp -rp $HOME/.ssh/known_hosts $node:$HOME/.ssh/known_hosts fi done exit 0

20181225-Linux Shell Bash环境下自动化创建ssh互信脚本的更多相关文章

  1. Linux Shell基础(下)

    Linux Shell基础(下) 目录 一.shell特殊符号cut命令 二.cut.sort.wc.uniq命令 三.tee.tr.split命令 四.简易审计系统 五.fork, exec, so ...

  2. Linux 14.04lts 环境下搭建交叉编译环境arm-linux-gcc-4.5.1

    交叉编译工具链是为了编译.链接.处理和调试跨平台体系结构的程序代码,在该环境下编译出嵌入式Linux系统所需要的操作系统.应用程序等,然后再上传到目标板上. 首 先要明确gcc 和arm-linux- ...

  3. linux系统Centos环境下搭建SVN服务器及权限配置

    linux系统Centos环境下如何搭建SVN服务器以及svnserve.conf.authz.passwd配置文件详细介绍   至于svn的概念,这里就不做详细阐述了,可以自行百度.简单来讲就是一个 ...

  4. source 命令的用法,是在当前bash环境下执行脚本文件

    原文: http://www.cnblogs.com/softwaretesting/archive/2012/02/13/2349550.html source命令用法: source FileNa ...

  5. JAVA中调用LevelDB用于Linux和Window环境下快速存储KV结构

    一.简介 JAVA中调用LevelDB用于Linux和Window环境下快速存储KV结构 二.依赖 <!-- https://mvnrepository.com/artifact/org.fus ...

  6. 一步步教你搭建VS环境下用C#写WebDriver脚本

    一步步教你搭建VS环境下用C#写WebDriver脚本http://www.automationqa.com/forum.php?mod=viewthread&tid=3529&fro ...

  7. Window环境下,PHP调用Python脚本

    参考 php调用python脚本*** php 调用 python脚本的方法 解决办法:php提供了许多调用其他脚本或程序的方法,比如exec/system/popen/proc_open/passt ...

  8. Linux Bash环境下单引(')、双引号(")、反引号(`)、反斜杠(\)的小结

    在bash中,$.*.?.[.].’.”.`.\.有特殊的含义.类似于编译器的预编译过程,bash在扫描命令行的过程中,会在文本层次上,优先解释所有的特殊字符,之后对转换完成的新命令行,进行内核的系统 ...

  9. Anaconda3+python3环境下如何创建python2环境(win+Linux下适用,同一个anaconda下py2/3共存)

    本人之前已经在anaconda环境下已经安装了python3的环境,现在因为一些需求,要安装python2环境 1.打开anaconda的anaconda prompt查看当前环境: conda in ...

随机推荐

  1. shell脚本_查找无效网址

    #!/bin/bashif [ $# -ne 1 ];then      echo -e "$Usage: $0 URL\n"      exit 1;fi echo Broken ...

  2. VSCode与Deepin资源管理器冲突

    解决方式: xdg-mime default dde-file-manager.desktop inode/directory 此外,网上有较多推荐(在deepin 15.8版本上测试无效): gvf ...

  3. IntelliJ IDEA下SVN的配置及使用说明

    1 下载及安装SVN客户端. 到官网下载小乌龟SVN客户端,官网地址:https://tortoisesvn.net/downloads.html,根据操作系统情况选择适合版本.比如64为操作系统,如 ...

  4. 在 Vue 结合 Axios 使用过程 中 post 方法,后台无法接受到数据问题

    关于在 vue 中 使用 axios 相关 bug 首先,我们来看下 axios 的 github 传送门 axios 然后我们再介绍下 axios 的作者的 github 传送门 Matt 最后,我 ...

  5. Android 开发工具类 18_NetWorkUtil

    检测网络的一个工具包: 1.网络是否可用: 2.判断是否有网络连接: 3.判断 WIFI 网络是否可用: 4.判断 MOBILE 网络是否可用; 5.获取当前网络连接的类型信息: 6.获取当前的网络状 ...

  6. Node.js 中的 stream

    什么是 stream Stream 借鉴自 Unix 编程哲学中的 pipe. Unix shell 命令中,管道式的操作 | 将上一个命令的输出作为下一个命令的输入.Node.js stream 中 ...

  7. DocX开源WORD操作组件的学习系列一

    DocX学习系列 DocX开源WORD操作组件的学习系列一 : http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_sharp_001_docx1.htm ...

  8. .NetCore2.1 WebAPI新增Swagger插件

    说明 Swagger是一个WebAPI在线注解.调试插件,过去我们主要通过手工撰写WebAPI接口的交互文档供前端开发人员或外部开发者, 官网地址:https://swagger.io/. 但是在实际 ...

  9. nginx部署dotnet core站点

    步骤 aspnetcore程序端口号5001,实际外部端口号8001,相当于把8001收到的请求转发给5001. 把发布出来的文件全部丢掉 /var/www/JuXiangTou 里面去.可以用scp ...

  10. CSS中层叠和CSS的7阶层叠水平(上篇)

    今天搜索资料时,忽然发现了以前没注意的一个知识点,所以拖过来搞一搞,这个知识点叫做CSS的7阶层叠水平 在说这个知识之前,我们必须要先了解一个东西以便于我们更好的理解CSS的7阶层叠水平 这个东西就是 ...