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)
I have written this script to copy my current HOSTS file to all my other servers.
However, I would like to do this from a regular user, not ROOT.
Since over-writing /etc/hosts takes root privelages, I would like to do SUDO. How can I put sudo inside that script?
This won't work, because it is permission denied to change /etc/hosts file.
cmd = 'sudo scp /etc/hosts regular_user@%s:/etc/hosts' % s
Best Answer
cat /etc/hosts | ssh otherhost "sudo sh -c 'cat >/etc/hosts'"
will do the trick.