如何将 /etc/hosts 复制到我所有的机器上?

Ale*_*lex 2 linux unix python hosts scp

import os, sys, time

servers = ['dev','admin','db1']
for s in servers:
    cmd = 'scp /etc/hosts regular_user@%s:/etc/hosts' % s
    print cmd
    os.system(cmd)
Run Code Online (Sandbox Code Playgroud)

我编写了这个脚本来将我当前的 HOSTS 文件复制到我所有的其他服务器。但是,我想从普通用户而不是 ROOT 执行此操作。

由于覆盖 /etc/hosts 需要 root 特权,我想做SUDO。如何将 sudo 放入该脚本中?

这将不起作用,因为更改 /etc/hosts 文件的权限被拒绝。

cmd = 'sudo scp /etc/hosts regular_user@%s:/etc/hosts' % s
Run Code Online (Sandbox Code Playgroud)

Jim*_*ski 6

cat /etc/hosts | ssh otherhost "sudo sh -c 'cat >/etc/hosts'" 会做的伎俩。