LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Mail script (https://www.linuxquestions.org/questions/linux-newbie-8/mail-script-461517/)

Braynid 07-06-2006 11:12 AM

Mail script
 
Hello!
I am now working on a e-mail checking script, until now i have:

Code:

function mail ()
{
telnet mail.rdslink.ro 110

user $1

pass $2

list

grep '+OK POP3'
}

The purpose is to have 2 arguments $1 username and $2 password, the script should login on the given mail server and grep the line +OK POP3. The problem is i don't know how to i tell the computer to wait a short period of tine between the telnet 'line' and the user 'line' and the the login fails...
Any suggestions are welcomed.

Thanks!

UPDATE:
I have seen that actualy the script waits until the telnetl conetions is over and then uses the imput data as i cauld se using:
Code:

function mail ()
{
telnet mail.rdslink.ro 110

echo "user" $1

echo "pass" $2

list

grep '+OK POP3'
}

And i get:
Code:

root@MainThinkTank:~# mail bosharelu behlicuta
Trying 193.231.236.20...
Connected to mail.rdslink.ro.
Escape character is '^]'.
+OK Hello there.
exit
quit
Connection closed by foreign host.
user bosharelu
pass behlicuta
-bash: list: command not found


spirit receiver 07-07-2006 04:11 AM

Try this:
Code:

telnet mail.rdslink.ro 110 << EOF | grep '+OK POP3'
user $1
pass $2
list
EOF


Braynid 07-07-2006 04:44 AM

Thanks spirit receiver!

With:
Code:

function bmail ()
{
telnet mail.rdslink.ro 110 << EOF | grep '+OK POP3'
user $1
pass $2
list
EOF
}

I get:
Code:

root@MainThinkTank:~# bmail
Connection closed by foreign host.

and:
Code:

root@MainThinkTank:~# bmail user pass
Connection closed by foreign host.

Where 'user' and 'pass' are correct username and password on the domain.

I have tried some other stuff but didn't worked out...

spirit receiver 07-07-2006 07:57 AM

This probably means that there's no line that contains "+OK POP3", so grep doesn't return anything. Remove pipe and grep command, then have a look at the output.

Braynid 07-09-2006 09:40 AM

With:

Code:

function mail ()
{
telnet mail.rdslink.ro 110 << EOF
user $1
pass $2
list
EOF
}

I get:
Code:

root@MainThinkTank:~# mail bosharelu behlicuta
Trying 193.231.236.20...
Connected to mail.rdslink.ro.
Escape character is '^]'.
Connection closed by foreign host.

I really don't get it. How can i make this wayt and/or execute the 'user' and 'pass' commands...

Berhanie 07-09-2006 10:28 AM

Try it with netcat instead of telnet: replace the word 'telnet' in your script with 'nc'.

spirit receiver 07-09-2006 11:53 AM

You're right, it won't work that way. That's because the commands will be sent even before the remote server is ready to receive them. You'll have to use "expect":
Code:

#! /bin/bash

LOGIN=$1
PASSWORD=$2

/usr/bin/expect <<EOF | grep -o "mailbox has [0-9]\+ messages"

spawn /usr/bin/telnet mail.rdslink.ro 110
expect "+OK"

send "user $LOGIN\n"
expect "+OK"

send "pass $PASSWORD\n"
expect "+OK"

send "quit"

EOF

If your script will be more complicated, consider using Perl, it might be much more comfortable

Braynid 07-10-2006 01:51 AM

Thanks spirit receiver, i have modified that a bit and it works like a charm!
Thanks a lot guys for all the support!

spirit receiver 07-10-2006 02:34 AM

Actually, netcat (which is called "netcat" here instead of "nc") is much less awkward. Thanks, Berhanie.

Braynid 07-10-2006 03:35 AM

Ok so now my script is:

Code:

function mail ()
{

LOGIN=$1
PASSWORD=$2

/usr/bin/expect <<EOF | grep '+OK logged in.'

spawn /usr/bin/telnet mail.rdslink.ro 110
expect "+OK"

send "user $LOGIN\n"
expect "+OK"

send "pass $PASSWORD\n"
expect "+OK"

send "quit"

EOF
}

What if instead of the grep '+OK logged in.' i want it to echo 'login ok' , how can i do that?

spirit receiver 07-10-2006 03:45 AM

Code:

grep "+OK logged in." >/dev/null && echo "login ok"

Braynid 07-10-2006 04:30 AM

Ok, that's ok, works just like it should. I have still some more stuff to ask:

1) The '>/dev/null' line makes 'grep' don't show? More precisely what does it do?
2) Where did you learn all that stuff ?
3) I have a problem, how can i make this little script echo "Login fail" when either the user or password are incorrect? There is nothing to 'grep' for....

spirit receiver 07-10-2006 06:38 AM

Quote:

Originally Posted by Braynid
1) The '>/dev/null' line makes 'grep' don't show? More precisely what does it do?

It directs the standard output of grep to /dev/null, and everything that's sent to /dev/null will be discarded.
Quote:

2) Where did you learl all that stuff ?
I learled :D that stuff (and still do) from the man pages, searching the web and trial&error. A good reference for general bash scripting is http://www.tldp.org/LDP/abs/html/.
Quote:

3) I have a problem, how can i make this little script echo "Login fail" when either the user or password are incorrect? There is nothing to 'grep' for....
You can do some more scripting in Expect, see "man expect". The following might work, for example:
Code:

#! /bin/bash

LOGIN=$1
PASSWORD=$2

/usr/bin/expect <<EOF

log_user 0

spawn /usr/bin/telnet pop.something.net 110
expect {
  "+OK"        {send_user "Connection established.\n"}
  timeout      {send_user "Timeout.\n"; exit 1}
}

send "user $LOGIN\n"
expect {
  timeout      {send_user "Timeout.\n"; exit 1}
  "+OK"
}

send "pass $PASSWORD\n"
expect {
  "+OK*\n"      {send_user "\$expect_out(0,string)"}
  -gl "-ERR"    {send_user "Username or password incorrect.\n"}
}

send "quit"
EOF


Braynid 07-11-2006 12:43 AM

Usig the script you provided i get:

Code:

send: spawn id exp6 not open
    while executing
"send "quit""

I tried to 'gerp' for 'not open' and post an "Login failed" message but that didn't worked out..

I get some more errors and i'm confused becouse i don't really know what the exact commands do :P And i really have no time to look them up propperly, so much to do!
Code:

root@MainThinkTank:~# bmail a b
missing close-brace
    while executing
"expect {
  "+OK*\n"      {send_user "$expect_out(0,string)"}
  -gl "-ERR"    {send_user "Username or password incorrect.\n"}

send "quit"
send "quit"
"


spirit receiver 07-11-2006 02:41 AM

Seems like you forgot a closing brace, namely the one before 'send "quit"'.


All times are GMT -5. The time now is 07:38 AM.