expect实现配置机器信任关系
利用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实现配置机器信任关系的更多相关文章
- ADFS 2.0 配置简介 PartⅡ – 配置 ADFS 信任关系
ADFS 与应用程序间的各种验证是基于信任关系的,在 ADFS 服务器配置好要信赖的应用程序(以 URL 为标识)后,应用程序再通过指定认证服务器来将用户引导至 ADFS 登录页,登录完成后再将用户的 ...
- AIX主机信任关系配置
1.配置主机信任关系的时候,需要先在两台主机/etc/hosts文件中添加要信任主机的IP,假设有(192.168.8.190 aix190,192.168.8.191 aix191)2个主机,在19 ...
- Solaris主机间的信任关系机制
解决问题: 管理员经常在其他服务器之间登录,是否需要密码切换. 知识点:主机间信任关系.R 命令集 /etc/hosts/equiv 文件 R服务是不加密的,别人可以破解. 主机名 + 用户名. + ...
- ssh两台机器建立信任关系无密码登陆
在建立信任关系之前先看看基于公钥.私钥的加密和认证. 私钥签名过程 消息-->[私钥]-->签名-->[公钥]-->认证 私钥数字签名,公钥验证 Alice生成公钥和私钥,并将 ...
- centos下建立双机信任关系
在有些情况下,我们希望在两台centos机器之间建立ssh连接的时候,可以不用输入密码.最常见的情况就是在使用脚本做数据库备份的时候.这种情况下,我们可以通过公钥/私钥来建立双机之间的信任关系. 网上 ...
- 批量实现多台服务器之间ssh无密码登录的相互信任关系
最近IDC上架了一批hadoop大数据业务服务器,由于集群环境需要在这些服务器之间实现ssh无密码登录的相互信任关系.具体的实现思路:在其中的任一台服务器上通过"ssh-keygen -t ...
- ssh 信任关系无密码登陆,清除公钥,批量脚本
实验机器: 主机a:192.168.2.128 主机b:192.168.2.130 实验目标: 手动建立a到b的信任关系,实现在主机a通过 ssh 192.168.2.130不用输入密码远程登陆b主机 ...
- 批量部署ssh信任关系
要求1:大批量部署SSH信任关系,在A文件分发服务器上大批量部署WEB层面信任关系文件分发服务器为:10.0.3.9 登录用户为:zhangsan WEB层IP段:10.0.3.10~10.0.3.6 ...
- Linux:两台服务器之间添加信任关系,进行远程操作的时候不需要输入密码
两台机器之间建立信任关系的步骤: 1. 在机器1上root用户执行ssh-keygen命令,生成建立安全信任关系的证书,直接Enter [root@CentOS64-x64 ~]# ssh-keyge ...
随机推荐
- 通达OA 一些工作流调整后带来的后果及应对措施
近期单位有个工作流须要改动,原因是最早设计时控件的字段设计不规范,控件直接使用了人员的名字来命名了.这不使用手机訪问时就出问题了,名字会直接显示出来,如今就须要进行调整. 调整初步有两个方案: 一是全 ...
- PHPMailer发送邮件乱码
PHPMailer发送邮件乱码, $mail->CharSet="GB2312";$mail->Encoding = "base64"; 设成这样不 ...
- Intel Naming Strategy--1
http://en.wikipedia.org/wiki/Mobile_Internet_device Computer sizes Classes of computers Larger S ...
- HDU 1398 Square Coins(母函数或dp)
Square Coins Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- Qt浅谈之二十一log调试日志
一.简单介绍 近期因调试code时,想了解程序的流程,但苦于没有一个简易的日志记录,不停使用qDebug打印输出,而终于提交代码时得去多次删除信息打印,有时还会出现新改动的代码分不清是哪些部分.而使用 ...
- 判断IPv6地址合法性
在 <netinet/in.h> 头文件下有下列这些宏用于判断IPv6地址合法性 返回0代表true,返回非零值代表ipv6地址为非指定类型的的地址(false) int IN6_IS_A ...
- 图像处理之基础---ffmpeg 中的图像缩放
http://blog.csdn.net/bweaglegao/article/details/8540860 http://www.cnblogs.com/acloud/archive/2011/1 ...
- GuozhongCrawler系列教程 (5) TransactionRequest具体解释
为了实现和维护并发抓取的属性信息提供线程安全的事务请求.TransactionRequest是一个抽象类自己不能设置Processor,却须要实现 TransactionCallBack接口.Tran ...
- AppFuse 3的乱码问题
书接上回:AppFuse 3常见问题与解决方法,一个新问题:乱码! 在3.0.0版本号下,运行mvn appfuse:full-source命令后使用mvn jetty:run启动工程,页面上就出现了 ...
- sanic官方文档解析之蓝图
1,蓝图(Blueprints) 蓝图可用于子路由的应用,代替增加路由的存在,蓝图的定义和增加路由的方法相似,灵活的在应用中注册,并且可插拔的方式. 尤其是在大型应用中使用蓝图的时候在你逻辑打断的地方 ...