Misc scripts

There seem to be a ton of scripts that I’ve cooked up for various purposes but are now obsolete. Here’s a quick sampling:

checkmail.ksh:
I can’t explain my reasoning for this one – it works but the functionality it provides is built into ksh, tcsh, zsh, bash, and dash đŸ˜› (Not the original Bourne shell, C shell, or the Almquist shell, however)

#!/bin/ksh                                                                      
                                                                                
# checks mail every minute while logged in                                      
while [[ 1 ]]                                                                   
do                                                                              
        echo > `tty`                                                            
        sleep 60                                                                
done

dirfinder.sh:
See above – It works but I have no idea why I would ever need such a thing.

#!/bin/bash                                                                     
#                                                                               
# Finds all directories on a given filesystem                                   
#                                                                               
FS="tmp home opt usr usr/local var var/lib/mysql var/ftp var/cache var/spool var
/log var/www boot"                                                              
                                                                                
for fs in $FS                                                                   
do                                                                              
        find /${fs} -xdev -type d -print 2>/dev/null > ~/dirs_in_$(echo $fs | sed 's/\//_/g')                                                                   
done  

du_by_user.ksh:
This one’s actually useful to get a quick overview of disk usage by user without enabling CPU-intensive disk quotas.

#!/bin/ksh                                                                      
                                                                                
# reports disk usage by user for each user in /etc/passwd                       
# accepts a filesystem as an argument                                           
                                                                                
USERS=`cat /etc/passwd | awk -F ":" '{print $1}`                                
FILESYSTEM=$1                                                                   
                                                                                
for user in $USERS                                                              
do                                                                              
        K_USED=`nice find ${FILESYSTEM}/ -xdev -user $user -type f -exec du -k {} \; | awk '{ s = s + $1 } END { print s }'`                                    
        if [ "$K_USED" != "" ]                                                  
        then                                                                    
                echo "Space used by $user on $FILESYSTEM: $K_USED"              
        fi                                                                      
done

types.sh:
It works but I have no idea why I’d ever need this kind of information.

#!/bin/bash                                                                     
#                                                                               
# Lists file types on system                                                    
#                                                                               
                                                                                
DIRS=`nice find / -type d -print`                                               
                                                                                
for dir in "$DIRS"                                                              
do                                                                              
        cd "$dir"                                                               
        nice file * | cut -d ":" -f 2  | sed -e 's/^[ \t]*//' | sed 's/[ \t]*$//' | sort | uniq -c                                                              
done 

checkpasswd.sh:
This one’s useful for routine checks to verify the permissions of important system files.

#!/bin/bash
#
# Checks permissions on /etc/passwd and friends
#

cd /etc
READ_TESTS="passwd group shadow gshadow"
for file in $READ_TESTS
do
        test -r $file || mail -s "/etc/${file} not readable" root@dustpuppy << EOF
/etc/${file} not readable at `date`
Permissions:
`ls -laZ $file`

EOF
        test -O $file || mail -s "/etc/${file} not owned by root" root@dustpuppy << EOF
/etc/${file} not owned by root at `date`
Permissions:
`ls -laZ $file`

EOF

        test -G $file || mail -s "/etc/${file} not group owned by root" root@dustpuppy << EOF
/etc/${file} not group owned by root at `date`
Permissions:
`ls -laZ $file`

EOF

done

email_batch_updater.pl:
This one takes a file full of username:email_addr entries and batch updates the e-mail addresses.

#!/usr/bin/perl                                                                 
use warnings;                                                                   
use strict;                                                                     
                                                                                
my $filename = $ARGV[0];                                                        
my $line;                                                                       
my @entry;                                                                      
                                                                                
die  ("Need a file name") unless (defined($filename));                          
open (BATCH, $filename)   or die ("Unable to open $filename");                  
                                                                                
while ( $line  = <BATCH>) {                                                     
        @entry = split(/:/, $line);                                             
        system("usrprep $entry[0] $entry[1]");                                  
}                                                                               
                                                                                
close BATCH; 

The usrprep utility mentioned in the above is used as a custom argument to useradd to setup the user’s environment on dustpuppy.

#!/bin/bash                                                                     
#                                                                               
# prepares user home directories                                                
                                                                                
user=$1                                                                         
                                                                                
mkdir /export/home/$user                                                        
cp -R /etc/skel/.[a-zA-Z0-9]* /export/home/$user                                
chmod 0700 /export/home/$user                                                   
echo ":0" > /export/home/$user/.procmailrc                                      
echo "! " $2 >> /export/home/$user/.procmailrc                                  
chown -R $user:$user /export/home/$user

fix_dotfile_perms.sh:
Sometimes dotfiles acquire weird permissions modes. This fixes them.

#!/bin/bash                                                                     
#                                                                               
# finds dotfiles in home directories, removes world write persimmons            
#                                                                               
                                                                                
DOTFILES=`nice find /home -xdev \( -type f -o -type d \) -name "\.*" \( -perm /0020 -o -perm /0002 \) -ls -exec chmod go-w \{\} \;`                             
#echo $DOTFILES                                                                 
linecount=`echo $DOTFILES | wc -m`                                              
                                                                                
if [ $linecount -gt 2 ]                                                         
then                                                                            
        mail -s "Bad Dotfile Persimmons" root@dustpuppy << EOF                  
The following dotfiles had bad persimmons:                                      
$DOTFILES                                                                       
                                                                                
