首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用sed更改dhcpcd.conf文件中的ip地址

使用sed更改dhcpcd.conf文件中的ip地址
EN

Stack Overflow用户
提问于 2017-12-06 02:24:00
回答 0查看 1.2K关注 0票数 1

在树莓派上,我正在尝试编写一个简单的脚本,它将允许我更改dhcpcd.conf文件中的静态ip设置。除了dns服务器之外,下面的脚本可以正常工作。对于该行,sed语句似乎不起作用,因为它包含由空格分隔的两个ip地址。脚本如下:

代码语言:javascript
复制
#!/bin/bash
currip=$(cat /etc/dhcpcd.conf | grep -e '^static ip_address=' | cut -d= -
f2)
currgw=$(cat /etc/dhcpcd.conf | grep -e '^static routers=' | cut -d= -f2)
currdns=$(cat /etc/dhcpcd.conf | grep -e '^static domain_name_servers=' | 
cut -d= -f2)
echo "current IP is $currip"
echo "current GW is $currgw"
echo "current DNS servers are $currdns"
echo "Enter new static ip in form of x.x.x.x/x: "
read newip
echo "Enter new GW in form of x.x.x.x: "
read newgw
echo "Enter new DNS servers in form of x.x.x.x x.x.x.x: "
read newdns
echo "currip is $currip"
echo "new ip will be $newip"
echo "new dns will be $newdns"
sed -i -e "s@$currip\b@$newip@g" /etc/dhcpcd.conf
sed -i -e "s@$currgw\b@$newgw@g" /etc/dhcpcd.conf
sed -i -e "s@$currdns\b@$newdns@g" /etc/dhcpcd.conf
chip=$(cat /etc/dhcpcd.conf | grep -e '^static ip_address=' | cut -d= -
f2)
chgw=$(cat /etc/dhcpcd.conf | grep -e '^static routers=' | cut -d= -f2)
chdns=$(cat /etc/dhcpcd.conf | grep -e '^static domain_name_servers=' | 
cut -d= -f2)
echo "The ip has been changed to $chip"
echo "The GW has been changed to $chgw"
echo "The DNS server have been changed to $chdns"

dhcpcd.conf文件中的行如下所示:

静态ip地址=192.168.126.7/24

静态routers=192.168.126.1

静态domain_name_servers=192.168.126.1 66.243.243.101

我需要如何为domain_name_servers调整我的sed语句?

EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47660306

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档