I am facing a strange problem in shell script. I am trying to write a simple script to connect to a POP3 server and count the number of messages in the mailbox. Bash version on Server is: 3.2.25(1)-release on Linux box.
When I manually type commands to do this, it works.
$ 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.
Now to get the count of messages, I wrote the script below.
#!/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
When I run this script, the output looks like this.
$ ./tcp.sh
+OK The Microsoft Exchange POP3 service is ready.
aft userid entered:-ERR Connection is closed. 12
$
After I sent the password to fd 3 it takes long time to read it and then it comes back with the message as "-ERR Connection is closed".
Can you please let me know what I am missing here?