利用expect的交互功能,自动配置信任机器之间的信任关系。

代码里会判断机器是否生成了秘钥,如果没有生成过,则自动帮助你执行 ssh-keygen

ssh_expect.sh 程序依赖expect 命令,用户可以通过同路径的 hosts.properties 文件配置需要设置的信任关系

ssh_expect.sh 程序源码

#!/bin/sh

HOSTS_PROPERTIES="hosts.properties"

expect_ssh_copy_id()
{
if [ "$#" -ne "" ]; then
echo "expect_ssh_copy_id <remoteUser> <remoteHostname> <password> <localUserhome> <timeout>";
exit ;
fi
local remoteUser=$
local remoteHostname=$
local password=$
local localUserhome=$
local timeout=$ expect -c "
set timeout $timeout
spawn ssh-copy-id -i $localUserhome/.ssh/id_rsa.pub $remoteUser@$remoteHostname
expect {
\"*yes/no\" { send \"yes\r\"; exp_continue }
\"*assword:\" { send \"$password\r\" }
}
expect eof
" > /dev/null 2>&1 } expect_ssh_keygen()
{
if [ "$#" -ne "" ]; then
echo "expect_ssh_keygen <localUserhome> <timeout>";
exit ;
fi
local localUserhome=$;
local timeout=$;
if [ -f ${localUserhome}/.ssh/id_rsa.pub -a -f ${localUserhome}/.ssh/id_rsa ] ; then
echo -e "\t${localUserhome}/.ssh has created id_rsa.pub and id_rsa"
else
echo -e "\t${localUserhome}/.ssh has not created id_rsa.pub and id_rsa.pub"
expect -c "
set timeout $timeout
spawn ssh-keygen
expect {
\"*save the key*id_rsa*\" {send \"\r\"; exp_continue }
\"*verwrite*y/n*\" { send \"y\r\"; exp_continue }
\"*passphrase*passphrase*\" { send \"\r\"; exp_continue }
\"*same passphrase*\" {send \"\r\" }
}
expect eof
exit
" > /dev/null 2>&1
if [ "$?" -eq "" ] ; then
echo -e "\tcreate id_rsa.pub,id_rsa successfully"
else
echo -e "\tcreate id_rsa.pub,id_rsa faild"
fi
fi } configure_trust_relation()
{
if [ "$#" -ne "" ]; then
echo "configure_trust_relation <remoteUser> <remoteHostname> <password> <localUserhome> <timeout>";
exit ;
fi
local remoteUser=$
local remoteHostname=$
local password=$
local localUserhome=$
local timeout=$ expect -c " set timeout $timeout
set trust true
set passwdRight true #
# checking remote machine is be trusted
# if trust, return
# if not trust, return
# if passwd wrong, return
#
spawn ssh $remoteUser@$remoteHostname expect {
\"*yes/no\" { send \"yes\r\" ; exp_continue }
\"*assword:\" { send \"$password\r\" ; set trust false }
} expect {
\"*assword:\" { send \"\003\" ; set passwdRight false }
} if { \"\$passwdRight\" == \"false\" } {
expect eof
exit
} expect { *\$* } send \"exit\r\"
sleep
if { \"\$trust\" == \"false\" } {
expect eof
exit
}
expect eof
exit
" > /dev/null 2>&1 rn=$?
if [ "$rn" -eq "" ] ; then
echo -e "\t${remoteUser}@${remoteHostname} is not be trusted, then exec ssh-copy-id to remote machine"
#expect_ssh_keygen $localUserhome $timeout
expect_ssh_copy_id $remoteUser $remoteHostname $password $localUserhome $timeout
else if [ "$rn" -eq "" ] ; then
echo -e "\t${remoteUser}@${remoteHostname} is be trusted"
else if [ "$rn" -eq "" ] ; then
#echo -e "\t${remoteHostname}@${remoteUser}'s passwd is wrong"
echo -e "\t@@@@@@@ ERROR @@@@@@@"
echo -e "\t@@@@@@@ [${remoteUser}@${remoteHostname}] passwd is wrong @@@@@@@"
fi
fi
fi
} function configure_all_hosts()
{ local localUserhome=$
local timeout=$ expect_ssh_keygen $localUserhome $timeout
cat ${filepath}/${hosts_properties} | grep -v "<" | grep -v "#" | while read line
do
local remoteHostname=$(echo $line | awk -F ' ' '{print $1}')
local remoteUser=$(echo $line | awk -F ' ' '{print $2}')
local password=$(echo $line | awk -F ' ' '{print $3}') echo "************* [ $remoteUser@$remoteHostname ] *************" ping -c $remoteHostname > /dev/null >& if [ a"$?" != "a0" ] ; then
echo -e "\t@@@@@@@ ERROR @@@@@@@"
echo -e "\t@@@@@@@ [$remoteHostname] cannot be connected @@@@@@@"
continue;
fi configure_trust_relation $remoteUser $remoteHostname $password $localUserhome $timeout done } main()
{ echo "************* [ BEGIN ] *************"
which expect > /dev/null >&
if [ "$?" -ne "" ]; then
echo "expect is not exists"
exit ;
fi hosts_properties=${HOSTS_PROPERTIES}
filepath=$( cd "$(dirname $0)"; pwd; )
localUserhome=$(cd ~;pwd;);
timeout=; configure_all_hosts ${localUserhome} ${timeout}; echo "************* [ OVER ] *************"
} main

hosts.properties 文件内容

#<hostname>   <user>      <password>
r730xd-c1 sdbadmin sdbadmin
r730xd-c2 sdbadmin sdbadmin
chen root root123
r730xd-c3 sdbadmin sdbadmin
chen2 root root1907
r730xd-c2 root root1907

程序执行方法

sh ssh_expect.sh

输出

************* [ BEGIN ] *************
/home/sdbadmin/.ssh has not created id_rsa.pub and id_rsa.pub
create id_rsa.pub,id_rsa successfully
************* [ sdbadmin@r730xd-c1 ] *************
sdbadmin@r730xd-c1 is not be trusted, then exec ssh-copy-id to remote machine
************* [ sdbadmin@r730xd-c2 ] *************
sdbadmin@r730xd-c2 is not be trusted, then exec ssh-copy-id to remote machine
************* [ root@chen ] *************
@@@@@@@ ERROR @@@@@@@
@@@@@@@ [chen] cannot be connected @@@@@@@
************* [ sdbadmin@r730xd-c3 ] *************
sdbadmin@r730xd-c3 is not be trusted, then exec ssh-copy-id to remote machine
************* [ root@chen2 ] *************
@@@@@@@ ERROR @@@@@@@
@@@@@@@ [chen2] cannot be connected @@@@@@@
************* [ root@r730xd-c2 ] *************
root@r730xd-c2 is not be trusted, then exec ssh-copy-id to remote machine
************* [ OVER ] *************

expect实现配置机器信任关系的更多相关文章

  1. ADFS 2.0 配置简介 PartⅡ – 配置 ADFS 信任关系

    ADFS 与应用程序间的各种验证是基于信任关系的,在 ADFS 服务器配置好要信赖的应用程序(以 URL 为标识)后,应用程序再通过指定认证服务器来将用户引导至 ADFS 登录页,登录完成后再将用户的 ...

  2. AIX主机信任关系配置

    1.配置主机信任关系的时候,需要先在两台主机/etc/hosts文件中添加要信任主机的IP,假设有(192.168.8.190 aix190,192.168.8.191 aix191)2个主机,在19 ...

  3. Solaris主机间的信任关系机制

    解决问题: 管理员经常在其他服务器之间登录,是否需要密码切换. 知识点:主机间信任关系.R 命令集 /etc/hosts/equiv 文件 R服务是不加密的,别人可以破解. 主机名 + 用户名. + ...

  4. ssh两台机器建立信任关系无密码登陆

    在建立信任关系之前先看看基于公钥.私钥的加密和认证. 私钥签名过程 消息-->[私钥]-->签名-->[公钥]-->认证 私钥数字签名,公钥验证 Alice生成公钥和私钥,并将 ...

  5. centos下建立双机信任关系

    在有些情况下,我们希望在两台centos机器之间建立ssh连接的时候,可以不用输入密码.最常见的情况就是在使用脚本做数据库备份的时候.这种情况下,我们可以通过公钥/私钥来建立双机之间的信任关系. 网上 ...

  6. 批量实现多台服务器之间ssh无密码登录的相互信任关系

    最近IDC上架了一批hadoop大数据业务服务器,由于集群环境需要在这些服务器之间实现ssh无密码登录的相互信任关系.具体的实现思路:在其中的任一台服务器上通过"ssh-keygen -t ...

  7. ssh 信任关系无密码登陆,清除公钥,批量脚本

    实验机器: 主机a:192.168.2.128 主机b:192.168.2.130 实验目标: 手动建立a到b的信任关系,实现在主机a通过 ssh 192.168.2.130不用输入密码远程登陆b主机 ...

  8. 批量部署ssh信任关系

    要求1:大批量部署SSH信任关系,在A文件分发服务器上大批量部署WEB层面信任关系文件分发服务器为:10.0.3.9 登录用户为:zhangsan WEB层IP段:10.0.3.10~10.0.3.6 ...

  9. Linux:两台服务器之间添加信任关系,进行远程操作的时候不需要输入密码

    两台机器之间建立信任关系的步骤: 1. 在机器1上root用户执行ssh-keygen命令,生成建立安全信任关系的证书,直接Enter [root@CentOS64-x64 ~]# ssh-keyge ...

随机推荐

  1. Computer form factor

    http://en.wikipedia.org/wiki/Motherboard_form_factor Computer form factor From Wikipedia, the free e ...

  2. [LeetCode] 038. Count and Say (Easy) (C++/Python)

    索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 038. Cou ...

  3. npm的常用配置

    第一次使用需要初始化 npm init -y npm-tut { "requires": true, "lockfileVersion": 1, "d ...

  4. DRP——Dom4j使用

    dom4j是一个Java的XMLAPI,类似于jdom.用来读写XML文件的.dom4j是一个很很优秀的JavaXMLAPI.具有性能优异.功能强大和极端易用使用的特点.Dom4j是一个易用的.开源的 ...

  5. 杭电1596find the safest road(spfa)

    find the safest road Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  6. sanic官方文档解析之Example(一)

    1,示例 这部的文档是简单的示例集合,它能够帮助你快速的启动应用大部分的应用,这些应用大多事分类的,并且提供给ini工作的连接代码: 1.1,基础示例 这部分示例集成了提供简单sanic简单的代码 单 ...

  7. oracle的索引有几种?各有何用途?

    1. b-tree索引Oracle数据库中最常见的索引类型是b-tree索引,也就是B-树索引,以其同名的计算科学结构命名.CREATE INDEX语句时,默认就是在创建b-tree索引.没有特别规定 ...

  8. bash shell和进程

    1 exec builtin 不创建子shell,在原进程的上启动新的脚本,但是它会把老shell的环境清理掉,所以,它从原shell中什么也不继承,在一个干净的环境中执行新的脚本.执行完之后退出当前 ...

  9. 【POJ3740】Easy Finding DLX(Dancing Links)精确覆盖问题

    题意:多组数据,每组数据给你几行数,要求选出当中几行.使得每一列都有且仅有一个1.询问是可不可行,或者说能不能找出来. 题解:1.暴搜.2.DLX(Dancing links). 本文写的是DLX. ...

  10. Why you shouldn’t connect your mobile application to a database

    BY CRAIG CHAPMAN · PUBLISHED 2015-07-02 · UPDATED 2015-07-02   Working at Embarcadero, I frequently ...