Linux 如何使用 Expect 自动执行 telnet 会话?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11250564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to automate telnet session using Expect?
提问by Justin
I'm trying to write an expect script to automate telnet. This is what I have so far.
我正在尝试编写一个期望脚本来自动化 telnet。这是我到目前为止。
#!/usr/bin/expect
# Test expect script to telnet.
spawn telnet 10.62.136.252
expect "foobox login:"
send "foo1\r"
expect "Password:"
send "foo2\r"
send "echo HELLO WORLD\r"
# end of expect script.
Basically, what I want to do is telnet to the following IP address and then echo HELLO WORLD. However, it seems that the script fails after attempting to telnet...I'm not sure if it's able to accept login and password input, but it is not echoing HELLO WORLD. Instead, I just get this output:
基本上,我想做的是 telnet 到以下 IP 地址,然后回显 HELLO WORLD。但是,似乎脚本在尝试 telnet 后失败了......我不确定它是否能够接受登录名和密码输入,但它没有回应 HELLO WORLD。相反,我只是得到这个输出:
cheungj@sfgpws30:~/justin> ./hpuxrama
spawn telnet 10.62.136.252
Trying 10.62.136.252...
Connected to 10.62.136.252.
Escape character is '^]'.
Welcome to openSUSE 11.1 - Kernel 2.6.27.7-9-pae (7).
foobox login: foo1
Password: foo2~/justin>
回答by rkyser
Have you seen this StackOverflow Question?
你见过这个 StackOverflow 问题吗?
He seems to have got things working by using curly braces.
他似乎通过使用花括号来解决问题。
回答by glenn Hymanman
You're sending the echo
command without first expecting the prompt. Try:
您发送echo
命令时没有先期待提示。尝试:
# after sending the password
expect -re "> ?$"
send "echo HELLO WORLD\r"
expect eof
回答by Todd A. Jacobs
It's hard to tell, but from the output you're pasting it looks like:
很难说,但从您粘贴的输出来看,它看起来像:
- Your script isn't waiting for login to complete before sending the next command.
- Your script is exiting and closing the process before you can see any output.
- 在发送下一个命令之前,您的脚本不会等待登录完成。
- 在您看到任何输出之前,您的脚本正在退出并关闭进程。
There are no guarantees in life, but I'd try this as a first step:
生活中没有任何保证,但我会尝试将其作为第一步:
#!/usr/bin/expect -f
spawn telnet 10.62.136.252
expect "foobox login:"
send "foo1\r"
expect "Password:"
send "foo2\r"
# Wait for a prompt. Adjust as needed to match the expected prompt.
expect "justin>"
send "echo HELLO WORLD\r"
# Wait 5 seconds before exiting script and closing all processes.
sleep 5
Alternatives
备择方案
If you can't get your script to work by manually programming it, try the autoexpect script that comes with Expect. You can perform your commands manually, and autoexpect will generate an Expect typescript based on those commands, which you can then edit as needed.
如果您无法通过手动编程来使脚本工作,请尝试使用 Expect 附带的 autoexpect 脚本。您可以手动执行命令,autoexpect 将根据这些命令生成一个 Expect 打字稿,然后您可以根据需要对其进行编辑。
It's a good way to find out what Expect actually sees, especially in cases where the problem is hard to pin down. It's saves me a lot of debugging time over the years, and is definitely worth a try if the solution above doesn't work for you.
这是找出 Expect 实际看到的内容的好方法,尤其是在问题难以确定的情况下。多年来,它为我节省了大量调试时间,如果上述解决方案对您不起作用,绝对值得一试。
回答by grepit
Here is a simplified version
这是一个简化版
#!/usr/bin/expect
# just do a chmod 755 one the script
# ./YOUR_SCRIPT_NAME.sh $YOUHOST $PORT
# if you get "Escape character is '^]'" as the output it means got connected otherwise it has failed
set ip [lindex $argv 0]
set port [lindex $argv 1]
set timeout 5
spawn telnet $ip $port
expect "'^]'."