Automatic IP modification of Linux shell scripts

Source: Internet
Author: User

#!/bin/sh
#auto Change IP netmask Gateway scripts
#wugk 2012-12-17
Cat << EOF
++++++++ automatically modifies IP and host name information +++++++++
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 definition variable, can be modified according to the actual situation
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 ()
{
#判断备份目录是否存在, there are spaces before and after the brackets! The exclamation point in the shell means the opposite.
If
[!-D $DIR];then
Mkdir-p $DIR
Fi
echo "Now change IP address, Doing Backup Interface eth0"
CP $ETHCONF $DIR
grep "DHCP" $ETHCONF
#如下 $? Used to determine the state of the last operation, which is 0, indicating that the last operation state was correct or successful #
If
[$?-eq 0];then
#read-P Interactive input variable ipaddr, with a space after the colon, sed-i modify the configuration file #
Read-p "Please insert IP Address:" ipaddr
Sed-i ' s/dhcp/static/g ' $ETHCONF
#awk-F. The first three columns are printed by using the. Number as a delimited field
Echo-e "ipaddr= $IPADDR \nnetmask= $NETMASK \ngateway= ' echo $IPADDR |awk-f. ' {print $ '. $ "." $ "". 254 ">> $ETHCONF
echo "This IP address change success!"
Else
Echo-n "This $ETHCONF are 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 [@]} to get the total number of variable values #
a=${#count [@]}
#while条件语句判断, the number is correct, incorrect loop prompt input, you can also use [0-9] to Judge 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 can modify multiple parameters consecutively #
Sed-i-E ' s/^ipaddr/#IPADDR/g '-e ' s/^netmask/#NETMASK/g '-e ' s/^gateway/#GATEWAY/g ' $ETHCONF
#echo-e \ n for continuous append, and wrap line #
Echo-e "ipaddr= $IPADDR \nnetmask= $NETMASK \ngateway= ' echo $IPADDR |awk-f. ' {print $ '. $ "." $ "". 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为菜单选择命令 in the format of select $var in: command. Do .... Done
Select I in "Change_ip" "change_hosts" "Change_network"
Do
#case method, generally used for judging under a variety of conditions
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

Automatic IP modification of Linux shell scripts

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.