期望脚本 su 到 root 并执行 sed

发布于 2024-11-07 11:12:05 字数 202 浏览 2 评论 0原文

我以前从未使用过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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抹茶夏天i‖ 2024-11-14 11:12:05

可以理解的是,以 root 身份执行脚本来处理命令是一个巨大的安全问题,通常是不允许的。

然而,Expect 能够运行伪终端,因此它可以像人类打字一样工作远离键盘。

这或多或少是您所要求的,但未经测试。

#!/bin/sh
# if run from shell, restart with wish \
exec wish "$0" "$@"

package require Expect

proc fix_a_host {host usrname password root_password} {
        # open session to host, wait for a username prompt
        spawn ssh $host
        expect "username:"

        # send username, wait for a password prompt
        send "$usrname\r"
        expect "password:"

        # send password, wait for shell prompt
        send "$password\r"
        expect "%"

        # become root, wait for prompt
        send "su\r"
        expect "#"

        # change TEXT to NEWTEXT in password file
        send "sed 's/TEXT/NEWTEXT'" /etc/passwd
        expect "#"

        # exit root, exit host connection
        send "exit\r"
        expect "%"

        send "exit\r"
        expect eof
}       

fix_a_host {"host1" "someuser" "user_password"  "root_password"}
fix_a_host {"host2" "someuser" "user_password"  "root_password"}

如果是我,我会将 sed 命令更改为破坏性较小的命令,例如 grep TEXT /etc/passwd ,直到确信它运行良好为止。对于显示您想要查看的输出的远程命令,请使用

set results $expect_out(buffer)
send "some_command\r"
expect "#"    # use the command prompt to indicate output is complete
puts "command output is got `$buffer`"

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.

#!/bin/sh
# if run from shell, restart with wish \
exec wish "$0" "$@"

package require Expect

proc fix_a_host {host usrname password root_password} {
        # open session to host, wait for a username prompt
        spawn ssh $host
        expect "username:"

        # send username, wait for a password prompt
        send "$usrname\r"
        expect "password:"

        # send password, wait for shell prompt
        send "$password\r"
        expect "%"

        # become root, wait for prompt
        send "su\r"
        expect "#"

        # change TEXT to NEWTEXT in password file
        send "sed 's/TEXT/NEWTEXT'" /etc/passwd
        expect "#"

        # exit root, exit host connection
        send "exit\r"
        expect "%"

        send "exit\r"
        expect eof
}       

fix_a_host {"host1" "someuser" "user_password"  "root_password"}
fix_a_host {"host2" "someuser" "user_password"  "root_password"}

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

set results $expect_out(buffer)
send "some_command\r"
expect "#"    # use the command prompt to indicate output is complete
puts "command output is got `$buffer`"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文