0

What is the best approach for developing a program in python that can replace an old DNS Server address for a new one in Ubuntu 12.04?

UPDATE: Basically what I need is a way to update my resolv.conf file content so that I can replace the nameservers configured in there.

So for example I have in my resolv.conf:

 nameserver 10.0.0.1
 nameserver 10.0.0.2

And I need to somehow modify to have the values:

 nameserver 10.0.0.3
 nameserver 10.0.0.4

I need to make this using python(or any scripting language or even by command line), because I need to do this in a user-friendly window that must run in a kiosk-mode Xubuntu.

**NOTE:

So far I have tried finding an ubuntu command that can achieve this, but I haven't found one.

I have also tried modifying /etc/resolv.conf but Python has no ability to modify a file, so I need to delete the file and create the file with the new content, however I have no permission to do this (already tried chmod 777 and chattr -a but they didn't work)

4

1 回答 1

1

因为这种编辑任务sed通常是你最好的朋友。为了完成您的示例,以下内容将起作用:

sed -i '
  s/10\.0\.0\.1/10.0.0.3/
  s/10\.0\.0\.2/10.0.0.4/
' /etc/resolv.conf

这使用搜索和替换运算符s//. 该-i标志导致sed就地修改文件,而不是在标准输出上打印修改后的版本。

请注意,我在匹配表达式中转义了句点 ( ),因为在用于匹配语法的.正则表达式中,该字符是通配符。sed.

但是,如果您只是在两种不同的配置之间切换,只需替换文件可能是最简单的解决方案:

cp /etc/resolv.conf.config1 /etc/resolv.conf
于 2013-07-23T20:31:54.230 回答