gpt4 book ai didi

linux - 无法使用 Bash 脚本连接到 POP3 服务器

转载 作者:可可西里 更新时间:2023-11-01 02:51:55 26 4
gpt4 key购买 nike

我在 shell 脚本中遇到了一个奇怪的问题。我正在尝试编写一个简单的脚本来连接到 POP3 服务器并计算邮箱中的邮件数量。服务器上的 Bash 版本是:3.2.25(1)-Linux 机器上的发行版。

当我手动输入命令来执行此操作时,它会起作用。

$ telnet <pop3 server ip> 110
Trying <pop3 server ip>...
Connected to <pop3 server ip>.
Escape character is '^]'.
+OK The Microsoft Exchange POP3 service is ready.
USER sadmin
+OK
PASS sadmin
+OK User successfully logged on.
STAT
+OK 1 10320
quit
+OK Microsoft Exchange Server 2010 POP3 server signing off.
Connection closed by foreign host.

现在要获取消息计数,我编写了以下脚本。

#!/bin/bash
exec 3<> /dev/tcp/<pop3 server ip>/pop3
read ok line <&3
echo $ok $line
echo USER sadmin >&3
read ok line <&3
echo aft userid entered:$ok $line
echo PASS sadmin1 >&3
echo STAT >&3
read num num1 <&3
echo $num $num1
echo quit >&3
exit

当我运行此脚本时,输出如下所示。

$ ./tcp.sh
+OK The Microsoft Exchange POP3 service is ready.
aft userid entered:-ERR Connection is closed. 12
$

在我将密码发送到 fd 3 后,它需要很长时间才能读取它,然后返回消息“-ERR Connection is closed”。

你能告诉我我在这里缺少什么吗?

最佳答案

因为是 Microsoft,您可能需要 CRLF 而不是简单的 UNIX 换行符,我无法对其进行测试,但我会尝试类似的方法:

例如crlf="$(echo xy | tr xy '\r\n')"

#!/bin/bash
crlf="$(echo xy | tr xy '\r\n')"
exec 3<> /dev/tcp/<pop3 server ip>/pop3
read ok line <&3
echo $ok $line
echo -en "USER sadmin$crlf" >&3
read ok line <&3
echo aft userid entered:$ok $line
echo -en "PASS sadmin1$crlf" >&3
echo -en "STAT$crlf" >&3
read num num1 <&3
echo $num $num1
echo -en "quit$crlf" >&3
exit

关于linux - 无法使用 Bash 脚本连接到 POP3 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26811118/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com