【问题标题】:Command for simulating "enter" button from the user用于模拟用户“输入”按钮的命令
【发布时间】:2015-09-29 03:06:23
【问题描述】:

我正在寻找一种在 tcl 中期望模拟按下“输入”脚本的方法(例如,在某些输出之后脚本停止,并且只有在我手动按下“输入”后它才会更进一步)它等待“输入” " 在继续输出剩余脚本之前从用户那里按下的键。

这是我遇到此问题的代码:

set timeout 20
set f [open "password.txt" r]
set password [read $f]
close $f

foreach i $password {

puts "trying this as a pass : $i"
spawn ssh user@exemple.net -p 724
expect "user@exemple.net's password:"
send $i
interact
}

此代码从password.txt 中获取它包含的所有单词,并尝试将它们作为user@exemple.net 的密码; 该代码有效,但在上面的代码expect "user@example.net's password:" 的这一行之后,我需要手动按下“输入”按钮,然后脚本将继续下一次尝试。

我怎样才能模拟这个输入。有没有模拟它的命令? 我是 tcl 的新手。感谢您的宝贵时间。

【问题讨论】:

  • stackoverflow 的想法是在您堆叠编码但不知道如何解决某些问题时提供帮助。所以,给我们一些你想模拟输入以完全理解你的问题的代码示例。
  • @r00t-err0r 我已经更新了描述。谢谢。

标签: linux bash shell tcl expect


【解决方案1】:

尝试改变

send $i

send "$i\n"

以这段代码为例:

#!/usr/bin/expect


# Set the time allowed to wait for a given response. SEt to 30 seconds because it can take a while
# for machines to respond to an ssh request.
set timeout 30000


proc connectToServer { user host password port } {

    set address $user
    append address "@"
    append address $host

    puts "Connecting to server: $host"

    spawn ssh $address -p $port
    expect {
        {*password: } {
            send "$password\n" 
            interact
        }
        {The authenticity of host*} {
            send "yes\n"
            expect {
                {*password: } {
                    send "$password\n"
                    interact
                }
            }
        }
        "*No route to host" { puts "No route to host" }
        eof {puts "Woops there was an eof"}
        timeout {puts "Timed out"}
    }
}


if {$argc < 3} {
    puts "Error - you need to provide at least 3 arguments"
    puts "* user"
    puts "* host"
    puts "* password"
    puts "* port - optional"
} else {
    set user [lindex $argv 0];
    set host [lindex $argv 1];
    set password [lindex $argv 2];

    # setting port argument is optional
    if {$argc > 3} {
        set port [lindex $argv 3];
    } else {
        set port 22
    }

    connectToServer $user $host $password $port
}

【讨论】:

  • 我正在尝试运行您的脚本,但我无法理解您的代码中是否应该编写用户主机密码和端口。您的代码看起来很棒,但我该如何运行它呢?谢谢。
  • 它可能需要改为send $i\r。有些终端关心,有些不关心。
  • 通常\r,回车,对应于回车键。
猜你喜欢
  • 2023-03-20
  • 1970-01-01
  • 1970-01-01
  • 2016-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多