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

ubuntu与centos中更换ip的shell代码

程序员文章站 2023-11-19 14:36:40
代码一ubuntu与centos复制代码 代码如下:ubuntu:#!/bin/bashecho "ipaddr:"read lineecho $linesed "s/ad...

代码一
ubuntu与centos

复制代码 代码如下:

ubuntu:
#!/bin/bash
echo "ipaddr:"
read line
echo $line
sed "s/address.*$/address=${line}/g" /etc/network/interfaces
centos(未测试):
#!/bin/bash
echo "ipaddr:"
read line
echo $line
sed "s/ipaddr.*$/ipaddr=${line}/g" /etc/sysconfig/network-scipts/ifcfg-eth0

代码二
ubuntu更改ip的shell代码,自己写的,比较拙,请指正
复制代码 代码如下:

#! /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