简体   繁体   English

自动化ssh登录:tcl expect interactive命令表现得很奇怪

[英]Automating ssh login: tcl expect interact command behaves weird

I wanted to automate ssh logins. 我想自动化ssh登录。 After some research, it seemed like tcl/expect was the route to go. 经过一些研究,似乎tcl / expect是走的路。

However, my issue is that when interact takes over my terminal, stuff doesn't work as expected (pun not intended). 但是,我的问题是,当交互接管我的终端时,东西不能按预期工作(双关语无意)。

For example, if I resize the terminal, it does not "take". 例如,如果我调整终端的大小,它就不会“占用”。 Also, sometimes the interact is not responsive, and sometimes it just hangs for no reason. 此外,有时互动没有响应,有时它只是无缘无故地挂起。 I have included my code below. 我在下面提供了我的代码。 My question for the code is, am I missing something? 我对代码的问题是,我错过了什么吗?

Also, is there a better way to do this (with another scripting language?) I need the terminal to be very responsive, and no different than if I had manually typed ssh on the console. 另外,有没有更好的方法来实现这一点(使用另一种脚本语言?)我需要终端非常灵敏,并且与我在控制台上手动键入ssh没有什么不同。

proc Login {username server password} {
    set prompt "(%|>|\#|\\\$) $"

    spawn /usr/bin/ssh $username@$server
    expect { 
        -re "Are you sure you want to continue connecting (yes/no)?" {
            exp_send "yes\r"
            exp_continue 
            #continue to match statements within this expect {}
        }

        -nocase "password: " { 
            exp_send "$password\r" 
            interact
        }
    }
}

The issue you bring up about 'interact' not noticing that the window has changed size is because, by default, interact only watches the streams of characters. 您提出的关于“交互”的问题没有注意到窗口已经改变了大小是因为,默认情况下,交互仅监视字符流。 When you resize the window, a WINCH signal is raised. 调整窗口大小时,会引发WINCH信号。 By default, Expect has the, uh, default action which is to ignore it. 默认情况下,Expect具有默认操作,即忽略它。 You should use Expect's trap command to catch the SIGWINCH. 您应该使用Expect的trap命令来捕获SIGWINCH。 Then you can do what you want - for example, propagating the new size to the other side or taking some other local action. 然后,您可以执行您想要的操作 - 例如,将新大小传播到另一侧或采取其他一些本地操作。

You can find examples in Expect's sample scripts that do this. 您可以在Expect的示例脚本中找到执行此操作的示例。 And there is an explanation and example in the Expect book of how to handle SIGWINCH. Expect一书中有关于如何处理SIGWINCH的解释和示例。

As W. Craig Trader mentions in a comment, the "right" way to do this is to use ssh-keygen to generate a key for authentication. 正如W. Craig Trader在评论中提到的,“正确”的方法是使用ssh-keygen生成用于身份验证的密钥。

You can avoid the "continue" prompt by setting -o StrictHostKeyChecking=no . 您可以通过设置-o StrictHostKeyChecking=no来避免“继续”提示。

spawn ssh my-server.example.net
trap {
    set rows [stty rows]
    set cols [stty columns]
    stty rows $rows columns $cols < $spawn_out(slave,name)
} WINCH
expect {
    "assword: " {send "Let-Me-In-Please\r"}
    "Last login"
}
interact

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

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