Sponsored Content
Top Forums UNIX for Beginners Questions & Answers Email alert when client in blacklist joins network Post 302975829 by Don Cragun on Sunday 19th of June 2016 07:21:35 PM
Old 06-19-2016
The following script might help with your issues, but it is making LOTS of assumptions since you haven't answered many of my questions. If any of my assumptions are wrong, this script won't work. My assumptions include, but might not be limited to:
  1. Your flashdrive is mounted on the mount point /var/media/ftp.
  2. A primary goal of this modification to your script (whether it sits on your flashdrive, in your $HOME/bin directory, or in some other directory that you implicitly or explicitly specify the you run this script) is to reduce accesses to your flashdrive. After the initial start-up, this script accesses your flashdrive once per hour if your blacklist (alertlist.txt) does not change during the preceding hour or four times if the blacklist does change during that hour. Accesses to files off of the flashdrive are less efficient than they might otherwise be because the primary goal of these changes to your script is to reduce accesses to the flashdrive.
  3. The interpreter used to run your script is not a traditional Bourne shell. The following script was written and tested using a Korn shell, but should work with any mostly POSIX-conforming shell (such as bash, dash, ksh, or zsh). It will NOT work with any csh derivative and will NOT work with a traditional Bourne shell.
  4. The script /var/media/ftp/look.sh can be moved off of the flashdrive into a different directory and run from that alternative location without referencing any other files on the flashdrive.
  5. This script looks for blacklisted nearby WiFi users and then sleeps for 4 seconds; it does not make any attempt to run every four seconds. (If it takes one second to run the code in the loop, this means the loop will be run about twelve times per minute instead of fifteen times per minute. I doubt that it will take one second to run one iteration of the loop, but the type of hardware you'r using, the amount of available memory you have, scheduling delays, system load, networking delays in delivering e-mail, etc. clearly may introduce delays that will keep this script from running 15 times per minute.)
  6. The only way to stop this script is to kill it (and if kill -9 is used to stop it, it will not be able to remove the temporary work areas it uses). It would be a good idea to add a stop file that the script can examine every time it goes through the loop and if that stop file is found, it will stop and exit normally.
  7. There is no attempt to keep multiple invocations of this script from running simultaneously. If that is a problem in your environment, add code to keep another invocation from running if the script is already running.
  8. Additional checks will be added to verify that commands run inside the loop complete successfully. This script is provided for demonstration purposes and is not suitable for use as production code.
  9. You will change the e-mail address shown in red in this script to be one or more valid e-mail addresses o people who want to receive the output produced by this script.
Code:
#!/bin/ksh
IAm=${0##*/}			# basename of this script
TempDir="/tmp/$IAm.$$"		# script working directory
ThumbDir='/var/media/ftp'	# Flashdrive mount point

BlackList='alertlist.txt'	# List of names on the blacklist
LookScript='look.sh'		# Script to look for nearby WiFi users
Log="$TempDir/log"		# Log of issues to report on this loop iteration
OldFile="$TempDir/old"		# LookScript output from previous loop iteration
NewFile="$TempDir/new"		# LookScript outupt from current loop iteration

# Remove script working directory on script termination.
trap 'rm -rf "$TempDir"' EXIT

# Verify that needed files are present and copy them from flashdrive to work
# area.
if ! cd "$ThumbDir"
then	printf '%s: Thumb Drive not mounted at "%s".  Aborting.\n' \
	    "$IAm" "$ThumbDir" >&2
	exit 1
fi
if [ ! -f "$BlackList" ] || [ ! -x "$LookScript" ]
then	ls -l "$BlackList" "$LookScript"
	printf '%s: "%s" or "%s" not found in "$ThumbDir".  Aborting.\n' \
	    "$IAm" "$BlackList" "$LookScript" "$ThumbDIr" >&2
	exit 2
fi

# Create a temporary work area (not on the flashdrive)...
if ! mkdir -p -m 0700 "$TempDir"
then	printf '%s: Could not create work area ("%s").  Aborting.\n' \
	    "$IAm" "$TempDir" >&2
	exit 3
fi
if ! cp "$BlackList" "$LookScript" "$TempDir"
then	printf '%s: Copying files to work araa failed.  Aborting.\n' "$IAm" >&2
	exit 4
fi

# And note the hour when the above files were copied.
hr=$(date +%H)

# Note also that each of the following executions of look.sh, grep, mv, and
# mailx shouldbe checked for successful completion, but doing that is left as
# an exercise for the reader...

# Note that whenever $Log is a non-empty file, a mail message will be sent...
# so the following will send a message the first time we go through the
# following loop even if no blacklisted users are nearby when this script
# starts.
date +"$IAm started on %x at %X" > "$Log"

# Get initial list of nearby blacklisted users.  (Note that this list is going
# to $OldFile instead of $NewFile so it can serve as the comparison point for
# the 1st time through the following loop.)
"$TempDir/$LookScript" active | grep -Fix -f "$TempDir/$BlackList" > "$OldFile"

# And, if it is not empty, log them as newly near...
[ -s "$OldFile" ] && while read -r name
do	printf '%s joined\n' "$name"
done < "$OldFile" >> "$Log"

# Now run the body of our script every four seconds.  Note that it would be
# more efficient to change directory to $TempDir, but staying on the flashdrive
# reduces accesses to the flashdrive.
while sleep 4
do	# Check to see if we need to update the blacklist at the top of every
	# hour...
	now=$(date +%H)
	if [ $now != $hr ]
	then	# We have just started a new hour, check to see if the blacklist
		# has changed.
		[[ "$BlackList" -nt "$TempDir/$BlackList" ]] &&
		    cp "$BlackList" "$TempDir/$BlackList" &&
		    echo  "$BlackList update installed." >> "$Log"
		# If your shell does not accept the syntax of the above test
		# command list, # comment it out and uncomment the following
		# slower find command that will make more accesses to the
		# flashdrive than the above AND list.
		# find . -name "$BlackList" -newer "$TempDir/$BlackList" \
		#     -exec cp {} "$TempDir/$BlackList" \; \
		#     -exec echo "$BlackList update installed." \; >> "$Log"
		hr=$now
	fi

	# Find blacklisted users currently on nearby WiFi.
	"$TempDir/$LookScript" active | grep -Fix -f "$TempDir/$BlackList" \
	    > "$NewFile"

	if [ -s "$OldFile" ] && [ -s "$NewFile" ]
	then	# Gather list of blacklisted users who are no longer nearby.
		grep -Fivx -f "$NewFile" "$OldFile" | while read -r name
		do	printf '%s left\n' "$name"
		done >> "$Log"

		# Gather list of blacklisted users who were not seen last time.
		grep -Fivx -f "$OldFile" "$NewFile" | while read -r name
		do	printf '%s joined\n' "$name"
		done >> "$Log"
	elif [ -s "$OldFile" ]
	then	# Gather list of blacklisted users who are no longer nearby.
		while read -r name
		do	printf '%s left\n' "$name"
		done < "$OldFile" >> "$Log"
	elif [ -s "$NewFile" ]
	then	# Gather list of blacklisted users who were not seen last time.
		while read -r name
		do	printf '%s joined\n' "$name"
		done < "$NewFile" >> "$Log"
	fi

	# If anything is different on this iteration, report our findings.
	if [ -s "$Log" ]
	then	mailx -s "Nearby blacklisted users" user@company.com < "$Log"
		# Clear the log file and move the current user list to be the
		# previous user list for the next iteration of the loop.
		> "$Log"
		mv "$NewFile" "$OldFile"
	fi
done

These 2 Users Gave Thanks to Don Cragun For This Post:
 

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Email alert script

I need to code a script, which will run via cron, every 30 minutes. The script will read a file containing a date&time and number (which represents disk space). The file gets appended to every 30 minutes. Here's a sample of the file: CPU 1:04/25/02 1:00 am:1972554 CPU 1:04/25/02 1:30... (1 Reply)
Discussion started by: moon
1 Replies

2. Programming

HELP broadcasting client IDs to network in C

I am trying to write a client server chat program in C and am unsure as to how I would broadcast all the users IDs over the network. At the moment I have the usernames stored in a array clientsock.cl_id, I am able to output this information to the server but am unsure how to transmit this... (1 Reply)
Discussion started by: dooker
1 Replies

3. IP Networking

Squid and email client

Hi All, We plan to use squid has a proxy so my question is if I use squid then is this support email clients like outlook express or microsoft outlook at client side and is any option in the squid to block few of the unwanted URL's Thanks, Bachegowda (7 Replies)
Discussion started by: bache_gowda
7 Replies

4. Shell Programming and Scripting

email Alert

Hello, I want a script that will scan the file /etc/httpd/conf/httpd.conf and the folder /etc/httpd/libexec/ -bash-2.05b# grep mod_r /etc/httpd/conf/httpd.conf LoadModule rewrite_module libexec/mod_rewrite.so AddModule mod_rewrite.c -bash-2.05b# -bash-2.05b# find... (4 Replies)
Discussion started by: fed.linuxgossip
4 Replies

5. UNIX for Dummies Questions & Answers

new to ldap, send email to a ou or group, and see a list from email client

hi, i'm running openldap on ubuntu 10.04, creating new items with apache directory studio (windows version). i use the ldap just as an address book to our small office (email clients are windows live mail 2009, 2011, microsoft outlook 2007 and 2010). a. i cant see a list of the contacts,... (0 Replies)
Discussion started by: V4705
0 Replies

6. Shell Programming and Scripting

Using top command to email if process is exceeding 25% and sending an email alert if so

This is my first time writing a script and Im having some trouble, Im trying to use the top command to monitor processes and the amount of CPU usage they require, my aim is to get an email if a process takes over a certain percentage of CPU usage I tried grep Obviosly that hasnt worked, Any... (8 Replies)
Discussion started by: jay02
8 Replies

7. UNIX for Beginners Questions & Answers

Email Alert in UNIX

Hi There I have to wrote a script where I am able to echo a result of an SQL script, however I want to be able to send an email to myself when it is more than 0 (so whenever a value is returned) is this possible? I tried one way from looking on the web but this didn't work, I have added my... (8 Replies)
Discussion started by: simpsa27
8 Replies

8. Shell Programming and Scripting

Email alert after termination

I am running the gaussian program on UNIX with bash and I want to form a script that will email me once the output life terminates either "normal termination" or "false" I just started learning this last week so could you let me know how to go about this.:b: (13 Replies)
Discussion started by: Jade_Michael
13 Replies
All times are GMT -4. The time now is 09:33 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy