如何使用expect通过ssh连接到系统并更改主机系统的密码?

发布于 2024-10-09 18:50:20 字数 964 浏览 5 评论 0原文

我正在自动化以下过程:
通过 ssh 连接到名为“alpha”的系统,用户名“alpha”的密码为“alpha”。连接后,我想设置 root 密码(“kickass”)。我连接的系统默认没有 root 密码。我编写了这个期望脚本来完成这项工作,但它不能一致地工作。它工作一次,然后如果我更改密码再次测试,它会在发出“sudo passwd root”后等待“输入新的 UNIX 密码:”提示。有什么想法吗?

#!/usr/bin/expect -f
set arg1 [lindex $argv 0]
set force_conservative 1  ;# set to 1 to force conservative mode even if
              ;# script wasn't run conservatively originally
if {$force_conservative} {
    set send_slow {1 .1}
    proc send {ignore arg} {
        sleep .1
        exp_send -s -- $arg
    }
}

set timeout -1
spawn ssh alpha@$arg1
match_max 100000
expect -exact "password: "
send -- "alpha\r"
expect -exact "alpha@alpha:~\$ "
send -- "sudo passwd root\r"
expect -exact "password for alpha: "
send -- "alpha\r"
expect -exact "new UNIX password: "
send -- "kickass\r"
expect -exact "Retype new UNIX password: "
send -- "kickass\r"
expect -exact "alpha@alpha:~\$ "
send -- "exit\r"
expect eof

谢谢。

I am automating the process of:
Connect to a system named "alpha" via ssh with password "alpha" for username "alpha". Once connected I would like to set the root password (to "kickass"). The system I am connecting to doesn't have a root password by default. I wrote this expect script to do the job but it doesn't work consistently. It works once and then if I change the password to test again, it waits at the "Enter new UNIX password:" prompt after issuing "sudo passwd root". Any ideas?

#!/usr/bin/expect -f
set arg1 [lindex $argv 0]
set force_conservative 1  ;# set to 1 to force conservative mode even if
              ;# script wasn't run conservatively originally
if {$force_conservative} {
    set send_slow {1 .1}
    proc send {ignore arg} {
        sleep .1
        exp_send -s -- $arg
    }
}

set timeout -1
spawn ssh alpha@$arg1
match_max 100000
expect -exact "password: "
send -- "alpha\r"
expect -exact "alpha@alpha:~\$ "
send -- "sudo passwd root\r"
expect -exact "password for alpha: "
send -- "alpha\r"
expect -exact "new UNIX password: "
send -- "kickass\r"
expect -exact "Retype new UNIX password: "
send -- "kickass\r"
expect -exact "alpha@alpha:~\$ "
send -- "exit\r"
expect eof

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

孤云独去闲 2024-10-16 18:50:20

扩展 Andrei Sfrent 的评论:

Sudo 通常具有某种超时值,因此,如果您在超时期限内发出第二个 sudo 命令,则不会再次要求您输入密码。您可以将代码修改为如下所示:

send -- "sudo passwd root\r"
expect {
        -exact "password for alpha: "
            {
                send -- "alpha\r"
                exp_continue
            }

        -exact "new UNIX password: "
            {
                send -- "kickass\r"
            }
        }

Expanding on Andrei Sfrent's comment:

Sudo normally has some kind of time-out value, so that if you issue a second sudo command within the time-out period you will not be asked for your password again. You could modify the code to something like this:

send -- "sudo passwd root\r"
expect {
        -exact "password for alpha: "
            {
                send -- "alpha\r"
                exp_continue
            }

        -exact "new UNIX password: "
            {
                send -- "kickass\r"
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文