期望-telnet连接 [英] expect - telnet connection

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

问题描述

我正在尝试创建一个简单的telnet连接脚本.我生成telnet进程.视版本而定,它可能会或可能不会要求输入密码.

I'm trying to create a simple telnet connection script. I spawn telnet process. Depending on the version, it may or may not ask for password.

之后,它要求输入用户名和密码并接受规则.成功登录后,它会提示您输入命令.

After that It asks for username and password and rulese acceptation. After successful login it prompts for command.

但是,我写的东西行不通.

However, thing I wrote does not work.

#/usr/bin/expect -f
set IP [lindex $argv 0]
set timeout 10
set send_slow {10 .5}
log_user 1

spawn telnet -l cli $IP

expect {
    timeout {
        puts "Network Connection Problem"
        close
    }
    "Password:" {
        send -s -- "cli\r"
        exp_continue
    }
    "Username:" {
        send -s -- "admin\r"
        expect "Password:"
        send -s -- "admin\r"
        exp_continue
    }
    "(Y/N)?" {
        send -s -- "Y\r"
        exp_continue
    }   
}
expect "# "
send -s -- "show version\r"

运行脚本后,我需要登录并同意.显示提示后,脚本不会执行show version命令.光标在几秒钟后闪烁,我看到以下信息:

After running script, I go through login and agreement. Once prompt is shown, script does not execute show version command. Cursor blinks after few seconds I see info:

期望:生成ID ID EXP6未打开在执行期望#""

expect: spawn id exp6 not open while executing "expect "# ""

有人可以纠正我的错误吗?我已经阅读了Expect手册,通过示例脚本,但是找不到任何解决方案.我敢肯定这很简单,但是我在这里苦苦挣扎.

Can someone please correct my mistakes? I've read expect manual, went through exemplary scripts but could not find any solution. I'm sure it's simple, yet I'm struggling here.

帮我上尉.

推荐答案

您在这里有一条声明

spawn telnet -l cli $IP

,用于将用户名指定为telnet会话的 cli .因此,永远不会到达以 admin 登录的代码.

that specifies the username as cli for the telnet session. So the code to login as admin will never be reached.

管理员的默认Shell提示符是

The default shell prompt for admin is

'# '

cli的默认shell提示是

The default shell prompt for cli is

'$ '

更改您的代码以处理查找任何一个shell提示.

change your code to handle looking for either shell prompt.

这篇关于期望-telnet连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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