模拟命令"输入"来自用户的按钮

时间:2015-09-28 18:41:44

标签: linux bash shell tcl expect

我在tcl中寻找一种方法,希望模拟按下"输入"脚本(例如在脚本停止的某些输出之后,只有在我手动按下&#34之后;输入"它进一步)它等待"输入"在继续输出剩余脚本之前从用户按下的键。

以下是我遇到此问题的代码:

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:"的这一行之后,我需要手动按下#​​34;输入"按钮然后脚本继续下一次尝试。

如何模拟此输入。有没有模拟它的命令? 我是tcl期待的新手。谢谢你的时间。

1 个答案:

答案 0 :(得分:0)

尝试更改

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
}