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. [Swift]LeetCode1027. 最长等差数列 | Longest Arithmetic Sequence

    Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall t ...

  2. C#基础语法

    究极入门之Hello world static void Main(string[] args) { //你好,世界 Console.WriteLine("HELLO WORLD" ...

  3. BBS论坛(二十五)

    25.1.发布帖子后台逻辑完成 (1)apps/models.py class PostModel(db.Model): __tablename__ = 'post' id = db.Column(d ...

  4. java基础(十六)----- equals()与hashCode()方法详解 —— 面试必问

    本文将详解 equals()与hashCode()方法 概述 java.lang.Object类中有两个非常重要的方法: public boolean equals(Object obj) publi ...

  5. Visual Studio 2017中使用SourceLink调试ASP.NET Core源码

    背景 当我们在学习ASP.NET Core或者调试ASP.NET Core程序的时候,有时候需要调试底层代码,但是当我们在Visual Studio中调试程序的时候,由于一些基础库或者第三方库缺少pd ...

  6. 带着新人学springboot的应用13(springboot+热部署)

    spring cloud我想做成一个系列,所以spring cloud+eureka后面会慢慢说到的,有兴趣的小伙伴可以关注后续! 这一节就简单说说springboot的热部署了(我一直想不通为什么叫 ...

  7. Chapter 5 Blood Type——25

    "I usually am — but about what in particular this time?" “我通常都是 —— 但是这次有什么特殊的吗?” "Dit ...

  8. Docker 网络之理解 bridge 驱动

    笔者在前文<Docker 网络之进阶篇>中介绍了 CNM(Container Network Model),并演示了 bridge 驱动下的 CNM 使用方式.为了深入理解 CNM 及最常 ...

  9. [图解]ARP协议(一)

    一.ARP概述 如果要在TCP/IP协议栈中选择一个"最不安全的协议",那么我会毫不犹豫把票投给ARP协议.我们经常听到的这些术语,包括"网络扫描"." ...

  10. Spring事务的传播行为

      本文主要介绍下Spring事务中的传播行为. 事务传播行为介绍 Spring中的7个事务传播行为: |事务行为|说明 | |:--|:--| |PROPAGATION_REQUIRED | 支持当 ...