[root@node02:/tmp]$ cat except.shhttps://www.cndba.cn/hbhe0316/article/4905
https://www.cndba.cn/hbhe0316/article/4905
https://www.cndba.cn/hbhe0316/article/4905
#!/bin/sh
###############################################################################################################
#Script name: except.sh
#Script description: ssh login another system.
#Current Release Version: 1.0.0
#Script Owner: hbhe0316
#Latest editor: hbhe0316
#Support platform: Linux OS for redhat and centos.
#Date: 2021/8/26---first Version for except.
#Change log:
#Descript:./except.sh ${username} ${password} ${sshhostList.cfg}
#
###############################################################################################################
DEST_USER=$1
PASSWORD=$2
HOSTS_FILE=$3
if [ $# -ne 3 ]; then
echo "Usage:"
echo "$0 remoteUser remotePassword hostsFile"
exit 1
fi
SSH_DIR=~/.ssh
SCRIPT_PREFIX=./tmp
echo ===========================
# 1. prepare directory .ssh
mkdir $SSH_DIR
chmod 700 $SSH_DIR
# 2. generat ssh key
TMP_SCRIPT=$SCRIPT_PREFIX.sh
echo "#!/usr/bin/expect">$TMP_SCRIPT
echo "spawn ssh-keygen -b 1024 -t rsa">>$TMP_SCRIPT
echo "expect *key*">>$TMP_SCRIPT
echo "send /r">>$TMP_SCRIPT
if [ -f $SSH_DIR/id_rsa ]; then
echo "expect *verwrite*">>$TMP_SCRIPT
echo "send y/r">>$TMP_SCRIPT
fi
echo "expect *passphrase*">>$TMP_SCRIPT
echo "send /r">>$TMP_SCRIPT
echo "expect *again:">>$TMP_SCRIPT
echo "send /r">>$TMP_SCRIPT
echo "interact">>$TMP_SCRIPT
chmod +x $TMP_SCRIPT
/usr/bin/expect $TMP_SCRIPT
rm $TMP_SCRIPT
# 3. generat file authorized_keys
cat $SSH_DIR/id_rsa.pub>>$SSH_DIR/authorized_keys
# 4. chmod 600 for file authorized_keys
chmod 600 $SSH_DIR/authorized_keys
echo ===========================
# 5. copy all files to other hosts
for ip in $(cat $HOSTS_FILE)
do
if [ "x$ip" != "x" ]; then
echo -------------------------
TMP_SCRIPT=${SCRIPT_PREFIX}.$ip.sh
# check known_hosts
val=`ssh-keygen -F $ip`
if [ "x$val" == "x" ]; then
echo "$ip not in $SSH_DIR/known_hosts, need to add"
val=`ssh-keyscan $ip 2>/dev/null`
if [ "x$val" == "x" ]; then
echo "ssh-keyscan $ip failed!"
else
echo $val>>$SSH_DIR/known_hosts
fi
fi
echo "copy $SSH_DIR to $ip"
echo "#!/usr/bin/expect">$TMP_SCRIPT
echo "spawn scp -r $SSH_DIR $DEST_USER@$ip:~/">>$TMP_SCRIPT
echo "expect *assword*">>$TMP_SCRIPT
echo "send $PASSWORD/r">>$TMP_SCRIPT
echo "interact">>$TMP_SCRIPT
chmod +x $TMP_SCRIPT
#echo "/usr/bin/expect $TMP_SCRIPT" >$TMP_SCRIPT.do
#sh $TMP_SCRIPT.do&
/usr/bin/expect $TMP_SCRIPT
rm $TMP_SCRIPT
echo "copy done."
fi
done
echo done.
2.将需要执行ssh免密的IP地址放入https://www.cndba.cn/hbhe0316/article/4905https://www.cndba.cn/hbhe0316/article/4905https://www.cndba.cn/hbhe0316/article/4905https://www.cndba.cn/hbhe0316/article/4905https://www.cndba.cn/hbhe0316/article/4905https://www.cndba.cn/hbhe0316/article/4905
[root@node02:/tmp]$ cat sshhostList.cfg
192.168.56.100
192.168.56.101
3.执行命令
https://www.cndba.cn/hbhe0316/article/4905
./except.sh ${username} ${password} sshhostList.cfg
如果需要实现root用户的免密,则执行命令
./except.sh root rootpassword sshhostList.cfg
如果需要实现oracle用户的免密,则执行命令
./except.sh oracle oraclepassword sshhostList.cfg
版权声明:本文为博主原创文章,未经博主允许不得转载。
oracle,linux