# Sylvain Robitaille # # A collection of one-line scripts that have been useful for various # tasks. ####################################################################### # Mail stats ... # 2004/04/14 Most often triggered SpamAssassin rules (MimeDefang as # Sendmail milter, using Mail::Spamassassin Perl module): grep action=spamscore /var/adm/syslog/maillog |\ grep -vw detail=0 |\ awk '{print $10}' |\ tr , '\012' |\ grep -v '^$' |\ sort |\ uniq -c |\ sort -nr # 2004/09/07 Distribution of delay times for sending messages to a # particular address: grep -h 'to=<${ADDRESS}@${DOMAIN}>, ' /local/var/log/system/maillog.* |\ awk '{print $8}' |\ awk -F[=,] '{print $2}' |\ sort -nr |\ uniq -c # 2004/10/13 Ordered list of hosts trying to send to unknown users: for m in `grep 'User unknown$' /var/adm/syslog/maillog |\ awk '{print $6}'`; do grep -w $m /var/adm/syslog/maillog |\ awk '$10 == "nrcpts=0," {print $13}' |\ sed 's/^relay=//; s/^.*@//' done | sort |uniq -c |sort -nr # 2004/10/14 Ordered list of hosts that disconnected before sending # data, without issuing a QUIT command: egrep -w "lost input channel from .* to MTA after rcpt" \ /var/adm/syslog/maillog |\ awk '{print $11}' |\ sort | uniq -c | sort -nr # 2004/11/25 Ordered list of hosts (optionally within a range) that sent # messages which triggered high SpamAssassin scores: grep "detail=spamassassin score " /var/adm/syslog/maillog |\ grep relay=132.205. |\ awk '{print $7}' |\ sed 's/^relay=\([^,]*\),/\1/' |\ sort | uniq -c | sort -nr # 2006/01/27 Top 10 hosts sending mail through "this" mail server, # ordered by number of SMTP sessions (assumes a recent # version of sendmail as the mail server): grep "daemon=MTA, relay=" /var/adm/syslog/maillog |\ sed 's/^.*, daemon=MTA, relay=//' |\ awk '{print $1}' |\ sort | uniq -c | sort -nr |\ head # 2006/09/20 Top 10 hosts having messages still in mail queues echo /var/spool/mqueue/*/qf* |\ xargs grep -h '^\$_' |\ sed 's/^..//' |\ sort |uniq -c |sort -nr |\ head ####################################################################### # More mail administrator stuff: # from a post to alt.os.linux.slackware, by Loki Harfagr # Message-Id: # To dequeue mail messages, force the queue run, with a timeout of 0, sendmail -v -q -oT0 # If you know the destination address, use : sendmail -v -qRadressee -oT0 # Or, with the Id (like your k7NFIx8r012657) sendmail -v -qIk7NFIx8r012657 -oT0 # It will try once then declare it dead for timeout. ####################################################################### # General use/system administration/other ... # getting a total of disk space used on a system: df -k | awk '{total += $3}; END {print total}' ####################################################################### # Quota calculations on a DEC-Unix system: # NOTE: refer to various mount points as necessary. # getting disk quota usage and commitment totals: repquota -uv /var/spool/mail |awk '{used+=$3;soft+=$4;hard+=$5}; \ END {print "used = " used; print "soft = " soft; print "hard = " hard}' # Finding users with the highest disk usage: repquota -uv /var/spool/mail |sort -k3 -nr # Calculating commitment if quotas are increased: repquota -uv /var/spool/mail |\ awk '{if($4 < 10000 && $4 > 0){$4 = 10000; $5 = 40000}; \ used+=$3;soft+=$4;hard+=$5}; \ END {print "used = " used; print "soft = " soft; print "hard = " hard}' ####################################################################### # From my own contributions to the Unix-Wizards mailing list: # Date: Mon, 19 Mar 2001 13:33:39 -0500 (EST) # From: Sylvain Robitaille # Subject: Re: SORT question # Message-ID: # # On Mon, 19 Mar 2001, Dave Weigel wrote: # # > ... we have some files that are fixed length records with no '\n' # > delimiter after each record. Can the Unix 'sort' be used to sort # > these types of files? If so, how? # > ... # Create a filter to insert a newline after every 80 characters, (Perl is # ideal for this), do the sort, then filter again to remove newlines: cat records |\ perl -p -e 's/(.{80})/$1\n/g;' |\ sort .... |\ perl -p -e 'chomp;' # Date: Fri, 23 Mar 2001 19:10:56 -0500 (EST) # From: Sylvain Robitaille # Subject: Re: Carriage Return # Message-ID: # # On Fri, 23 Mar 2001 16:20:29 -0600, Doug Lindstrom wrote: # # > How can I place a carriage return in a flat text file every 350 # > charaters? This is a big 1 line file that I need broken down. # # I solved a problem just like this earlier this week (on this very same # mailing list)... perl -p -e 's/(.{350})/$1\n/g' < oldfile > newfile # Date: Wed, 18 Apr 2001 16:21:29 -0400 (EDT) # From: Sylvain Robitaille # Subject: Re: how to insert text into a file # Message-ID: # # On Sat, 14 Apr 2001, Jay Prakash wrote: # # > I want to insert commands between lines(say ###start-here and # > ###end-here) in the main file. # # You're on the right track with sed, but Perl is likely a better tool for # this job. Something like the following (untested, and unrefined, but # you should get the idea): while (<>) { # This prints out everything up to and including the line # containing "###start-here", without any changes: print; last if m%^###start-here$%; } opendir THISDIR, "." or die "can't open directory .\n"; foreach $file (readdir THISDIR) { next if (! -f $file); # perform whatever needs to be done, sending output to stdout. } closedir THISDIR or die "can't close directory .\n"; while (<>) { # continue sending our stdin to stdout, untouched. print; } # Date: Thu, 19 Apr 2001 14:49:28 -0400 (EDT) # From: Sylvain Robitaille # Subject: Re: Script question # Message-ID: # # On Thu, 19 Apr 2001, Sanjay Kale wrote: # # > I have one file (f1) which has an entry # > kale $name # > ... # > Now I want the output to be # > kale sanjay # > i.e. $name read from file f1 should be # > replaced by sanjay (as defined in the script). # # Use sed, not ksh: sed 's/\$name/sanjay/' infile # Date: Mon, 9 Jul 2001 15:11:32 -0400 (EDT) # From: Sylvain Robitaille # Subject: Re: insert a cahracter every 80 lines # Message-ID: # # On Fri, 6 Jul 2001, Alex Zaleski wrote: # # > I need a compact (ksh) solution to format a text file. I need to # > insert a "1" and then a RETURN every 80 lines starting with line #1, # > so it would be on line #1, 81, 162, etc. # # I'm not sure if there's a typo in your suggested line numbers, so I'm # going to print two solutions, with only a subtle difference between # them: # # If you meant: line #'s 1, 82, 163, 244, etc. use: perl -p -e 'print "1\n" if($. % 80 == 1)' < input_file # If you meant: line #'s 1, 81, 161, 241, etc. use: perl -p -e 'print "1\n" if($. % 79 == 1)' < input_file # Date: Fri, 13 Jul 2001 12:28:42 -0400 (EDT) # From: Sylvain Robitaille # Subject: Re: Wiritng a shell script # Message-ID: # # On Fri, 13 Jul 2001, Ashutosh Mehta wrote: # # > I am writing shell script to automate the task of checking the contents # > of file and then compare it with threshold value and if figure is higher # > than threshold value, trigger the alarm. # # Given the sample file contents you provided, I think it's a safe bet # that all lines you're interested in have leading whitespace. Assuming # that's correct, check this out (NOTE: this hasn't been tested, but it's # probably a good start): # -----8<----- cut here -----8<----- #!/bin/sh # Path to our data file: DATAFILE=/path/to/datafile # Our threshold: THRESHOLD=25 # First, get the highest number out of the data file: N=`grep "^ " ${DATAFILE} |\ awk '$1 > total {total = $1}; END {print total}'` # If the data is above our threshold, send mail: if [ ${N} -gt ${THRESHOLD} ]; then sendmail -t << END_OF_ALERT From: Data Watcher To: Database Administrator Subject: Threshold crossed! Here are the contents of the data file: ${DATAFILE}: `cat ${DATAFILE}` You probably want to shutdown the reactor core soon! END_OF_ALERT fi -----8<----- cut here -----8<----- ####################################################################### # From others' contributions to various resources From: Loki Harfagr Subject: Re: sort IP addresses Newsgroups: alt.os.linux.slackware ... $ awk '{$1+=1000; $2+=1000; $3+=1000; $4+=1000; print}' FS=. input |\ sort -n |\ awk '{$1-=1000; $2-=1000; $3-=1000; $4-=1000; print}' OFS=. ... From: Laurenz Albe Newsgroups: alt.os.linux.slackware Subject: Re: sort IP addresses ... sort -t. -n -k1,1 -k2,2 -k3,3 -k4,4 ... From: Mark Hill Newsgroups: alt.os.linux.slackware Subject: listing man pages - was Re: slrn help ... find $(echo $MANPATH | sed 's/:/ /g') -type f ... # Manipulating wildcard filenames; example is renaming all files ending # in "txt" to ${filename}.old. Result in this case is: # mv inventory.txt inventory.txt.old # mv logs.txt logs.txt.old # mv netstat.txt netstat.txt.old # mv reports.txt reports.txt.old # ... # From: B.Yanchitsky@gmail.com Newsgroups: alt.os.linux.slackware Subject: Re: A curiosity with cp and rm ... ls *txt | sed 's/.*/mv & &.old/' | sh ... # Local variation of the above, produces: # mv inventory.txt inventory.old # mv logs.txt logs.old # mv netstat.txt netstat.old # mv reports.txt reports.old # ... ls *txt | sed 's/.*/mv & `basename & .txt`.old/' | sh