They were fixed.                                                                
                                                                                
-cron daemon on dustpuppy                                                       
                                                                                
EOF                                                                             
                                                                                
fi

motd:
Sets the system message of the day.

#!/bin/bash
#
# Edits the MOTD
#

TMPFILE=/tmp/motd.`date +%F`.$UID

# First, copy the existing MOTD
if [ -e /etc/motd.src ]
then
        cp /etc/motd.src $TMPFILE
else
        cp /etc/motd $TMPFILE
fi

# Edit the copy
vi $TMPFILE

# Check to see if the new MOTD is blank
chars=`cat $TMPFILE | wc -m` # yes, it is dumb but if I ask wc to read the file directly,
                             # it will print the filename after the character count which will cause
                             # bash's [ builtin to bork when I try to -gt on text
#echo $chars
if [ $chars -lt 2 ]
then
        # ask if the user really wants to clear the MOTD
        echo "Do you REALLY want to clear the MOTD?"
        read response

        if [ $response == "Y" ] || [ $response == "y" ] || [ $response == "yes" ]
        then
                cat /dev/null > /etc/motd
                rm /etc/motd.src
                rm $TMPFILE
                exit
        fi
fi

# Ask if the user really wants to set the MOTD
cat $TMPFILE
echo "Do you REALLY want to set this as the MOTD?"
read response

if [ $response == "Y" ] || [ $response == "y" ] || [ $response == "yes" ]
then
        date > /etc/motd
        cat $TMPFILE >> /etc/motd
        echo $USER >> /etc/motd
        cp $TMPFILE /etc/motd.src
fi

rm $TMPFILE

unroll_list.pl:
I wrote this when I was transitioning from static alias lists to GNU mailman. It unrolls the alias lists into formatted e-mail lists that mailman can use to build mailing lists from.

#!/usr/bin/perl                                                                 
use warnings;                                                                   
use strict;                                                                     
                                                                                
my $aliases;                                                                    
my @emails;                                                                     
                                                                                
open(ALIASES, "/etc/aliases") or die $!;                                        
                                                                                
while ($aliases = <ALIASES>) {                                                  
        last if ($aliases =~ m/^everyone:/);                                    
}                                                                               
                                                                                
@emails = split(/,/, $aliases);                                                 
chomp foreach (@emails); # om nom nom                                           
@emails = sort @emails;                                                         
foreach (@emails) {                                                             
        print "$_\n" if $_ =~ m/@/;                                             
}

fix_exec_perms.sh:
This one scans for nonexecutable files in the network file store and removes their execute bits if set (Samba is bad about handling execute bits consistently).

#!/bin/bash                                                                     
#                                                                               
# Removes executable permissions from non-executable files                      
#                                                                               
                                                                                
nice find /home/homelan/ -type f -perm /u+x,g+x,o+x -exec /usr/local/bin/validate_exec \{\} \;

validate_exec:
Looks up the file type in the types database to see if it should logically be executable. If not, it unsets the execute bit.

#!/bin/sh                                                                       
#                                                                               
# Checks to see if a file is an executable type and if not, removes execute bit 
#                                                                               
                                                                                
file "$1" | grep "executable\|script" > /dev/null                               
                                                                                
if [ $? -ne 0 ]                                                                 
then                                                                            
        #echo "$1 is not executable."                                           
        chmod a-x $1                                                            
fi     

verifyperms.sh:
Checks for various permissions problems on the filesystem and does what it can to fix them.

#!/bin/bash
#
# Looks for bad directory persimmons and fixes them.
#

FS_LIST="/ /tmp /home /opt /usr /usr/local /var /var/lib/mysql /var/ftp /var/cache /var/spool /var/log /var/www"

for fs in $FS_LIST
do
        baddirs=`nice find ${fs}/ -xdev -type d \( -perm -0002 -a ! -perm -1000 \) -ls -exec chmod +t \{\} \;`
        linecount=`echo $baddirs | wc -m`
        #echo $linecount
        if [ $linecount -gt 2 ]
        then
                mail -s "Bad Directory Persimmons on $fs" root@dustpuppy << EOF
Bad Directory Persimmons found for the following directories:
$baddirs

They were all fixed.

-cron daemon on dustpuppy

EOF
        fi

        badfiles=`nice find ${fs}/ -xdev -type f -perm -0002 -ls`
        linecount=`echo $badfiles | wc -m`
        if [ $linecount -gt 2 ]
        then
                mail -s "Bad File Persimmons on $fs" root@dustpuppy << EOF
Bad File Persimmons found for the following files:
$badfiles

Please fix by setting proper group ownership and removing world-writable bit.

-cron daemon on dustpuppy

EOF

        fi

        badfiles=`nice find ${fs}/ -xdev \( -nouser -o -nogroup \) -ls -exec chown nobody:nogroup \{\} \;`
        linecount=`echo $badfiles | wc -m`
        if [ $linecount -gt 2 ]
        then
                mail -s "Bad Ownership on $fs" root@dustpuppy << EOF
The following files are unowned:
$badfiles

They've been assigned to nobody:nogroup - please find out who they belong to and
assign the proper ownership.

-cron daemon on dustpuppy

EOF

        fi
done
Advertisements
  1. #1 by Andrew on February 14, 2011 - 9:51 AM

    Nice little collection of scripts, I love making little collections myself of scripts and code in case I might ever need it in the future.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: