LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-06-2013, 10:19 AM   #1
sbo
LQ Newbie
 
Registered: Aug 2012
Posts: 9

Rep: Reputation: Disabled
Wink Scripting Help Please


Hi Guys and girls,

I must first mention that I'm no scripting guru at all and I'm stuck with a daily exercise that I need to automate. You are welcome to propose a complete alternative to this if you feel it is better. I know this might be a bit too much but please any help will do.

I will try to be very clear of what I'm try to do.

Requirements: The script will login to multiple FreeBSD Exim servers and add or remove hosts from Exim’s reject files for blocked hosts/senders per IP or sender address.

1. The script needs to be in a TUI prompt and is ran from a remote Linux server.
2. The script should have 3 major functions on the first run prompt. Where you should be prompted to press key on your keyboard to perform a function.
(i) Check status
(ii) Add culprit
(iii) Remove culprit
3. It should use the current user’s login credentials to ssh to the servers then su – once logged in.
4. Since this is meant to be interactive we allow it to prompt for both the user and the root pass to login to the rest of the servers
5. Exim files are located in a directory called /usr/local/etc/exim/reject/
6. The script must called with an input value where the value is the IP/user@domain.com to be queried. E.g. “blacklist 41.x.x.86”

Function Processes:

(i) Check status (press 1 check status)
• This process should simply run a query like grep –R on both the local blocked_host.txt and on the 22 relays’ /usr/local/etc/exim/reject/my_blocked_hosts
• Then return a status message of something like “host not blocked” where the query seem to have returned null result or host never blocked before.
• Also in this case, the results will be in 2 phrases. “host not blocked” this host has been previously blocked 0 times.
• In the event where the is found on any of the 22 relays the status message should read something like “host found in /usr/local/etc/exim/reject/my_blocked_hosts” and then the function should go on and query the local file for the 2nd phrase to tell how many times it has been blocked. So full status message of a blocked host will be like “host found in /usr/local/etc/exim/reject/my_blocked_hosts, this host has been previously blocked x times.

(i) Add culprit (press 2 to add culprit)
• This function will have to append a local file called blocked_host.txt counter for which it will add that host and set the counter to an increment of +1 for it every time the same host is blocked or appended to the file.
• So if the culprit has not been entered to the file before the first entry will constist of 3 columns, first one being the IP or email address then the comment e.g. ‘SPAM-HOST’ followed by a numeric value in the 3rd column denoting the number for which the same entry has been captured/submitted to the file.
• In the even where the same entry has been captured before only the 3rd column will be modified by the increamental value of 1
• Then the next step is to go into all the 22 server and append that IP at the bottom of the file called /usr/local/etc/exim/reject/my_blocked_hosts
• Then finally return a status message of “successfully blocked”
(ii) Remove culprit(press 3 to remove culprit)
• This one is self-explanatory, the function needs to just go to the 22 relays scan the whole ../reject/ directory and remove any entry the queried value(IP or address)
• This will then return a “successfully removed” status code and it does not modify the local file ‘blocked_host.txt’.

I also want to run an audit log of the blocked_host.txt local file with just the day of modification and the line modified.

Last edited by sbo; 02-06-2013 at 10:26 AM.
 
Old 02-06-2013, 10:24 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 20,713

Rep: Reputation: 5091Reputation: 5091Reputation: 5091Reputation: 5091Reputation: 5091Reputation: 5091Reputation: 5091Reputation: 5091Reputation: 5091Reputation: 5091Reputation: 5091
Quote:
Originally Posted by sbo View Post
Hi Guys and girls,
I must first mention that I'm no scripting guru at all and I'm stuck with a daily exercise that I need to automate. You are welcome to propose a complete alternative to this if you feel it is better. I know this might be a bit too much but please any help will do.

I will try to be very clear of what I'm try to do.

Requirements: The script will login to multiple FreeBSD Exim servers and add or remove hosts from Exim’s reject files for blocked hosts/senders per IP or sender address.

1. The script needs to be in a TUI prompt and is ran from a remote Linux server.
2. The script should have 3 major functions on the first run prompt. Where you should be prompted to press key on your keyboard to perform a function.
(i) Check status
(ii) Add culprit
(iii) Remove culprit
3. It should use the current user’s login credentials to ssh to the servers then su – once logged in.
4. Since this is meant to be interactive we allow it to prompt for both the user and the root pass to login to the rest of the servers
5. Exim files are located in a directory called /usr/local/etc/exim/reject/
6. The script must called with an input value where the value is the IP/user@domain.com to be queried. E.g. “blacklist 41.x.x.86”

Function Processes:

(i) Add culprit (press 2 to add culprit)
• This function will have to append a local file called blocked_host.txt counter for which it will add that host and set the counter to an increment of +1 for it every time the same host is blocked or appended to the file.
• So if the culprit has not been entered to the file before the first entry will constist of 3 columns, first one being the IP or email address then the comment e.g. ‘SPAM-HOST’ followed by a numeric value in the 3rd column denoting the number for which the same entry has been captured/submitted to the file.
• In the even where the same entry has been captured before only the 3rd column will be modified by the increamental value of 1
• Then the next step is to go into all the 22 server and append that IP at the bottom of the file called /usr/local/etc/exim/reject/my_blocked_hosts
• Then finally return a status message of “successfully blocked”
(ii) Check status (press 1 check status)
• This process should simply run a query like grep –R on both the local blocked_host.txt and on the 22 relays’ /usr/local/etc/exim/reject/my_blocked_hosts
• Then return a status message of something like “host not blocked” where the query seem to have returned null result or host never blocked before.
• Also in this case, the results will be in 2 phrases. “host not blocked” this host has been previously blocked 0 times.
• In the event where the is found on any of the 22 relays the status message should read something like “host found in /usr/local/etc/exim/reject/my_blocked_hosts” and then the function should go on and query the local file for the 2nd phrase to tell how many times it has been blocked. So full status message of a blocked host will be like “host found in /usr/local/etc/exim/reject/my_blocked_hosts, this host has been previously blocked x times.
(iii) Remove culprit(press 3 to remove culprit)
• This one is self-explanatory, the function needs to just go to the 22 relays scan the whole ../reject/ directory and remove any entry the queried value(IP or address)
• This will then return a “successfully removed” status code and it does not modify the local file ‘blocked_host.txt’.

I also want to run an audit log of the blocked_host.txt local file with just the day of modification and the line modified.
Sounds like a good description of your homework. We'll be happy to help....so just post what you've written, and tell us where you're stuck.

But, we aren't going to write a script for you.
 
Old 02-06-2013, 12:03 PM   #3
sbo
LQ Newbie
 
Registered: Aug 2012
Posts: 9

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by TB0ne View Post
Sounds like a good description of your homework. We'll be happy to help....so just post what you've written, and tell us where you're stuck.

But, we aren't going to write a script for you.


Ohk, as I said I'm no scripting person but I've been using the ssh power tool scrip found at http://code.google.com/p/sshpt/sourc...k/sshpt.py?r=4 which I then simply feed the desired action/commands in the quotes like ./sshpt.py -u myuser -P mypass -f hosts_file "sed -i.old /"197.x.x.165 SPAMHOST"/d /usr/local/etc/exim/reject/my_blocked_hosts" and also do the same for querying on my second line with but just had to change the command in quotes to do some grepping. Now this just logs into servers and modify files but does not address my needs of knowing how many times have I blocked this host and so forth(this is what I wish to introduce in my script).

So for that I would simply declare my shell as #!/usr/bin/sh and throw in those 2 lines calling the python login script to add or remove hosts in a form of an 'if' function and I exit.

