[root@c3-zabbix-serv hurl]# cat sendsshkey.sh 
 
#!/bin/bash 
if [ -z $1 ]; then
echo "Usage  : sh $0 username passwd"
echo "Example: sh $0 root 'passwd!@#2015'" 
fi 
function sendSSHKey(){ 
SSH='ssh-copy-id -i' 
HOME_DIR=`cd ~ && pwd` 
KEYSTORE=$HOME_DIR/.ssh/id_rsa.pub 
# $SSH $KEYSTORE ${1}@${2}
# 三个参数是'user'--'ip'--'password'
echo "========$#  -- $1 -- $2 -- $3 ========="
if [ ! $2 ] || [ ! $3 ] ;then
    echo "ip/password not found !" 
    exit
else
expect -c "
    spawn $SSH $KEYSTORE ${1}@${2}
    expect {
    \"*yes/no*\" {send \"yes\r\"; exp_continue}
    \"*password*\" {send \"$3\r\"; exp_continue}
    \"*Password*\" {send \"$3\r\";}
}
"
echo -e "\033[40;32m send sshkey to $2 success \033[0m\n"
fi
#读取一行数据,用i做形参
cat ./hostlist.txt |while read i 
do
USER=$1
#从'hostlist.txt'文件中寻找IP
IP=`echo $i | awk '{print $1}'`
#echo " $# -- $1 --  $2"
#如果有一个参数,则从'hostlist.txt'文件中寻找密码
if [ $# -eq 1 ];then
    PASSWORD=`echo $i | awk '{print $2}'`
#如果有两个参数,把第二个参数当作密码
elif [ $# -eq 2 ];then
    PASSWORD=$2
else
    exit 
fi
sendSSHKey $USER $IP $PASSWORD
done 
 
====================================
使用命令:

批量配置SSH 免密钥登录脚本的更多相关文章

  1. linux配置ssh免密钥登录

    https://blog.csdn.net/xiaoyi23000/article/details/80597516 1.执行命令ssh-keygen -t rsa,生成公钥和私钥 2.会在当前用户的 ...

  2. linux系统ssh免密钥登录配置

    linux主机配置ssh免密钥登录,具体配置如下: 1.执行命令ssh-keygen -t rsa,生成公钥和私钥(具体步骤详见下图) 2.会在当前用户的家目录的.ssh/生成公钥和私钥, 3.执行s ...

  3. gitlab用户添加ssh免密钥认证后clone还是要求输入密码

    今天在centos 7公网服务器上安装gitlab在配置ssh免密钥时遇到一个奇怪的事,正确添加了本机的公钥到gitlab账户上,进行clone时死活都要你输入密码gitlab使用yum安装的,之前在 ...

  4. Xshell配置ssh免密码登录-密钥公钥(Public key)与私钥(Private Key)登录【已成功实例】

    本文转自https://blog.csdn.net/qjc_501165091/article/details/51278696 ssh登录提供两种认证方式:口令(密码)认证方式和密钥认证方式.其中口 ...

  5. 配置Ssh免密码登录

    配置Ssh免密码登录 一个master节点,两个client节点(client1.client2) 1.所有节点创建hadoop用户,并设置密码 以root账号登录: useradd hadoop p ...

  6. Xshell配置ssh使用密钥公钥(publice key)登录

    ssh登录提供两种认证方式:口令(密码)认证方式和密钥认证方式.其中口令(密码)认证方式是我们最常用的一种,这里介绍密钥认证方式登录到linux/unix的方法. 使用密钥登录分为3步:1.生成密钥( ...

  7. CentOS 6.5配置SSH免密码登录

    centos 系统对权限的设置非常微妙,如果权限设置大了则ssh 拒绝,如果权限小了,则ssh 更是被拒绝(我曾经配置好久没有打通,就是因为权限过大的原因) 参考链接:http://www.linux ...

  8. Linux配置SSH免密码登录

    CentOS配置SSH免密码登录为例说明:SSH远程登录的安全外壳协议有两种身份认证机制: - 用户名+密码 -密钥登录 环境准备 host1:192.168.0.10host2:192.168.0. ...

  9. Linux - 配置SSH免密通信 - “ssh-keygen”的基本用法

    目录 1 什么是SSH 2 配置SSH免密登录 2.1 安装必需的软件 2.2 ssh-keygen创建公钥-私钥对 2.3 ssh-copy-id把A的公钥发送给B 2.4 在A服务器上免密登录B服 ...

随机推荐

  1. spring加载classpath与classpath*的区别别

    1.无论是classpath还是classpath*都可以加载整个classpath下(包括jar包里面)的资源文件. 2.classpath只会返回第一个匹配的资源,查找路径是优先在项目中存在资源文 ...

  2. python学习笔记(3)——进制符号&转换公式

    进制转换法则: 进制符号  bin().oct().hex().int('',进制)+待转格式数 10进制→其他进制 # dec2bin # 十进制 to 二进制: bin() >>> ...

  3. zabbix3.0_网络发现问题

    问题1. Zabbix网络发现system.uanem找不到主机,打开zabbix_server.conf文件的debug DebugLevel=5 # 错误信息如下 # item [system.u ...

  4. POSIX多线程—概述

    作者:阿波链接:http://blog.csdn.net/livelylittlefish/article/details/7918110 (整半年没有更新,发几篇以前的读书笔记.) Content ...

  5. docker常用命令理解

    docker help Commands: attach Attach local standard input, output, and error streams to a running con ...

  6. 16 this和super和构造代码块

    this关键词---当前类的对象的引用 public class Public { String name; int age; public static void main(String[] arg ...

  7. 15Oracle Database 索引

    Oracle Database 索引 索引 索引的目的是加快查询速度,就像一本数据的目录一样.建立索引的原则:非常少的DML操作:经常出现在where语句中的字段 2.20.1.建立索引 l  对t_ ...

  8. [Algorithm] 7. Serialize and Deserialize Binary Tree

    Description Design an algorithm and write code to serialize and deserialize a binary tree. Writing t ...

  9. python_ 学习笔记(运算符)

    python的运算符基本和C语言一致,以下说一些不一样的! 算术运算符 **:代表乘方,对应也有**=: //:代表商向下取整,对应也有//=: 逻辑运算符 and or not 位运算符 :& ...

  10. 59.关系型与document类型数据模型对比

    现假设有如下两个类: class Department(object): def __init__(self, dept_id, name, desc, employees=[]): self.dep ...