python3 - 使用 sudo 的子进程 >> 追加到 /etc/hosts
python3 - subprocess with sudo to >> append to /etc/hosts
我一直在努力解决“How do I use sudo to redirect output to a location I don't have permission to write to?" and "append line to /etc/hosts file with shell script”的解决方案,但没有成功。
我要"append 10.10.10.10 puppetmaster"结束/etc/hosts。 (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 文件静止不动。
有人可以指出我做错了什么吗?
您遇到的问题在sudo
.
的范围内
您正在尝试使用参数 sh
和 -c" "10.10.10.10 puppetmaster"
调用 sudo
的代码。然而,>>
运算符的重定向是由周围的 shell 完成的,当然需要其权限。
为了达到你想要的效果,尝试使用 sudo 启动 shell 然后给出命令:
sudo bash -c 'sh -c" "10.10.10.10 puppetmaster" >> /etc/hosts"'
这会成功,因为您从 sudo
开始的 bash
具有超级用户权限,因此当它尝试使用 >>
执行输出重定向时不会失败。
要在 Python 中执行此操作,请使用:
subprocess.call("""sudo bash -c 'sh -c" "10.10.10.10 puppetmaster" >> /etc/hosts"'""", shell=True)
但是,当然,如果您 运行 您的 Python 脚本已经具有超级用户权限(使用 sudo
启动它),则所有这些都不是必需的,原始代码将起作用(在 call
中没有额外的 sudo
):
subprocess.call('sh -c" "10.10.10.10 puppetmaster" >> /etc/hosts"', shell=True)
一旦你 运行 使用 sudo 的脚本,你就可以在 python 中很容易地做到这一点:
with open("/etc/hosts","a") as f:
f.write('10.10.10.10 puppetmaster\n')
以 a
开头将追加。
如果您不升级整个脚本的权限,我建议如下:
p = subprocess.Popen(['sudo', 'tee', '-a', '/etc/hosts'],
stdin=subprocess.PIPE, stdout=subprocess.DEVNULL)
p.stdin.write(b'10.10.10.10 puppetmaster\n')
p.stdin.close()
p.wait()
然后你可以将任意内容写入进程的标准输入(p.stdin
)。
只需使用dd
:
subprocess.Popen(['sudo', 'dd', 'if=/dev/stdin',
'of=/etc/hosts', 'conv=notrunc', 'oflag=append'],
stdin=subprocess.PIPE).communicate("10.10.10.10 puppetmaster\n")
我一直在努力解决“How do I use sudo to redirect output to a location I don't have permission to write to?" and "append line to /etc/hosts file with shell script”的解决方案,但没有成功。
我要"append 10.10.10.10 puppetmaster"结束/etc/hosts。 (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 文件静止不动。 有人可以指出我做错了什么吗?
您遇到的问题在sudo
.
您正在尝试使用参数 sh
和 -c" "10.10.10.10 puppetmaster"
调用 sudo
的代码。然而,>>
运算符的重定向是由周围的 shell 完成的,当然需要其权限。
为了达到你想要的效果,尝试使用 sudo 启动 shell 然后给出命令:
sudo bash -c 'sh -c" "10.10.10.10 puppetmaster" >> /etc/hosts"'
这会成功,因为您从 sudo
开始的 bash
具有超级用户权限,因此当它尝试使用 >>
执行输出重定向时不会失败。
要在 Python 中执行此操作,请使用:
subprocess.call("""sudo bash -c 'sh -c" "10.10.10.10 puppetmaster" >> /etc/hosts"'""", shell=True)
但是,当然,如果您 运行 您的 Python 脚本已经具有超级用户权限(使用 sudo
启动它),则所有这些都不是必需的,原始代码将起作用(在 call
中没有额外的 sudo
):
subprocess.call('sh -c" "10.10.10.10 puppetmaster" >> /etc/hosts"', shell=True)
一旦你 运行 使用 sudo 的脚本,你就可以在 python 中很容易地做到这一点:
with open("/etc/hosts","a") as f:
f.write('10.10.10.10 puppetmaster\n')
以 a
开头将追加。
如果您不升级整个脚本的权限,我建议如下:
p = subprocess.Popen(['sudo', 'tee', '-a', '/etc/hosts'],
stdin=subprocess.PIPE, stdout=subprocess.DEVNULL)
p.stdin.write(b'10.10.10.10 puppetmaster\n')
p.stdin.close()
p.wait()
然后你可以将任意内容写入进程的标准输入(p.stdin
)。
只需使用dd
:
subprocess.Popen(['sudo', 'dd', 'if=/dev/stdin',
'of=/etc/hosts', 'conv=notrunc', 'oflag=append'],
stdin=subprocess.PIPE).communicate("10.10.10.10 puppetmaster\n")