欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

Shell脚本实现自动修改IP地址

程序员文章站 2023-11-14 13:04:46
作为一名linux sa,日常运维中很多地方都会用到脚本,而服务器的ip一般采用静态ip或者mac绑定,当然后者比较操作起来相对繁琐,而前者我们可以设置主机名、ip信息、网...

作为一名linux sa,日常运维中很多地方都会用到脚本,而服务器的ip一般采用静态ip或者mac绑定,当然后者比较操作起来相对繁琐,而前者我们可以设置主机名、ip信息、网关等配置。修改成特定的主机名在维护和管理方面也比较方便。如下脚本用途为:修改ip和主机名等相关信息,可以根据实际需求修改,举一反三!

#!/bin/sh 
#auto change ip netmask gateway scripts 
#wugk 2012-12-17 
cat << eof 
++++++++自动修改ip和主机名等相关信息+++++++++ 
ethconf=/etc/sysconfig/network-scripts/ifcfg-eth0 
hosts=/etc/hosts 
network=/etc/sysconfig/network 
dir=/data/backup/`date +%y%m%d` 
netmask=255.255.255.0 
+++++++++-------------------------+++++++++++ 
eof 
#define path 定义变量,可以根据实际情况修改 
 
 ethconf=/etc/sysconfig/network-scripts/ifcfg-eth0 
 hosts=/etc/hosts 
 network=/etc/sysconfig/network 
 dir=/data/backup/`date +%y%m%d` 
 netmask=255.255.255.0 
 
echo "================================================" 
echo 
#定义change_ip函数 
function change_ip () 
{ 
#判断备份目录是否存在,中括号前后都有空格,!叹号在shell表示相反的意思# 
if 
 [ ! -d $dir ];then 
 
 mkdir -p $dir 
 
fi 
 
 echo "now change ip address ,doing backup interface eth0" 
 cp $ethconf $dir 
 
 grep "dhcp" $ethconf 
#如下$?用来判断上一次操作的状态,为0,表示上一次操作状态正确或者成功# 
if 
 
 [ $? -eq 0 ];then 
#read -p 交互输入变量ipaddr,注冒号后有空格,sed -i 修改配置文件# 
 read -p "please insert ip address:" ipaddr 
 sed -i 's/dhcp/static/g' $ethconf 
#awk -f. 意思是以.号为分隔域,打印前三列# 
 echo -e "ipaddr=$ipaddr\nnetmask=$netmask\ngateway=`echo $ipaddr|awk -f. '{print $1"."$2"."$3}'`.254" >>$ethconf 
 echo "this ip address change success !" 
 
else 
 
 echo -n "this $ethconf is static exist ,please ensure change yes or no": 
 read i 
 
fi 
 
if 
 [ "$i" == "y" -o "$i" == "yes" ];then 
 read -p "please insert ip address:" ipaddr 
 
 count=(`echo $ipaddr|awk -f. '{print $1,$2,$3,$4}'`) 
 #定义数组, ${#count[@]}代表获取变量值总个数# 
 a=${#count[@]} 
 #while条件语句判断,个数是否正确,不正确循环提示输入,也可以用[0-9]来判断ip# 
while 
 
 [ "$a" -ne "4" ] 
 
do 
 
 read -p "please re inster ip address,example 192.168.0.11 ip": ipaddr 
 count=(`echo $ipaddr|awk -f. '{print $1,$2,$3,$4}'`) 
 a=${#count[@]} 
 
done 
 #sed -e 可以连续修改多个参数# 
 sed -i -e 's/^ipaddr/#ipaddr/g' -e 's/^netmask/#netmask/g' -e 's/^gateway/#gateway/g' $ethconf 
 #echo -e \n为连续追加内容,并自动换行# 
 echo -e "ipaddr=$ipaddr\nnetmask=$netmask\ngateway=`echo $ipaddr|awk -f. '{print $1"."$2"."$3}'`.254" >>$ethconf 
 echo "this ip address change success !" 
else 
 echo "this $ethconf static exist,please exit" 
 exit $? 
 
fi 
 
} 
 
#定义hosts函数 
############function hosts############## 
function change_hosts () 
{ 
 
if 
 
 [ ! -d $dir ];then 
 mkdir -p $dir 
 
fi 
 
 cp $hosts $dir 
 read -p "please insert ip address": ipaddr 
 
 host=`echo $ipaddr|sed 's/\./-/g'` 
 cat $hosts |grep 127.0.0.1 |grep "$host" 
  
if 
 [ $? -ne 0 ];then 
 sed -i "s/127.0.0.1/127.0.0.1 $host/g" $hosts 
 echo "this hosts change success " 
 
else 
 echo "this $host is exist .........." 
 
fi 
 
}
 
###########fuction network############### 
#定义network函数 
function change_network () 
{ 
 if 
 
 [ ! -d $dir ];then 
 mkdir -p $dir 
 
 fi 
 cp $network $dir 
 read -p "please insert ip address": ipaddr 
 
 host=`echo $ipaddr|sed 's/\./-/g'` 
 grep "$host" $network 
 
 if 
 [ $? -ne 0 ];then 
 sed -i "s/^hostname/#hostname/g" $network 
 echo "network=$host" >>$network 
 
else 
 echo "this $host is exist .........." 
 
 fi 
 
} 
 
#ps3一般为菜单提示信息# 
 ps3="please select ip or hosts menu": 
#select为菜单选择命令,格式为select $var in ..command.. do .... done 
 select i in "change_ip" "change_hosts" "change_network" 
 
do 
#case 方式,一般用于多种条件下的判断 
case $i in 
  change_ip ) 
  change_ip 
;; 
  change_hosts ) 
  change_hosts 
;; 
  change_network ) 
  change_network 
;; 
  *) 
  echo 
  echo "please insert $0: change_ip(1)|change_hosts(2)|change_network(3)" 
  echo 
;; 
esac 
 
done