bash 需要帮助修改 Shell 脚本以更改 CentOS 上的 IP 地址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24073929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 10:37:46  来源:igfitidea点击:

Need help to modify a Shell Script to change ip addresses on CentOS

bashshellnetworkingcentos

提问by Varun U

I have a shell script to change IP address (on a CentOS), instead of changing it manually. Here is the script:

我有一个 shell 脚本来更改 IP 地址(在 CentOS 上),而不是手动更改它。这是脚本:

#!/bin/bash
# changeip.sh

usage(){
    clear
    echo "Usage: 
# vi /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE="eth0"
BOOTPROTO=none
NM_CONTROLLED="yes"
ONBOOT=yes
TYPE="Ethernet"
IPADDR=192.168.1.2
GATEWAY=192.168.1.1
newip" echo "Example:
#!/bin/bash

# Shell script input parsing here

system-config-network-cmd -i <<EOF
DeviceList.Ethernet.eth0.type=Ethernet
DeviceList.Ethernet.eth0.BootProto=static
DeviceList.Ethernet.eth0.OnBoot=True
DeviceList.Ethernet.eth0.NMControlled=True
DeviceList.Ethernet.eth0.Netmask=192.168.1.255
DeviceList.Ethernet.eth0.IP=${new_ip_value}
DeviceList.Ethernet.eth0.Gateway=192.168.1.1
ProfileList.default.ActiveDevices.1=eth0
EOF

service network stop
service network start
127.0.0.1" exit 1 } new_ip_value= local_ip_value=$(ifconfig eth0|awk '/inet addr/ {split (,A,":"); print A[2]}') # call usage() function if filename not supplied [[ $# -eq 0 ]] && usage echo "/etc/hosts" echo "----------------------------------------------------------" sed -ie 's/'$local_ip_value'/'$new_ip_value'/g' /etc/hosts sed 's/'$local_ip_value'/'$new_ip_value'/g' /etc/hosts echo "" echo "/etc/sysconfig/network-scripts/ifcfg-eth0" echo "----------------------------------------------------------" sed -ie 's/'$local_ip_value'/'$new_ip_value'/' /etc/sysconfig/network-scripts/ifcfg-eth0 sed 's/'$local_ip_value'/'$new_ip_value'/' /etc/sysconfig/network-scripts/ifcfg-eth0 echo "The IP of $local_ip_value has successfully been changed to $new_ip_value." service network restart exit

I need to add further parameters like:

我需要添加更多参数,例如:

  1. BOOTPROTO
  2. ONBOOT
  3. GATEWAY
  4. DNS1
  1. 引导程序
  2. 开机
  3. 网关
  4. DNS1

and so on.

等等。

Could any body let me know how i should modify the script to add these details? My ifcfg-eth0 should ultimately like this:

任何机构都可以让我知道我应该如何修改脚本以添加这些细节吗?我的 ifcfg-eth0 最终应该是这样的:

##代码##

Any help is appreciated!

任何帮助表示赞赏!

Thanks!

谢谢!

回答by 6EQUJ5

A much better way to change IP addresses on CentOS is to use the system-network-config-cmdtool.

在 CentOS 上更改 IP 地址的更好方法是使用该system-network-config-cmd工具。

For example:

例如:

##代码##

If you run as system-config-network-cmd -eit dumps the existing configuration.

如果您在运行system-config-network-cmd -e时转储现有配置。