Shell code for ip address replacement in ubuntu and centos

Source: Internet
Author: User

Code 1
Ubuntu and centos

Copy codeThe Code is as follows: ubuntu:
#! /Bin/bash
Echo "ipaddr :"
Read line
Echo $ line
Sed "s/address. * $/address =$ {line}/g"/etc/network/interfaces
Centos (not tested ):
#! /Bin/bash
Echo "ipaddr :"
Read line
Echo $ line
Sed "s/IPADDR. * $/IPADDR =$ {line}/g"/etc/sysconfig/net-scipts/ifcfg-eth0

Code 2
Modify the shell code of the IP address in ubuntu. It is self-written. please correct me.Copy codeThe Code is as follows :#! /Bin/sh

Echo "Use gateway at 192.168.0.1 (Y) or 192.168.1.1 (N)
Or detail set (O) \ nPlease choose Y/N/O"
Read OP
If ["$ OP" = Y] | ["$ OP" = y]; then
GATEWAY = 192.168.0.1
IP = 192.168.0.215
Elif ["$ OP" = N] | ["$ OP" = n]; then
GATEWAY = 192.168.1.1
IP = 192.168.1.215
Else
Echo "Please input the ip :"
Read IP
Echo "Please input the gateway"
Read GATEWAY
Fi
Echo "auto lo
Iface lo inet static

Auto eth0
Iface eth0 inet static
Netmask 255.255.255.0
Address $ IP
Gateway $ GATEWAY ">/tmp/interfaces
Clear
Echo "Init File interfaces Successfully! "
Echo "*************************************"
Cat/tmp/interfaces
Echo "*************************************"
Echo "Change the file in/etc/networks/interfaces? (Y/N )"
Read OP
If ["$ OP" = Y] | ["$ OP" = y]; then
Echo "Set OK! "
Cp/tmp/interfaces/etc/network/interfaces
/Etc/init. d/networking restart
Else
Echo "Give up"
Fi
Rm/tmp/interfaces
Echo "Whether change the DNS (/etc/resolv. conf) (Y/N )"
Read OP
If ["$ OP" = Y] | ["$ OP" = y]; then
Echo "Input the DNS server IP :"
Read IP
Echo "Now change the dns ip"
Mv/etc/resolv. conf/etc/resolv. conf. bak
Echo "nameserver $ IP">/etc/resolv. conf
Echo "Done! "
Fi

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.