LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 10-13-2010, 02:16 PM   #1
linux_evangelist
LQ Newbie
 
Registered: Oct 2010
Posts: 14

Rep: Reputation: 0
Post Help writing a script...


What I want to do is update a configuration file with a list of current usernames and leave the rest of the config file intact. Here's a sample of what the file looks like:

# Check the /usr/share/doc/sendmail/README.cf file for a description
# of the format of this file. (search for access_db in that file)
# The /usr/share/doc/sendmail/README.cf is part of the sendmail-doc
# package.
#
# by default we allow relaying from localhost...
localhost.localdomain RELAY
localhost RELAY
127.0.0.1 RELAY

To:mydomain.com error:5.1.1:"550 User unknown"
Connect:mydomain.com RELAY


I know how to append entries to a list, but what I'm trying to do is fill out the list between To:mydomain.com and Connect:mydomain.com. The end result should look something like:

# Check the /usr/share/doc/sendmail/README.cf file for a description
# of the format of this file. (search for access_db in that file)
# The /usr/share/doc/sendmail/README.cf is part of the sendmail-doc
# package.
#
# by default we allow relaying from localhost...
localhost.localdomain RELAY
localhost RELAY
127.0.0.1 RELAY


To:mydomain.com error:5.1.1:"550 User unknown"
To:user1@mydomain.com RELAY
To:user2@mydomain.com RELAY
To:user3@mydomain.com RELAY
To:user4@mydomain.com RELAY
To:user5@mydomain.com RELAY
To:user6@mydomain.com RELAY
To:user7@mydomain.com RELAY
To:user8@mydomain.com RELAY
To:user9@mydomain.com RELAY
To:user10@mydomain.com RELAY
To:user11@mydomain.com RELAY
Connect:mydomain.com RELAY


So what I'm meaning to accomplish is to read 'user*' from a list and write "To:$USERNAME@mydomain.com RELAY" while maintaining the structure of the config file. Any thoughts from the community?
 
Old 10-13-2010, 02:45 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Hi, welcome to LQ!

Code:
awk '{print} /To:mydomain.com error:5.1.1:"550 User unknown"/{for(i=1;i<12;i++){print "To:user"i"@mydomain.com RELAY"}}' file > newfile
Untested, should work.



Cheers,
Tink

Last edited by Tinkster; 10-13-2010 at 02:46 PM.
 
Old 10-13-2010, 03:04 PM   #3
J_Szucs
Senior Member
 
Registered: Nov 2001
Location: Budapest, Hungary
Distribution: SuSE 6.4-11.3, Dsl linux, FreeBSD 4.3-6.2, Mandrake 8.2, Redhat, UHU, Debian Etch
Posts: 1,126

Rep: Reputation: 58
Code:
string="To:user13@mydomain.com RELAY"; sed 's/^\(Connect:mydomain.com RELAY\)$/'"$string"'\n\1/' file > newfile
Tested, it should work (as far as there is a line "Connect:mydomain.com RELAY" in file).
Though this just adds one user, so it should be modified to cycle through users. But then it becomes slightly less efficient than Tinkster's solution, so, stick with the latter.

Last edited by J_Szucs; 10-13-2010 at 03:12 PM.
 
Old 10-14-2010, 02:02 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
If your user list is coming from a separate file which has actual usernames, ie not ordered user1, user2, the following will help:
Code:
awk '1;/To:mydomain.com/{while((getline name < "userfile") > 0){print "To:"name"@mydomain.com RELAY"}}' file > newfile
 
1 members found this post helpful.
Old 11-05-2010, 04:04 PM   #5
linux_evangelist
LQ Newbie
 
Registered: Oct 2010
Posts: 14

Original Poster
Rep: Reputation: 0
Okay, so I'm having trouble getting anywhere with this and it is because of my own ignorance. Here's what I'm trying to accomplish:

I'm trying to configure /etc/access so that it automatically updates itself with new customers, and set it as a cron job.

The process that I'm having trouble with is to get the usernames from /etc/features and /etc/mail/aliases into the /etc/access file.

I've been wrestling with this for some time now and I'm just trying to patch pieces of information that I gather into a haphazard script and i'm not doing it right. I've given an honest, whole-hearted attempt at doing this on my own and now it's time to throw in the towel. Is there anyone who can help me fix my actual problem?
 
Old 11-05-2010, 04:12 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
If you told us which bits of grails or my own suggestions didn't
work for you, or what you actually got to, maybe?

W/o more input, or actual data, there's not much ANYONE can do
about your problem.


Cheers,
Tink
 
Old 11-12-2010, 12:09 PM   #7
linux_evangelist
LQ Newbie
 
Registered: Oct 2010
Posts: 14

Original Poster
Rep: Reputation: 0
Okay, so here's what I'm trying to accomplish: I'm trying to configure /etc/mail/access so that it rejects mail to unknown users in order to prevent backscatter spam. I've gotten it all set up, but now I'm trying to write a script which will update the /etc/mail/access file with new email addresses. Here's what I have so far:

1) I grab the current users from /etc/features

cut -d *.* -f1 /etc/features > /etc/features.tmp

Here's an example of what the /etc/features file holds.

before:

root:mail=0,ftp=0,description=root
ftp:mail=-1,ftp=-1,description=Anonymous FTP
office:mail=10,ftp=-1,description=Company - Receptionist
msmith:mail=40,ftp=-1,description=Company - Mark Smith
johnson:mail=25,ftp=-1,description=Michael Johnson
...

after:

root
ftp
office
msmith
johnson

Next, I need to add aliases to this list.

praliases | egrep -v "(^#.*|^$)"

I later found out that the target server I'm running this on doesn't have egrep and I can't install software on it accept a subset of applications that the hosting provider supplies. I need a new way to do this (perhaps just with grep)

Next, I run this list through sort and uniq.

The last step is the one I'm having the most trouble with. I am trying to add these usernames to the /etc/mail/access file while conforming to the file's structure.

Here's the file now:

# Check the /usr/share/doc/sendmail/README.cf file for a description
# of the format of this file. (search for access_db in that file)
# The /usr/share/doc/sendmail/README.cf is part of the sendmail-doc
# package.
#
# by default we allow relaying from localhost...
localhost.localdomain RELAY
localhost RELAY
127.0.0.1 RELAY

#To:mydomain.com RELAY
To:mydomain.com error:5.1.1: "550 User unknown"
Connect:mydomain.com RELAY

...and here's what it should be:

# Check the /usr/share/doc/sendmail/README.cf file for a description
# of the format of this file. (search for access_db in that file)
# The /usr/share/doc/sendmail/README.cf is part of the sendmail-doc
# package.
#
# by default we allow relaying from localhost...
localhost.localdomain RELAY
localhost RELAY
127.0.0.1 RELAY

#To:mydomain.com RELAY
To:mydomain.com error:5.1.1: "550 User unknown"
To:root@mydomain.com RELAY
To:ftp@mydomain.com RELAY
Toffice@mydomain.com RELAY
To:msmith@mydomain.com RELAY
To:johnson@mydomain.com RELAY
Connect:mydomain.com RELAY

I don't know if the Connect:mydomain.com has to be the last line in the config file or not, but if not, I imagine we could just append new entries to the bottom of the list. That would make things simple. My thoughts are:

grep out usernames and aliases, sort them, remove duplicates, compare them to the current list of usernames/aliases and put any of the new ones in the access file (appending "To:" and "@mydomain.com RELAY" of course.)

Last edited by linux_evangelist; 11-12-2010 at 12:14 PM.
 
Old 11-15-2010, 07:19 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Now that more information has come to light I would suggest that a single one liner probably will not be enough.
This is rough but I think you will get the gist:
Code:
#!/bin/bash

while IFS=':' read -r NAME _
do
    if ! grep -q "To:$NAME" /etc/mail/access
    then
        EMAIL+=("To:$NAME@mydomain.com\tRELAY")
    fi
done</etc/features

sed -i "/#To:mydomain.com.*/a${EMAIL[*]}" /etc/mail/access
sed -i 's/RELAY /RELAY\n/' /etc/mail/access
Pretty sure the seds could be better but I am a little tired.
 
  


Reply



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
Need help writing script Rustylinux SUSE / openSUSE 1 12-05-2006 10:05 AM
Help writing script Geminias Linux - Newbie 9 08-02-2006 09:45 PM
Need help for writing Script vikrambhimbar Linux - Software 2 03-05-2006 04:28 AM
Help writing a rm script VisionZ Linux - Newbie 17 03-24-2004 07:15 AM
writing a Script spanky5125 Linux - Security 5 01-08-2002 09:22 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:37 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
Open Source Consulting | Domain Registration