利用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. android 内部文件读取

    Android 文件管理方法 Android使用的是基于Linux的文件系统,对于文件的訪问和管理是通过权限设置来限制的. 在Linux系统中,文件权限分别描写叙述了创建者.同组用户和其它用户对文件的 ...

  2. 自己动手写CPU之第九阶段(7)——MIPS32中的LL、SC指令说明

    将陆续上传新书<自己动手写CPU>,今天是第46篇. 在MIPS32指令集中有两条特殊的存储载入指令:链接载入指令LL.条件存储指令SC,本次将介绍这两条指令.在兴许将实现这两条指令. 9 ...

  3. openwrt hotplug

    由内核发出 event 事件. kobject_uevent() 产生 uevent 事件(lib/kobject_uevent.c 中), 产生的 uevent 先由 netlink_broadca ...

  4. 翻译:A Tutorial on the Device Tree (Zynq) -- Part IV

    获取资源信息 内核模块驱动加载之后,就开始把硬件资源管理起来,如读写寄存器.接收中断. 来看看设备树里的一条: xillybus_0: xillybus@50000000 { compatible = ...

  5. 使用Axis2开发WebService

    一.准备 1.下载Axis2.eclipse插件 axis2-1.6.2-war.zip: http://mirror.bjtu.edu.cn/apache//axis/axis2/java/core ...

  6. 【bzoj4240】有趣的家庭菜园

    只要统计每一个左右分别有多少比他高的去min,然后求和 #include<algorithm> #include<iostream> #include<cstdlib&g ...

  7. Erlang Garbage Collector

    Erlang Garbage Collector | Erlang Solution blog https://www.erlang-solutions.com/blog/erlang-garbage ...

  8. KbmMW资源汇总(特别是xalion的文章)

    KbmMW框架是收费的,不在此提供下载,如需购买,请自行联系作者Kim Madsen. 网址资源: 官网主页:http://www.components4programmers.com/product ...

  9. 2款JS脚本判断手机浏览器跳转WAP手机网站

    随着移动设备的普及,企业的网络宣传已经不能局限在PC端,而需要同时在移动端有所建树.对于公司网站来说,以前都是做的PC端的,当然手机等移动端也可以访问,但是用户体验肯定不如完全适合的手机端来的方便.我 ...

  10. silverlight中 ComboBox绑定数据库,并获取当前选定值

    silverlight中 ComboBox绑定数据库,并获取当前选定值 在silverlight中 用combobox下拉菜单绑定数据库的方法和用DataGrid绑定数据库的方法类似. page.xa ...