If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Need some help, please. . .

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-10-04, 18:04
josherz01 josherz01 is offline
Registered User
 
Join Date: Mar 2004
Posts: 3
Lightbulb Need some help, please. . .

I have an interesting problem that I have not been able to solve myself. I need to strip out ip addresses from a mail server log file from authenticated POP3 and IMAP connections and save them to a file. Reason for this is that I just installed assp (anti spam smtp proxy) and I want to plug these ip addresses into the relayhosts file. This way anyone who successfully logs in by POP3 or IMAP will have full relaying priveledges.

Now this script should recognize duplicate ip addresses and not add them to the file.

Here is a sample of successful POP3 and IMAP logins:

02:24:21.14 2 POP-24199 ([10.0.0.1]) 'user@domain' connected from [10.0.0.1:60765](temp client)

15:00:06.33 2 IMAP-01201([10.0.0.1]) 'user@domain' connected from [10.0.0.1:32055](temp client)

Any help would be greatly appreciated! Thank You.
Reply With Quote
  #2 (permalink)  
Old 03-11-04, 03:22
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
Something like this ?
Code:
HOSTS=relayhosts.dat
LOG=mail.log

awk '
NF>1 {
   sub(/.*\[/,"") ;
   sub(/].*/,"") ;
  if ($1 != "") print
} ' $LOG | \
sort -u -o $HOSTS $HOSTS -
If you don't want to keep the port number, replace :
sub(/].*/,"") ;
By
sub(/:.*/,"") ;
__________________
Jean-Pierre.
Reply With Quote
  #3 (permalink)  
Old 03-11-04, 16:21
josherz01 josherz01 is offline
Registered User
 
Join Date: Mar 2004
Posts: 3
Ah thank you so much. . .I'll give it a try

EDIT: Not the exact results I'm looking for, but its close. Using the : instead of the ] gives me a line that starts with an IP addy but has unfiltered data behind it, example:

170.215.88.150]) 0 {980} retrieved, 44187 bytes

But also it appears to pick up other weird info from the log, which is not what I need, example:

18595] SMTP(hotmail.com)hz220fnco@hotmail.com failed

It would seem to me that at the begin of the script, we need to some how filter for data pertaining to 'POP-' and 'IMAP-' first. Otherwise I get lists of ip addy's for rejected messages/etc.

Last edited by josherz01; 03-11-04 at 17:14.
Reply With Quote
  #4 (permalink)  
Old 03-11-04, 17:26
S_Scheible S_Scheible is offline
Registered User
 
Join Date: Feb 2004
Posts: 17
You can grep the POP- and IMAP- lines and pipe them into the awk command suggested by aigles:
# grep -E POP-\|IMAP- | thatawkcommand
You can also pipe the output of that into a cut command that will remove everything after the square bracket, sort it and eliminate duplicates:
# thatgrepcommand | thatawkcommand | cut -f1 -d\] | sort | uniq

Personally, I avoid awk because I know only a few awk commands.
# grep -E POP-\|IMAP- filename.log | cut -f2 -d\[ | cut -f1 -d\] | sort | uniq
should select the lines containing POP- or IMAP-, cut away everything before the opening square bracket, then everything behind the closing one, then sort and eliminate duplicate IPs. Might be slower than awk though.
Reply With Quote
  #5 (permalink)  
Old 03-11-04, 17:30
josherz01 josherz01 is offline
Registered User
 
Join Date: Mar 2004
Posts: 3
Ahh that makes sense. . .I'll give that a try as well.

That actually looks like it might have worked! Thank You!

Last edited by josherz01; 03-11-04 at 17:33.
Reply With Quote
  #6 (permalink)  
Old 03-12-04, 02:43
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
A new version of my script :
- Selects lines POP- and IMAP-
- The ip address is all time the second [xxx] field
- The port number is removed
- The file $HOST which contains already known ip addresses is updated.

Code:
HOSTS=relayhosts.dat
LOG=mail.log

awk '
/POP-|IMAP-/ {          # Select input lines lines
   sub(/.*\[.*\[/,"") ; # remove chars from start to second [
   sub(/].*/,"") ;      # remove chars from [ to end
   sub(/:.*/,"");       # remove port number
   print                # print ip address
} ' $LOG | \
sort -u -o $HOSTS $HOSTS -
__________________
Jean-Pierre.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On