简体   繁体   中英

SED in remote SUDO ssh script

I am trying to disable RHN check when running yum on 1000 servers. It is done by:

Editing this file /etc/yum/pluginconf.d/rhnplugin.conf

[main]
enabled = 0 

I wrote a script to do this remotely. We are using individual accounts and I need to execute this command using SUDO:

for HOST in $(cat serverlist ) ; do echo $HOST; ssh -o ConnectTimeout=5 -oStrictHostKeyChecking=no $HOST -t 'sudo cp /etc/yum/pluginconf.d/rhnplugin.conf /etc/yum/pluginconf.d/rhnplugin.$(date +%F) ; sudo sed -i -e "s/1/0/g" /etc/yum/pluginconf.d/rhnplugin.conf ' ; done

I know it is a long line but why does it not work?

All individual commands work on their own

sudo cp /etc/yum/pluginconf.d/rhnplugin.conf /etc/yum/pluginconf.d/rhnplugin.$(date +%F)

sudo sed -i -e "s/1/0/g" /etc/yum/pluginconf.d/rhnplugin.conf

have tried escaping the special chars:

sudo sed -i -e "s\/1\/0\/g" /etc/yum/pluginconf.d/rhnplugin.conf

But I get an error all the time:

sed: -e expression #1, char 1: unknown command: `?'

Thanks for your help.

The sudo(1) command expects a pseudo-teletype (pty) and fails if it does not see one. Rewrite your command line to use su(1) instead. Use your local sudo(1) configuration to limit access to this script so only the select few can execute the script.

I actually found the answer to this question, or rather workaround. See the snippet below, where I got to -as root- ssh as me (szymonri) to other host, then invoke sed command as root in order to edit /etc/hosts file. All thanks to base64 magic.

ME=`echo -e "$(hostname -I | awk '{print $1}')\toverlord"`
B64ENC=`echo "sed -i 's/.*overlord/$ME/g' /etc/hosts" | base64`
su - szymonri sh -c "ssh jetson bash -c \\\"echo $B64ENC \| base64 --decode \| sudo bash \\\""
  1. line: I"m obtaining m yown IP address as an /etc/hosts line
  2. line: I'm base64 encoding sed command with the first line in it.
  3. line: I'm invoking the SSH shenannigan, where I su as regular user, ssh to another box as the user, and use power of sudo to edit the file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM