LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Help writing a script... (https://www.linuxquestions.org/questions/linux-newbie-8/help-writing-a-script-837860/)

linux_evangelist 10-13-2010 02:16 PM

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?

Tinkster 10-13-2010 02:45 PM

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

J_Szucs 10-13-2010 03:04 PM

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.

grail 10-14-2010 02:02 AM

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

linux_evangelist 11-05-2010 04:04 PM

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?

Tinkster 11-05-2010 04:12 PM

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

linux_evangelist 11-12-2010 12:09 PM

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
To:office@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.)

grail 11-15-2010 07:19 AM

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.


All times are GMT -5. The time now is 03:43 PM.