我正在尝试编写一个expect脚本来自动化telnet。 这是我迄今为止。
#!/usr/bin/expect # Test expect script to telnet. spawn telnet 10.62.136.252 expect \”fooBox login:\” send \”foo1r\” expect \”Password:\” send \”foo2r\” send \”echo HELLO WORLDr\” # end of expect script.
基本上,我想要做的是telnet到下面的IP地址,然后回应HELLO WORLD。 但是,似乎脚本尝试telnet后失败…我不知道它是否能够接受login名和密码input,但它不是呼应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>
我怎样才能从预期的脚本中的文件中读取数据?
为什么我的git没有自动更新Expect脚本?
期望中的 n和 r之间的区别?
使用期望脚本的多个文件自动化SCP
期望实用工具从jenkins执行时不工作
Bash和Expect:有没有办法从Expect缓冲区中忽略或删除ANSI控制序列?
与期望产卵的pipe输出
期待,互动,然后再次期待
自动化脚本的结尾,等待您按ENTER键
当expect脚本中有多个“spawn”语句时,只有最后一个spawn语句被完全执行
您正在发送echo命令,而不预期提示。 尝试:
# after sending the password expect -re \”> ?$\” send \”echo HELLO WORLDr\” expect eof
很难说,但是从粘贴的输出看起来像这样:
在发送下一个命令之前,您的脚本不会等待登录完成。
您的脚本正在退出并关闭该过程,然后才能看到任何输出。
生活中没有任何保证,但我会尝试这是第一步:
#!/usr/bin/expect -f spawn telnet 10.62.136.252 expect \”fooBox login:\” send \”foo1r\” expect \”Password:\” send \”foo2r\” # Wait for a prompt. Adjust as needed to match the expected prompt. expect \”justin>\” send \”echo HELLO WORLDr\” # Wait 5 seconds before exiting script and closing all processes. sleep 5
备择方案
如果您无法通过手动编程来使脚本正常工作,请尝试使用Expect附带的autoexpect脚本。 您可以手动执行您的命令,并且autoexpect会根据这些命令生成一个Expect typescript,然后您可以根据需要进行编辑。
这是一个很好的方法来找出期望实际上看到什么,特别是在难以确定问题的情况下。 这多年来为我节省了大量的调试时间,如果上面的解决方案对您不适合,绝对值得一试。
你见过这个StackOverflow问题吗?
他似乎用花括号工作。
这是一个简化的版本
#!/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 \”\’^]\’.\”
总结
以上是内存溢出为你收集整理的如何使用Expect自动化Telnet会话?全部内容,希望文章能够帮你解决如何使用Expect自动化Telnet会话?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
请登录后查看评论内容