【发布时间】:2015-06-08 21:32:36
【问题描述】:
我一直在努力解决“How do I use sudo to redirect output to a location I don't have permission to write to?”和“append line to /etc/hosts file with shell script”的解决方案,但没有成功。
我想在 /etc/hosts 的末尾“追加 10.10.10.10 puppetmaster”。 (Oracle/Red-Hat linux)。
一直在尝试以下变化:
subprocess.call("sudo -s", shell=True)
subprocess.call('sudo sh -c" "10.10.10.10 puppetmaster" >> /etc/hosts"', shell=True)
subprocess.call(" sed -i '10.10.10.10 puppetmaster' /etc/hosts", shell=True)
但是 /etc/hosts 文件静止不动。 有人可以指出我做错了什么吗?
【问题讨论】:
-
为什么不直接使用 python 追加到文件中?
-
坦率地说,我是 python 新手(来自 java 背景)。有一种方法可以在不调用 bash shell 的情况下使用 python 追加?我会查一下这个。谢谢。
-
是的,就是用sudo运行脚本,我加了两行代码
标签: python subprocess