auto.sh expect
#!/bin/bash starttime=$(date +%s) rm -rf ip.txt for i in 168.17.1.{2..253}; do ping -W 1 -w 0.05 -c 1 $i &>/dev/null && echo $i is up && echo $i >> ip.txt & done wait endtime=$(date +%s) totaltime=$(( $endtime - $starttime )) echo 开始时间:$(date -d @"${starttime}" "+%Y-%m-%d %H:%M:%S") echo 开始时间:$(date -d @"$endtime" "+%Y-%m-%d %H:%M:%S") echo 消耗总时间:${totaltime}s
#!/bin/bash set -ex #生成公钥和私钥 ssh-keygen -q -t rsa -N '' -f /root/.ssh/id_rsa &>/dev/null <<< y #IP=( 168.17.1.99 168.17.1.199 168.19.5.39 ) IP=( $(cat /root/ip.txt) ) username=root password=huawei@123 yum install -y nc &>/dev/null function aa(){ for i in ${IP[*]} do { host=$i if [ "$(nc -i 2 -w 2 $i 22 && echo yes || echo no)" == "no" ];then continue else /usr/bin/expect <<EOF set timeout 5 spawn ssh-copy-id -i /root/.ssh/id_rsa.pub $username@$host expect { "*yes/no*" {send "yes\r";exp_continue;} "*assword*" {send "$password\r"} } expect eof EOF /usr/bin/expect <<EOF set timeout 5 spawn ssh $username@$host "ip -o -4 addr" expect { "*yes/no*" {send "yes\r";exp_continue;} "*assword*" {send "$password\r"} } EOF fi } & done wait } rm -rf host.txt aa | grep "Now try logging" | awk '{print $8" "$9}'| sed 's#"##g'| sed "s#'##g" >> host.txt ~
#!/bin/bash set -ex localip=$(ip -o -4 addr | grep "scope global"| awk '{print $4}'|awk -F "/" '{print $1}') host=$(cat host.txt | awk -F "@" '{print $2}') cmdname=$1 for i in ${host} do if [[ $i != "$localip" ]];then ssh root@$i $cmdname $2 $3 fi done
#!/bin/bash localip=$(ip -o -4 addr | grep "scope global"| awk '{print $4}'|awk -F "/" '{print $1}') host=$(cat host.txt | awk -F "@" '{print $2}') filename=$1 dstname=$2 for i in ${host} do if [[ $i != "$localip" ]];then scp $filename root@$i:$dstname fi done ~