期望脚本自动执行telnet登录 [英] expect script to automate telnet login

查看:220
本文介绍了期望脚本自动执行telnet登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have been trying to create an expect script to automatically login to my device through telnet

如果期望命令没有多种可能性,则脚本可以正常工作,然后登录到设备.

If there are no multiple possibilities for the expect command , the script works fine, logs in to the device.

#!/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

当我传递正确的参数时,上面的脚本可以正常工作并成功登录.但是,一旦我向期望命令中添加了其他分支(用于错误处理),该脚本就会卡在login:提示符下.一段时间后,它将打印

The script above works fine and logs in successfully when i pass the correct parameters. But once i add additional branches(for error handling) to the expect command , the script gets stuck at login: prompt.After some time it prints Script Error Any help?? Erroneous script below.

#!/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(控制台)连接有效.所以ssh不是一个选择.

PS: This script is to be further developed to wait for additional prompts to load different build images on the device. Only telnet(console) connection works. so ssh is not an option.

推荐答案

我的糟糕. 问题出在花括号上.它们应该与Expect命令在同一行.

My bad. The problem was with the curly braces. They are supposed to be at the same line as the expect command .

这篇关于期望脚本自动执行telnet登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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