cat>ipscan<<'EOFABC'
#!/bin/bash
CDDIR="$(cd "$(dirname "$0")" ; pwd -P)"
NDIR="$CDIR/.."
starttime=$(date +%s)
rm -rf ${CDDIR}/.ip.txt
for i in 168.17.1.{2..253};
do
{
ping -W 1 -w 1 -c 1 $i &>/dev/null && echo $i is up && echo $i >> ${CDDIR}/.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
EOFABC
cat>autossh<<'EOFABD'
#!/bin/bash
set -ex
CDDIR="$(cd "$(dirname "$0")" ; pwd -P)"
NDIR="$CDIR/.."
IP=( $(cat $CDDIR/.ip.txt) )
username=root
password=huawei@123
########################################################
#生成公钥和私钥
rm -rf /root/.ssh/*
ssh-keygen -q -t rsa -N '' -f /root/.ssh/id_rsa &>/dev/null <<< y
########################################################
function main(){
rpm -qa expect || yum install -y expect &>/dev/null
rpm -qa nc || yum install -y nc &>/dev/null
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 $username@$host "rm -rf /root/.ssh/authorized_keys"
expect {
"*yes/no*" {send "yes\r";exp_continue;}
"*assword*" {send "$password\r"}
}
expect eof
EOF
/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 ${CDDIR}/.host.txt
main | grep "Now try logging" | awk '{print $8" "$9}'| sed 's#"##g'| sed "s#'##g" >> ${CDDIR}/.host.txt
EOFABD
cat>remotebash<<'EOGG'
#!/bin/bash
set -ex
CDDIR="$(cd "$(dirname "$0")" ; pwd -P)"
NDIR="$CDIR/.."
localip=$(ip -o -4 addr | grep "scope global"| awk '{print $4}'|awk -F "/" '{print $1}')
host=$(cat ${CDDIR}/.host.txt | awk -F "@" '{print $2}')
cmdname=$1
for i in ${host}
do
if [[ $i != "$localip" ]];then
ssh root@$i $cmdname $2 $3 $4
fi
done
EOGG
cat>remotecopy<<'EOGH'
#!/bin/bash
CDDIR="$(cd "$(dirname "$0")" ; pwd -P)"
NDIR="$CDIR/.."
localip=$(ip -o -4 addr | grep "scope global"| awk '{print $4}'|awk -F "/" '{print $1}')
host=$(cat ${CDDIR}/.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
EOGH
chmod 777 ipscan
chmod 777 autossh
chmod 777 remotebash
chmod 777 remotecopy