利用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. Ikki&#39;s Story IV - Panda&#39;s Trick (poj 3207 2-SAT)

    Language: Default Ikki's Story IV - Panda's Trick Time Limit: 1000MS   Memory Limit: 131072K Total S ...

  2. C语言printf()函数具体解释和安全隐患

    一.问题描写叙述 二.进一步说明 请细致注意看,有例如以下奇怪的现象 int a=5; floatx=a; //这里转换是没有问题的.%f打印x是 5.000000 printf("%d\n ...

  3. [游戏]L4D求生之路官方比赛地图修补完好说明

    游戏模式:L4D求生之路4356(1.0.2.1)药抗比赛模式 更新日期:2015.06.04 -----毫不留情01----- 1.开局补给手枪 -----毫不留情02----- 1.开局补给手枪 ...

  4. 光纤与PON基础概念整理

    近期有幸接触到通讯这一领域,此文专门用于整理记录. 首先是光纤与光缆  光纤(OpticalFiber,OF)是用来导光的透明介质纤维,一根有用化的光纤是由多层透明介质构成,一般能够分为三部分:折射率 ...

  5. Jenkins+maven+SVN+Tomcat部署过程

    一.下载地址 应首先确认安装了JDK: Jenkins下载地址:http://mirrors.shu.edu.cn/jenkins/windows-stable/jenkins-2.107.3.zip ...

  6. 使网页适应UIWebView的宽度

    比較简单的做法是:在- (void)webViewDidFinishLoad:这种方法中,改动JavaScript的值: //UIWebViewDelegate - (void)webViewDidF ...

  7. php输出echo、print、print_r、printf、sprintf、var_dump比较

    php输出echo.print.print_r.printf.sprintf.var_dump比较 一.echo    echo() 实际上不是一个函数,是php语句,因此您无需对其使用括号.不过,如 ...

  8. Django 之 form表单

    Django中的Form表单 1.背景 平时我们在书写form表单时,经常都是手动的去写一些input标签,让用户输入一些功能,进行一些校验的判断,等等.Django中的form表单就能够帮我们去实现 ...

  9. gRPC错误码 http状态码 provide your APIs in both gRPC and RESTful style at the same time

    How gRPC error codes map to HTTP status codes in the response https://github.com/grpc-ecosystem/grpc ...

  10. javascript 阻止事件冒泡 cancelBubble

    javascript简单的阻止事件冒泡,可以使用事件的cancelBubble方法为true: html部分 <button id="btn1">点击显示div< ...