expect脚本实现ssh互信

ssh_trust.exp

#!/usr/bin/expect

proc ssh-keygen {} {
	spawn ssh-keygen -q -P "" -t rsa -f "/root/.ssh/id_rsa"
	
	expect {
		-timeout 3
		"verwrite (y/n)?" {send "n\r";return 0}
		timeout {return 1}
	}
	catch wait result
	return [lindex $result 3]
}

proc ssh-copy-id {user host password} {
	spawn ssh-copy-id $user@$host

	expect {
		-timeout 5
		"(yes/no)?" {send "yes\r";exp_continue}
		"ermission denied" {return 1}
		"assword:" {send "$password\r";exp_continue}
		"already exist" {return 0}
		"IDENTIFICATION HAS CHANGED!" {
			wait
			exec ssh-keygen -R $host
			ssh-copy-id $user $host $password
			return 0
		}
		timeout {return 1}
	}
	catch wait result
	return [lindex $result 3]
}

set ret [ssh-keygen]
if {$ret} {
	exit $ret
}


set ret [ssh-copy-id [lindex $argv 0] [lindex $argv 1] [lindex $argv 2]]
if {$ret} {
	exit $ret
}

install_mongo.sh

#!/bin/bash -xv

set -o errexit
set -o nounset

[ -d /trace ] || mkdir /trace
g_log="/trace/install_mongo.log"
declare -a g_arr

function logger()
{
	local level=$1
	local msg=$2
	local line=$3
	echo $(date +"%Y-%m-%d %T.%3N") ${BASH_SOURCE[0]}:$line [$level] $msg
}

function ssh_trust()
{
	which expect || yum -y install expect
	
	for line in "${g_arr[@]}"
	do
		read host user password instance_name port base_path <<< $line
		./ssh_trust.exp $user $host $password &
	done
	
	local status=0
	local ret=0
	for pid in $(jobs -p)
	do
		wait $pid
		status=$?
		[[ $status == 0 ]] || ret=$status
	done
	return $ret
}

function parse_cfg()
{
	logger "INFO" $FUNCNAME $LINENO
	
	while read line || [[ -n $line ]]
	do
		[[ $(echo $line | awk '{print $1}') == 'host' ]] && continue
		g_arr+=("$line")
	done < install_mongo.cfg
}

parse_cfg && ssh_trust

install_mongo.cfg

host				user	password	instance_name	port	base_path
192.168.122.133		root	123456		mongod			35001	/data/mongodb
192.168.122.135		root	123456		mongod			35001	/data/mongodb
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值