telnet - 期望脚本可以自动 telnet 登录

标签 telnet expect

我一直在尝试创建一个 expect 脚本来通过 telnet 自动登录到我的设备

如果expect 命令没有多种可能性,则脚本可以正常工作,登录设备。

#!/usr/bin/expect
set timeout 20
set ip [lindex $argv 0]
set port [lindex $argv 1]
set user [lindex $argv 2]
set password [lindex $argv 3]

spawn telnet $ip $port
expect "'^]'." sleep .1;
send "\r";
sleep .1;
expect   "login:"
send "$user\r"
expect "Password:"
send "$password\r";
interact

当我传递正确的参数时,上面的脚本工作正常并成功登录。但是一旦我向expect命令添加了额外的分支(用于错误处理),脚本就会卡在login:提示。一段时间后,它会打印 Script Error
有什么帮助吗??下面的错误脚本。
#!/usr/bin/expect
set timeout 20
set ip [lindex $argv 0]
set port [lindex $argv 1]
set user [lindex $argv 2]
set password [lindex $argv 3]

spawn telnet $ip $port
expect "'^]'."
sleep .1;
send "\r";
expect
{
  "login:"
  {
        send "$user\r"
        expect "Password:"
        send "$password\r";
        interact

  }

  "host: Connection refused"
  {
    send_user "ERROR:EXITING!"
    exit
  }

}

PS:此脚本将进一步开发以等待其他提示以在设备上加载不同的构建镜像。只有 telnet(console) 连接有效。所以 ssh 不是一种选择。

最佳答案

我的错。
问题出在花括号上。它们应该与expect 命令在同一行。

关于telnet - 期望脚本可以自动 telnet 登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7789710/

相关文章:

python - 通过 telnet 和 rdb 的 pdb 交互模式

python - 如何通过 telnet 运行 Python 程序?

linux - 如何在 perl 脚本中的 telnet 命令提示符下运行命令

Bash 期望 Telnet : handle premature connection close by server

c - 从源构建期望(静态链接)

linux - 当用户按任意键时telnet发送哪些数据

Linux 期望在使用交互后不将控制权返回给 bash 脚本

regex - 如何在 TCL/EXPECT 中使用 $expect_out 分配变量?

c++ - 如何在 Linux 上从 C++ 中使用 libexpect 创建的 FILE* 读取标准输出?

python - 如何在 python 中通过 ssh 运行 telnet