javascript和JQuery焦点图和代码特效大全
当前最流行的开源CMS网站系统大全
当前位置:主页 > 操作系统 > Linux/Unix >

shell脚本更新和添加host

来源:IT技术网编辑:一页书发布于:2013-05-30人围观脚本shellhost

脚本可以根据传入的参数进行对 /etc/hosts 更改或者添加host。例如执行 sudo ./changeHost.sh blog.duplicatedcode.com 192.168.1.222。域名是固定的,外网ip不断变化

 

# !/bin/sh
###################
#@in_url 输入的url
#@in_ip 输入的ip
##########################
in_url=${1}
in_ip=${2}

#更改host
updateHost()
{
# read 
  inner_host=`cat /etc/hosts | grep ${in_url} | awk '{print $1}'`
  if [ ${inner_host} = ${in_ip} ];then
     echo "${inner_host}  ${in_url} ok"
  else
    #替换
     sed -i "s#${inner_host}#${in_ip}#g" /etc/hosts
     if [ $? = 0 ];then
       echo "change ${inner_host} to ${in_ip} ok"
       else 
         inner_ip_map="${in_ip} ${in_url}"
         echo ${inner_ip_map} >> /etc/hosts
         if [ $? = 0 ]; then
           echo "${inner_ip_map} to hosts success host is `cat /etc/hosts`"
         fi
     fi
  fi
}

main()
{
   updateHost
}
main

脚本,shell,host,相关的文章
有时间的话来看看IT界的突发事件