regex - 使用 expect 或 shell 脚本的 Telnet SMTP

标签 regex linux shell expect ubuntu-12.04

想用 expect 脚本建立一个 Auth Smtp 连接...只是为了测试我想获取 ehlo 参数但是 expect 不能像这样工作

#!/usr/bin/expect
set timeout -1
set smtp [lindex $argv 0]
set port [lindex $argv 1]
spawn telnet $smtp $port
expect "[2]{2,}[0]{1,}"
send "ehlo\n"

我希望代码 220 来自邮件服务器继续发送 ehlo ... 就像

..../...:telnet smtp.mail.yahoo.de 25
Trying 77.238.184.85...
Connected to smtp2-de.mail.vip.ukl.yahoo.com.
Escape character is '^]'.
220 smtp116.mail.ukl.yahoo.com ESMTP
ehlo
250-smtp116.mail.ukl.yahoo.com
250-AUTH LOGIN PLAIN XYMCOOKIE
250-PIPELINING
250-SIZE 41697280
250 8BITMIME

错误说法:

spawn telnet smtp.mail.yahoo.de 25
invalid command name "2"
    while executing
"2"
    invoked from within
"expect "[2]{2,}[0]{1,}""
    (file "./login.exp" line 6)

如果我只是写 expect "220" 而不是 expect "[2]{2,}[0]{1,}" 它有效但会忽略 发送“ehlo\n”

最佳答案

按照上面的建议,我使用 exp_internal 1 来了解期望真正听到的内容...... 此外,我还可以推荐 autoexpect,它创建的 expect 脚本并不完美,但在改进了一些编码之后,它是一个真正的帮助,最终它起作用了。

#!/usr/bin/expect
#exp_internal 1
set timeout -1
set smtp [lindex $argv 0]
set port [lindex $argv 1]
spawn telnet $smtp $port
expect -re {[2]{2,}[0]{1,}}
sleep 3;
send -- "Ehlo\r"
expect -re {[2]{1,}[5]{1,}[0]{1,}}
send -- "quit\r"
expect eof

关于regex - 使用 expect 或 shell 脚本的 Telnet SMTP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12320592/

相关文章:

c# - 查找具有指定首字母的单词(正则表达式)

JavaScript - 检查字符串中的任何小写字母

ruby - 在 Linux 上使用 rvm、ruby 1.8.7 和 gcc 4.7 安装 bundler

linux - 收到特定主题电子邮件时触发 .sh 脚本

linux - 等同于 .cmd 中的 find -o (shell)

java - 具有 "{"的字符串的模式匹配

regex - 如何修剪线包括特定字符串与 sed

python-3.x - 我无法用 pip 安装 cupy

Java getenv() 返回 null

shell - 通过更改路径中的一个目录来更快地访问 Unix cd?