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