脚本实现功能:批量或单个SSH免交互登录认证

脚本应用场景:当部署集群时,大多数实现要配置好管理节点与从节点的SSH免交互登录,针对这样的情况,写了下面脚本,简化工作。

脚本支持系统:Ubuntu和CentOS


#!/bin/bash
# Description: configuration local host and remote host ssh keypair authentication, Support Ubuntu and CentOS operation system.
# Blog: http://lizhenliang.blog.51cto.com function color_echo() {
if [ $ == "green" ]; then
echo -e "\033[32;40m$2\033[0m"
elif [ $ == "red" ]; then
echo -e "\033[31;40m$2\033[0m"
fi
}
function os_version() {
local OS_V=$(cat /etc/issue |awk 'NR==1{print $1}')
if [ $OS_V == "\S" -o $OS_V == "CentOS" ]; then
echo "CentOS"
elif [ $OS_V == "Ubuntu" ]; then
echo "Ubuntu"
fi
}
function check_ssh_auth() {
if $(grep "Permission denied" $EXP_TMP_FILE >/dev/null); then
color_echo red "Host $IP SSH authentication failure! Login password error."
exit
elif $(ssh $INFO 'echo yes >/dev/null'); then
color_echo green "Host $IP SSH authentication successfully."
fi
rm $EXP_TMP_FILE >/dev/null
}
function check_pkg() {
local PKG_NAME=$
if [ $(os_version) == "CentOS" ]; then
if ! $(rpm -ql $PKG_NAME >/dev/null >&); then
echo no
else
echo yes
fi
elif [ $(os_version) == "Ubuntu" ]; then
if ! $(dpkg -l $PKG_NAME >/dev/null >&); then
echo no
else
echo yes
fi
fi
}
function install_pkg() {
local PKG_NAME=$
if [ $(os_version) == "CentOS" ]; then
if [ $(check_pkg $PKG_NAME) == "no" ]; then
yum install $PKG_NAME -y
if [ $(check_pkg $PKG_NAME) == "no" ]; then
color_echo green "The $PKG_NAME installation failure! Try to install again."
yum makecache
yum install $PKG_NAME -y
[ $(check_pkg $PKG_NAME) == "no" ] && color_echo red "The $PKG_NAME installation failure!" && exit
fi
fi
elif [ $(os_version) == "Ubuntu" ]; then
if [ $(check_pkg $PKG_NAME) == "no" ]; then
apt-get install $PKG_NAME -y
if [ $(check_pkg $PKG_NAME) == "no" ]; then
color_echo green "$PKG_NAME installation failure! Try to install again."
apt-get autoremove && apt-get update
apt-get install $PKG_NAME --force-yes -y
[ $(check_pkg $PKG_NAME) == "no" ] && color_echo red "The $PKG_NAME installation failure!" && exit
fi
fi
fi
}
function generate_keypair() {
if [ ! -e ~/.ssh/id_rsa.pub ]; then
color_echo green "The public/private rsa key pair not exist, start Generating..."
expect -c "
spawn ssh-keygen
expect {
\"ssh/id_rsa):\" {send \"\r\";exp_continue}
\"passphrase):\" {send \"\r\";exp_continue}
\"again:\" {send \"\r\";exp_continue}
}
" >/dev/null 2>&1
if [ -e ~/.ssh/id_rsa.pub ]; then
color_echo green "Generating public/private rsa key pair successfully."
else
color_echo red "Generating public/private rsa key pair failure!"
exit
fi
fi
} EXP_TMP_FILE=/tmp/expect_ssh.tmp if [[ $ =~ ^[a-z]+@[-]{,}\.[-]{,}\.[-]{,}\.[-]{,}@.* ]]; then
install_pkg expect ; generate_keypair
for i in $@; do
USER=$(echo $i|cut -d@ -f1)
IP=$(echo $i|cut -d@ -f2)
PASS=$(echo $i|cut -d@ -f3)
INFO=$USER@$IP
expect -c "
spawn ssh-copy-id $INFO
expect {
\"(yes/no)?\" {send \"yes\r\";exp_continue}
\"password:\" {send \"$PASS\r\";exp_continue}
}
" > $EXP_TMP_FILE # if login failed, login error info append temp file
check_ssh_auth
done
elif [[ $ =~ ^[a-z]+@[-]{,}\.[-]{,}\.[-]{,}\.[-]{,}-[-]{,}@.* ]]; then
install_pkg expect ; generate_keypair
START_IP_NUM=$(echo $|sed -r 's/.*\.(.*)-(.*)@.*/\1/')
END_IP_NUM=$(echo $|sed -r 's/.*\.(.*)-(.*)@.*/\2/')
for ((i=$START_IP_NUM;i<=$END_IP_NUM;i++)); do
USER=$(echo $|cut -d@ -f1)
PASS=$(echo $|cut -d@ -f3)
IP_RANGE=$(echo $|sed -r 's/.*@(.*\.).*/\1/')
IP=$IP_RANGE$i
INFO=$USER@$IP_RANGE$i
expect -c "
spawn ssh-copy-id $INFO
expect {
\"(yes/no)?\" {send \"yes\r\";exp_continue}
\"password:\" {send \"$PASS\r\";exp_continue}
}
" > $EXP_TMP_FILE
check_ssh_auth
done
else
echo "Example1: $0 <root@192.168.1.10-15@password>"
echo "Example2: $0 <root@192.168.1.10@password>"
echo "Example3: $0 [root@192.168.1.10@password root@192.168.1.11@password root@192.168.1.12@password ...]"
fi

 

