期望脚本 su 到 root 并执行 sed
我以前从未使用过expect,但需要一个脚本来对大量主机执行以下操作
- ssh 到机器
- su 到 root 并输入 root 密码
- sed /etc/passwd 中的一个文件以将某些文本替换为其他文本,对于这个例子,我们假设原始文本是 TEXT,而替换它的文本是 NEWTEXT
有谁可以帮忙吗?
I've never used expect before but need a script to do the following for a large list of hosts
- ssh into a machine
- su to root and enter the root password
- sed a file in /etc/passwd to replace some text with some other text, for this example lets just say the original text is TEXT and the text to replace it with is NEWTEXT
Can anyone help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可以理解的是,以 root 身份执行脚本来处理命令是一个巨大的安全问题,通常是不允许的。
然而,Expect 能够运行伪终端,因此它可以像人类打字一样工作远离键盘。
这或多或少是您所要求的,但未经测试。
如果是我,我会将 sed 命令更改为破坏性较小的命令,例如 grep TEXT /etc/passwd ,直到确信它运行良好为止。对于显示您想要查看的输出的远程命令,请使用
Understandably, executing a script to process commands as root is a huge security issue, and is generally not allowed.
However, Expect has the ability to run a pseudo terminal, so it can act just like a human typing away at a keyboard.
This is more or less what you asked, but is untested.
If it were me, I'd change the sed command to something far less destructive, like
grep TEXT /etc/passwd
until confident it works well. For a remote command which display output you want to see, use