shell 根据用户的要求修改ip获取方式 并修改ip以及掩码

   1 #!/bin/bash

  2 read_ip () {
  3 read -p "please input your IP: " IP
  4 read -p "please input your NETMASK: " MASK
  5 read -p "please input your gateway: " GATE
  6 read -p "please input your dns address: " DNS
  7 }
  8 get_ip_way () {
  9 read -p "Please input a Network Card that you want to setup:" CARD
 10 echo "The way your system get ip is: `grep '^BOOTPROTO*' /etc/sysconfig/network-scripts/ifcfg-$CARD | cut -d'=' -f2`"   
 11 }
 12 
 13 set_ip_way () {
 14 sed -i 's/^BOOTPROTO=dhcp/BOOTPROTO=static/' /etc/sysconfig/network-scripts/ifcfg-$CARD
 15 if [ $? == 0 ];then
 16  echo "IPADDR=$IP" >> /etc/sysconfig/network-scripts/ifcfg-$CARD
 17  echo "NETMASK=$MASK" >> /etc/sysconfig/network-scripts/ifcfg-$CARD
 18  echo "GATEWAY=$GATE" >> /etc/sysconfig/network-scripts/ifcfg-$CARD
 19 else
 20 sed -i 's/^IPADD.*/IPADDR='$IP'/' /etc/sysconfig/network-scripts/ifcfg-$CARD
 21 sed -i 's/^NETMASK.*/NETMASK='$MASK'/' /etc/sysconfig/network-scripts/ifcfg-$CARD
 22 sed -i 's/^GATEWA.*/GATEWAY='$GATE'/' /etc/sysconfig/network-scripts/ifcfg-$CARD
 23 fi
 24 sed -i 's/^name.*/nameserver '$DNS'/' /etc/resolv.conf
 25 echo "Setting..."
 26 service network restart &> /dev/null
 27 echo "setup is successful."
 28 }
 29 
 30 get_ip_way
 31 read_ip
 32 set_ip_way
 

你可能感兴趣的:(linux,职场,休闲)