1

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?

Rafa Viotti
  • 9,998
  • 4
  • 42
  • 62
abb
  • 352
  • 1
  • 10
  • 23

1 Answers1

1

Because its Microsoft you probably need a CRLF not a simple UNIX newline, I have no way to test it but I would try something like:

E.g. 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
  • Why this magic with `tr`? You can use `cr='\r'; echo -e "foo$cr"` or a bit clearer `crlf='\r\n'; echo -en "foo$crlf"` to get `foo`. – praetorian droid Nov 07 '14 at 23:49
  • It should work, but CR LF are notorious for having different interpretations and tr is reputable for a long time in producing the appropriate characters. (It still is not ^M like Windows) –  Nov 08 '14 at 00:09
  • You can read more here about the issues: http://stackoverflow.com/questions/1552749/difference-between-cr-lf-lf-and-cr-line-break-types –  Nov 08 '14 at 00:11
  • Never heard of 'echo' versions, that do not work properly with -en and '\r\n'. Or bash versions in this case, because here 'echo' is a bash builtin command. – praetorian droid Nov 09 '14 at 15:10
  • On my system echo is not a bash builtin and there are various behaviors of the echo command - did you read the link data above? Its not that trivial, the CRLF that Microsoft used early on is actually an EOL that was a single character. All of the double character \r\n or even single character \r that works in lieu of the single character EOL is something different than the single character EOL that Microsoft used. –  Nov 09 '14 at 15:47
  • Thanks A. Danishewski & Praetorian. I was able to make it work using your inputs. Its not working with both CR and LF in my bash. I have just used echo -e "USER SADMIN\r" >&3 and it worked. – abb Nov 10 '14 at 16:38
  • @abb, glad to hear you got it working - perhaps the software is modernized. –  Nov 10 '14 at 20:05
  • You can read about other methods if you have future problems here: http://en.wikipedia.org/wiki/Unix2dos –  Nov 10 '14 at 20:10