I do know the basics and the general rules of scripting(bash) but it is never in my daily duty requirements. I set down and try to type the requirements as vivid as they can be to make it easy for anyone who would be helping me. I left it this open because I have seen that in most cases you type your erroneous script out of frustration to get things working then post it in a forum, get bitten, chewed and insulted for all the mistakes in it even though you would have pre-declared your novice status of the subject(which I don't mind) but then what is worse is that your erroneous fumble would be used as a prototype towards your solution even though someone else could have a complete different, quicker and better way altogether for the same end-results. hence I simply give description and leave it open, the description might be very long but this script could be fairly short.

And also, thanks TB0ne for your prompt response and direction.

Last edited by sbo; 02-06-2013 at 12:07 PM.
 
Old 02-06-2013, 12:05 PM   #4
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Yawnstown, Ohio
Distribution: Mojave
Posts: 9,345
Blog Entries: 36

Rep: Reputation: Disabled
Quote:
2. The script should have 3 major functions on the first run prompt. Where you should be prompted to press key on your keyboard to perform a function.
(i) Check status
(ii) Add culprit
(iii) Remove culprit
Why not just put "status" on the entry page, thus removing (i) Check status?
 
Old 02-06-2013, 12:09 PM   #5
sbo
LQ Newbie
 
Registered: Aug 2012
Posts: 9

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by sbo View Post
Ohk, as I said I'm no scripting person but I've been using the ssh power tool scrip found at http://code.google.com/p/sshpt/sourc...k/sshpt.py?r=4 which I then simply feed the desired action/commands in the quotes like ./sshpt.py -u myuser -P mypass -f hosts_file "sed -i.old /"197.x.x.165 SPAMHOST"/d /usr/local/etc/exim/reject/my_blocked_hosts" and also do the same for querying on my second line with but just had to change the command in quotes to do some grepping. Now this just logs into servers and modify files but does not address my needs of knowing how many times have I blocked this host and so forth(this is what I wish to introduce in my script).

So for that I would simply declare my shell as #!/usr/bin/sh and throw in those 2 lines calling the python login script to add or remove hosts in a form of an 'if' function and I exit.

I do know the basics and the general rules of scripting(bash) but it is never in my daily duty requirements. I set down and try to type the requirements as vivid as they can be to make it easy for anyone who would be helping me. I left it this open because I have seen that in most cases you type your erroneous script out of frustration to get things working then post it in a forum, get bitten, chewed and insulted for all the mistakes in it even though you would have pre-declared your novice status of the subject(which I don't mind) but then what is worse is that your erroneous fumble would be used as a prototype towards your solution even though someone else could have a complete different, quicker and better way altogether for the same end-results. hence I simply give description and leave it open, the description might be very long but this script could be fairly short.

And also, thanks TB0ne for your prompt response and direction.



And also I must mention that I have seen interactive scripts in use before but I must mention that I've never made one myself.
 
Old 02-06-2013, 12:14 PM   #6
sbo
LQ Newbie
 
Registered: Aug 2012
Posts: 9

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Habitual View Post
Why not just put "status" on the entry page, thus removing (i) Check status?
well 'status' it is then and thanks for the input mate, as I said the only sole purpose here is to automate the blocking and and unblocking and also keep track of the blocking record so perhaps I could take further action for persisting culprits. how it gets done is open for proposal, I just had to put something down as a base for direction.
 
Old 02-06-2013, 02:43 PM   #7
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 20,713

Rep: Reputation: 5091Reputation: 5091Reputation: 5091Reputation: 5091Reputation: 5091Reputation: 5091Reputation: 5091Reputation: 5091Reputation: 5091Reputation: 5091Reputation: 5091
Quote:
Originally Posted by sbo View Post
I do know the basics and the general rules of scripting(bash) but it is never in my daily duty requirements. I set down and try to type the requirements as vivid as they can be to make it easy for anyone who would be helping me. I left it this open because I have seen that in most cases you type your erroneous script out of frustration to get things working then post it in a forum, get bitten, chewed and insulted for all the mistakes in it even though you would have pre-declared your novice status of the subject(which I don't mind) but then what is worse is that your erroneous fumble would be used as a prototype towards your solution even though someone else could have a complete different, quicker and better way altogether for the same end-results. hence I simply give description and leave it open, the description might be very long but this script could be fairly short.
Well, fumbling and getting things incorrect is how you learn to do something. There isn't a Formula 1 racing driver alive today who was born knowing how to drive...they were all learners at some point. And "helping" is when YOU provide some of the work, and we assist with the parts where you're stuck...otherwise, it's just someone doing it for you. And I'm not sure how it is on other forums, but I've not seen people here get "bitten, chewed and insulted" for showing effort, ever.

The best way to learn to script is to just start doing it. Make mistakes, and research how to fix them, one by one. And there isn't ANY programmer ANYWHERE who won't do things differently from someone else with the same experience; "better" is a relative term. If your script works, then it is doing what you want; job done. As you learn more, you may be able to refine it, and do in one line what took you ten before...that's learning. And the script you posted isn't a bash script, but a python script...two different things.

If this was me, I'd keep all the files you're updating daily on your local system, and have a bash script make changes, then copy the results out to your remote systems with a simple scp call. You can even set up a user just for copying files (one that has no shell, and has keys swapped), so that the copies would not be interactive, and you could even make backup copies of the files you're modifying first, to let you roll back changes if there's an issue. So, (assuming your exim files are in one place)
Open your input hosts file
  • STEP 1 - STATUS grep -Ri "hostname" *
  • STEP 2 - ADD CULPRIT Use grep to find the line, awk to grab the pieces for counter increment, then sed to replace the existing line with the new one
  • STEP 3 - DEL CULPRIT Use sed to delete the line with the corresponding hostname
  • After steps 2 or 3, put your SCP statement in to copy the new files to their appropriate destinations.
Again, we'll be happy to help you...so post what you've written so far.
 
Old 02-06-2013, 03:23 PM   #8
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Yawnstown, Ohio
Distribution: Mojave
Posts: 9,345
Blog Entries: 36

Rep: Reputation: Disabled
Quote:
well 'status' it is then and thanks for the input mate...
Less code!
Requirements didn't say you couldn't.

This will bake your banana: No "Search culprits"?

Resource added:
Bash Scripting search

Last edited by Habitual; 02-06-2013 at 03:26 PM.
 
Old 02-06-2013, 04:17 PM   #9
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,393
Blog Entries: 55

Rep: Reputation: 3565Reputation: 3565Reputation: 3565Reputation: 3565Reputation: 3565Reputation: 3565Reputation: 3565Reputation: 3565Reputation: 3565Reputation: 3565Reputation: 3565
Quote:
Originally Posted by sbo View Post
(..) we allow it to prompt for both the user and the root pass to login (..)
Root should not SSH in.


Quote:
Originally Posted by sbo View Post
(..) the next step is to go into all the 22 server and append that IP at the bottom of the file called /usr/local/etc/exim/reject/my_blocked_hosts (..)
...so the essence is you need a cron job on each server to PULL in an update from your central repository and populate its exim/reject/my_blocked_hosts with that.
 
Old 02-27-2013, 03:06 AM   #10
sbo
LQ Newbie
 
Registered: Aug 2012
Posts: 9

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by unSpawn View Post
Root should not SSH in.



...so the essence is you need a cron job on each server to PULL in an update from your central repository and populate its exim/reject/my_blocked_hosts with that.
Gents, I used the scp method you advised where I keep the file in the central mysql db which from time to time exports the contents into a txt file which is where the rest of the relays will periodically scp to and update themselves. no ssh, the how many time and the like stories are setup in a tables fields.

Thanks
 
  


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
Scripting Help thudpuppy Linux - Newbie 4 06-23-2012 04:00 PM
LXer: Scripting the Linux desktop, Part 2: Scripting Nautilus LXer Syndicated Linux News 0 02-17-2011 05:02 AM
Firefox Scripting Add-on (Scripting HTML / Javascript inside Firefox) linuxbeatswindows Programming 1 09-18-2009 11:09 PM
teaching shell scripting: cool scripting examples? fax8 Linux - General 1 04-20-2006 05:29 AM
Scripting CICA Linux - General 7 10-03-2005 11:36 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:34 PM.

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