Shell批量SSH免交互登录认证的更多相关文章

  1. shell编程-ssh免交互批量分发公钥脚本

    脚本基本原理 1.控制端免交互创建秘钥和公钥: 1 ssh-keygen -t rsa -f /root/.ssh/id_rsa -N "" 2.免交互发送公钥 1 sshpass ...

  2. Linux的SSH免密登录认证过程研究

    一.先看下SSH免密登录使用到的工具和生成的文件 工具:ssh-keygen用于生成秘钥文件,其中秘钥分为公钥和私钥.ssh-copy-id用于复制公钥文件到被控制机. 文件:ssh-keygen生成 ...

  3. ssh 免交互登录 ,远程执行命令脚本。

    ##免交互SSH登录auto_login_ssh () {    expect -c "set timeout -1;                spawn -noecho ssh -o ...

  4. 8.shell编程之免交互

    shell编程之免交互 目录 shell编程之免交互 Here Document免交互 免交互定义 Here Document变量设定 多行的注释 expect expect 定义 expect基本命 ...

  5. Shell脚本实现SSH免密登录及批量配置管理

    本节索引 场景分析 ssh免密登录 pssh工具批量管理 SHELL自动化脚本 本篇总结 场景分析 作为一个运维工程师,不是每个人工作的环境都想阿里.腾讯那样,动不动就上亿的PV量,上万台服务器.我们 ...

  6. 批量实现ssh免密登录

    本节索引 场景分析 ssh免密登录 pssh工具批量管理 SHELL自动化脚本 本篇总结 场景分析 作为一个运维工程师,不是每个人工作的环境都想阿里.腾讯那样,动不动就上亿的PV量,上万台服务器.我们 ...

  7. SSH免密登录(并且免yes交互)

    问题描述:主机A使用ssh协议远程主机B,默认会开启口令认证,即输入主机B对应用户的登录密码,并且第一次登录时,主机A需验证是否接受来自主机B的公钥,输入"yes/no"完成交互. ...

  8. ssh 免密码登录实现批量处理

    搭建集群的时候ssh 免密码登录是一个问题以下脚本将实现批量处理 文件1主机名:host 17.19.18.11:12317.19.18.12:123 文件2:ssh_setup.py #!/usr/ ...

  9. linux上ssh免密登录原理及实现

    因为我的服务器集群需要回收日志到中央进行统一处理,所以需要建立ssh互信关系实现免密登录.关于ssh的使用大家可能都很熟悉了,我们今天主要来讲下ssh连接和免密登录的原理. scp 传输文件 scp( ...

随机推荐

  1. python geohash算法逆地址编码原理初探

    1.geohash有什么用途呢?这几天刚好有个测试任务是关于设备信息位置处理的,里面提及到geohash:抱着测试的警觉性,打算研读一下这个geohash到底是什么?Geohash 是一种地理编码系统 ...

  2. ACM-ICPC 2018 焦作赛区网络预赛 G题 Give Candies

    There are NN children in kindergarten. Miss Li bought them NN candies. To make the process more inte ...

  3. HDU2833-WuKong(求不同起点,终点最短路的交点最多数量)

    Liyuan wanted to rewrite the famous book "Journey to the West" ("Xi You Ji" in C ...

  4. WebAPI之postman变量和session/token

    postman使用变量: 之前我们请求里面的主机地址都是localhost,表示本机.而在实际测试过程中,服务器地址往往并非是在本机上的,这时候就需要用到变量. postman支持多个测试环境,一个环 ...

  5. 《Java Spring框架》SpringXML配置详解

    Spring框架作为Bean的管理容器,其最经典最基础的Bean配置方式就是纯XML配置,这样做使得结构清晰明了,适合大型项目使用.Spring的XML配置虽然很繁琐,而且存在简洁的注解方式,但读懂X ...

  6. python中super的用法实例解析

    概念 super作为python的内建函数.主要作用如下: 允许我们避免使用基类 跟随多重继承来使用 实例 在单个继承的场景下,一般使用super来调用基类来实现: 下面是一个例子: class Ma ...

  7. JVM CPU Profiler技术原理及源码深度解析

    研发人员在遇到线上报警或需要优化系统性能时,常常需要分析程序运行行为和性能瓶颈.Profiling技术是一种在应用运行时收集程序相关信息的动态分析手段,常用的JVM Profiler可以从多个方面对程 ...

  8. Android 共享参数 SharedPreferences

    完成共享参数的读写 public class SharedPreference { private Context context; public SharedPreference(Context c ...

  9. Cocos Creator 资源加载流程剖析【六】——场景切换流程

    这里讨论场景切换的完整流程,从我们调用了loadScene开始切换场景,到场景切换完成背后发生的事情.整个流程可以分为场景加载和场景切换两部分,另外还简单讨论了场景的预加载. 加载场景的流程 load ...

  10. js 五种绑定彻底弄懂this,默认绑定、隐式绑定、显式绑定、new绑定、箭头函数绑定详解

     壹 ❀ 引 可以说this与闭包.原型链一样,属于JavaScript开发中老生常谈的问题了,百度一搜,this相关的文章铺天盖地.可开发好几年,被几道this题安排明明白白的人应该不在少数(我就是 ...