Linux批量执行SSH命令

网友投稿 1464 2022-09-01

Linux批量执行SSH命令

Linux批量执行SSH命令

[root​​@node02​​:/tmp]$ cat except.sh

#!/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=$1PASSWORD=$2HOSTS_FILE=$3if [ $# -ne 3 ]; then echo "Usage:" echo "$0 remoteUser remotePassword hostsFile" exit 1fiSSH_DIR=~/.sshSCRIPT_PREFIX=./tmpecho ===========================# 1. prepare directory .sshmkdir $SSH_DIRchmod 700 $SSH_DIR# 2. generat ssh keyTMP_SCRIPT=$SCRIPT_PREFIX.shecho "#!/usr/bin/expect">$TMP_SCRIPTecho "spawn ssh-keygen -b 1024 -t rsa">>$TMP_SCRIPTecho "expect *key*">>$TMP_SCRIPTecho "send /r">>$TMP_SCRIPTif [ -f $SSH_DIR/id_rsa ]; then echo "expect *verwrite*">>$TMP_SCRIPT echo "send y/r">>$TMP_SCRIPTfiecho "expect *passphrase*">>$TMP_SCRIPTecho "send /r">>$TMP_SCRIPTecho "expect *again:">>$TMP_SCRIPTecho "send /r">>$TMP_SCRIPTecho "interact">>$TMP_SCRIPTchmod +x $TMP_SCRIPT/usr/bin/expect $TMP_SCRIPTrm $TMP_SCRIPT# 3. generat file authorized_keyscat $SSH_DIR/id_rsa.pub>>$SSH_DIR/authorized_keys# 4. chmod 600 for file authorized_keyschmod 600 $SSH_DIR/authorized_keysecho ===========================# 5. copy all files to other hostsfor 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." fidoneecho done.

2.将需要执行ssh免密的IP地址放入

[root@node02:/tmp]$ cat sshhostList.cfg192.168.56.100192.168.56.101

3.执行命令

./except.sh ${username} ${password} sshhostList.cfg如果需要实现root用户的免密,则执行命令./except.sh root rootpassword sshhostList.cfg如果需要实现oracle用户的免密,则执行命令./except.sh oracle oraclepassword sshhostList.cfg

oracle,linux

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:shell实现加密压缩文件自动解压
下一篇:关于php开启错误提示的总结(php常见错误)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~