如何使用通过期待ssh来连接到系统并更改主机系统的密码? [英] How do I use expect to connect via ssh to a system and change the password of the host system?

查看:197
本文介绍了如何使用通过期待ssh来连接到系统并更改主机系统的密码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是自动化的过程:结果
连接到一个名为阿尔法通过带有密码阿尔法的SSH用户名阿尔法的系统。一旦连接成功,我想设置root密码(为kickass)。我连接到该系统没有默认root密码。我写这个expect脚本来完成这项工作,但它不工作始终。如果我更改密码再次测试它的工作一次,然后,等待在输入新的UNIX口令:发布命令passwd的根后提示。任何想法?

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

感谢。

推荐答案

扩展在安德烈Sfrent的评论:

Expanding on Andrei Sfrent's comment:

须藤通常有某种形式的超时值,因此,如果您发出你将不再要求您输入密码的超时期限内的第二sudo命令。你可以修改code到这样的事情:

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"
            }
        }

这篇关于如何使用通过期待ssh来连接到系统并更改主机系统的密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