Linux Network scripts

来源:互联网 发布:java怎么调用二维码 编辑:程序博客网 时间:2024/06/06 16:59
 
  local dhcp_ip=`/sbin/ifconfig eth0 | grep 'inet addr' | awk -F: '{print $2}' | awk '{print $1}'`
  local dhcp_netmask=`ifconfig eth0 | grep 'inet addr' | awk -F: '{print $NF}'`
  local dhcp_dns=`grep nameserver /etc/resolv.conf | head -1 | awk '{print $2}'`
  local dhcp_hostname=`/bin/hostname`
  local dhcp_gw=`/sbin/ip route show |grep default | awk '{print $3}'`
 
 ##Configure DNS
      echo -n "search " >>/etc/resolv.conf
      echo "$static_hostname" |cut -d '.' -f 2->>/etc/resolv.conf
      echo "nameserver $static_dns">>/etc/resolv.conf    
 
 
  # overwite ifcfg-eth0 with static ip setting.
  cat > /etc/sysconfig/network-scripts/ifcfg-eth0 <<-EOF
DEVICE=eth0
ONBOOT=yes
TYPE=Ethernet
IPADDR=$static_ip
NETMASK=$static_netmask
PEERDNS=no
EOF

 

cat > /etc/sysconfig/network <<-EOF

NETWORKING=yes
HOSTNAME=$static_hostname
GATEWAY=$static_gw
EOF

 
  echo "Restarting networking..."
  echo
  service network restart
  hostname $static_hostname
 
}

#function: update /etc/hosts file
#parameter:
#return
function fix_hosts {
    local host_name=`hostname`
    local host_name_s=`echo $host_name|awk -F. '{print $1}'`
    local ips=`ifconfig -a | grep -v "127.0.0.1" | grep 'inet addr' | awk -F: '{print $2}' | awk '{print $1}'`
    for ip in $ips; do
        if [ "x$ip" != "x" ];then
            sed -i "/$ip/d" /etc/hosts
            echo "$ip $host_name $host_name_s" >> /etc/hosts
        fi
    done
}

#function: check if the IP address is valid
#parameter:
#return:  1   if the IP address is invalid
#note: in the if or while , the 1  equal to false
function check_ip {
  if ! ifconfig -a | grep "inet addr" |grep -q -v "127.0.0.1";
  then
     return 1
  fi
}

#function: check if the hostname is valid
#parameter:
#return:  1   if the IP hostname is invalid
#note: in the if or while , the 1  equal to false
function check_hostname {
  local host_name=`hostname`
  if [ "x$host_name" = "xlocalhost.localdomain" ]
  then
    return 1
  fi
 
  if ! hostname -i>/dev/null 2>&1
  then
    return 1
  fi
}


原创粉丝点击