鹿先生丶

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

 #!/bin/bash

#本地主机位地址
local_host_ip="::X"

#远端主机位地址
remote_host_ip="::X"
#echo ${remote}

#获取本地的ipv6地址集,一串字符串
local_ip_str=$(ip addr  | grep 'inet6' |grep '240'| grep '/64' | grep 'noprefixroute' | awk '{print $2}' | awk '{split($1,arr,"/"); print arr[1]}')

#将地址集转换为数组
local_ipv6s=(${local_ip_str})

#计数
i=0

#for 循环      ${local_ipv6s[@]  表示数组的所有内容
for ipv6 in ${local_ipv6s[@]}  
do

   #检测ipv6地址是否可以上网
   ping -6 -c 1  -I ${ipv6}  mirrors6.tuna.tsinghua.edu.cn | grep '1 received' & > /dev/null

  #检测刚才的命名是否正常执行,成功执行则表示可以通外网
   if [ $? -eq 0 ]; then

       #获取ipv6地址,并推出循环
        local_ip=${ipv6}

   break
   else

  #不成功则加1
        i=${i}+1
   fi
done

#分割获取ipv6地址前缀
prefix_ip=(${ipv6//${local_host_ip}/})

#合并获得远端的ipv6地址前缀
remote_ip=${prefix_ip[0]}${remote_host_ip}

#提前拷贝一份主机原本的hosts文件,

#写入host
cat /root/host > /root/hosts

#z增加一条hosts条目
echo  "${remote_ip}  esxi.notetophome.fun" >> /root/hosts

#将做好的hosts文件 拷贝至/etc/hosts
/bin/cp /root/hosts  /etc/hosts

 

 

crontab -e 定时任务

*/10  *  *  *  *  root   sh  shell文件

posted on 2022-10-16 18:59  鹿先生丶  阅读(232)  评论(0编辑  收藏  举报