简体   繁体   English

远程 SUDO ssh 脚本中的 SED

[英]SED in remote SUDO ssh script

I am trying to disable RHN check when running yum on 1000 servers.我试图在 1000 台服务器上运行 yum 时禁用 RHN 检查。 It is done by:它是通过以下方式完成的:

Editing this file /etc/yum/pluginconf.d/rhnplugin.conf编辑这个文件/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:我们正在使用个人帐户,我需要使用 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. sudo(1) 命令需要一个伪电传打字机 (pty),如果没有看到,则失败。 Rewrite your command line to use su(1) instead.重写您的命令行以使用 su(1) 代替。 Use your local sudo(1) configuration to limit access to this script so only the select few can execute the script.使用您的本地 sudo(1) 配置来限制对该脚本的访问,以便只有少数人可以执行该脚本。

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.看到为了下面的代码片段,在那里我得到了-as根- ssh作为(szymonri)到其他主机,然后调用sed命令,作为根用户编辑/etc/hosts的文件。 All thanks to base64 magic.这一切都归功于base64魔法。

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行:我正在获取我自己的 IP 地址作为/etc/hosts
  2. line: I'm base64 encoding sed command with the first line in it.行:我是 base64 编码 sed 命令,其中第一行。
  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.行:我正在调用 SSH shenannigan,在那里我以普通用户身份 su,以用户身份 ssh 到另一个框,并使用 sudo 的功能来编辑文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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