#!/bin/bash
# 获取系统版本
get_osname() {
os_name=`cat /etc/os-release|grep "^ID="|awk -F= '{print $2}'`
if [ "$os_name" == "ubuntu" ];then
return 0
elif [ "$os_name" == '"centos"' ];then
return 1
fi
}
check_out() {
if [ $? -eq 0 ];then
echo "expect install complete"
else
echo "expect install falied"
exit -1
fi
}
# 检查expect
EXPECT_CMD=`which expect 2>/dev/null`
if [ $? -ne 0 ]
then
echo "expect installing..."
if get_osname;then
apt -y install expect 2>&1 > /dev/null
check_out
else
yum -y install expect 2>&1 > /dev/null
check_out
fi
fi
# 检查.ssh
SSHKEY_RSA=/root/.ssh/id_rsa
[ ! -f ${SSHKEY_RSA} ] && ssh-keygen -t rsa -f ${SSHKEY_RSA} -P ""
PASSWD="password"
function sshcopyid
{
expect -c "
set timeout -1;
spawn ssh-copy-id $1;
expect {
\"yes/no\" { send \"yes\r\" ;exp_continue; }
\"password:\" { send \"$PASSWD\r\";exp_continue; }
};
"
}
for host in $@
do
sshcopyid $host
done
ssh免密配置
于 2023-09-14 21:08:34 首次发布