LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-06-2006, 11:12 AM   #1
Braynid
Member
 
Registered: May 2006
Location: Romania
Distribution: CentOS
Posts: 140

Rep: Reputation: 15
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

Last edited by Braynid; 07-06-2006 at 11:15 AM.
 
Old 07-07-2006, 04:11 AM   #2
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
Try this:
Code:
telnet mail.rdslink.ro 110 << EOF | grep '+OK POP3'
user $1
pass $2
list
EOF
 
Old 07-07-2006, 04:44 AM   #3
Braynid
Member
 
Registered: May 2006
Location: Romania
Distribution: CentOS
Posts: 140

Original Poster
Rep: Reputation: 15
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...
 
Old 07-07-2006, 07:57 AM   #4
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
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.
 
Old 07-09-2006, 09:40 AM   #5
Braynid
Member
 
Registered: May 2006
Location: Romania
Distribution: CentOS
Posts: 140

Original Poster
Rep: Reputation: 15
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...
 
Old 07-09-2006, 10:28 AM   #6
Berhanie
Senior Member
 
Registered: Dec 2003
Location: phnom penh
Distribution: Fedora
Posts: 1,625

Rep: Reputation: 165Reputation: 165
Try it with netcat instead of telnet: replace the word 'telnet' in your script with 'nc'.
 
Old 07-09-2006, 11:53 AM   #7
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
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
 
Old 07-10-2006, 01:51 AM   #8
Braynid
Member
 
Registered: May 2006
Location: Romania
Distribution: CentOS
Posts: 140

Original Poster
Rep: Reputation: 15
Thanks spirit receiver, i have modified that a bit and it works like a charm!
Thanks a lot guys for all the support!
 
Old 07-10-2006, 02:34 AM   #9
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
Actually, netcat (which is called "netcat" here instead of "nc") is much less awkward. Thanks, Berhanie.
 
Old 07-10-2006, 03:35 AM   #10
Braynid
Member
 
Registered: May 2006
Location: Romania
Distribution: CentOS
Posts: 140

Original Poster
Rep: Reputation: 15
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?
 
Old 07-10-2006, 03:45 AM   #11
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
Code:
grep "+OK logged in." >/dev/null && echo "login ok"
 
Old 07-10-2006, 04:30 AM   #12
Braynid
Member
 
Registered: May 2006
Location: Romania
Distribution: CentOS
Posts: 140

Original Poster
Rep: Reputation: 15
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....

Last edited by Braynid; 07-10-2006 at 06:47 AM.
 
Old 07-10-2006, 06:38 AM   #13
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
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 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
 
Old 07-11-2006, 12:43 AM   #14
Braynid
Member
 
Registered: May 2006
Location: Romania
Distribution: CentOS
Posts: 140

Original Poster
Rep: Reputation: 15
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"
"

Last edited by Braynid; 07-11-2006 at 01:17 AM.
 
Old 07-11-2006, 02:41 AM   #15
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shel script mail send ("/" in mail address) problem anaid Linux - Networking 3 08-23-2005 07:41 AM
shell script and mail ntan81 Programming 4 10-29-2004 03:55 PM
CGI Script runs to send mail, but mail is never sent robertwo Linux - Newbie 2 06-10-2004 09:57 AM
mail script doesn't run wedgeworth Linux - Software 0 10-15-2003 01:02 PM
E-Mail notification to users via SMS (gateway script ok, but notification script?!?) Riku2015 Linux - Networking 10 03-08-2002 10:16 AM


All times are GMT -5. The time now is 09:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration