Archive for the ‘Scripting’ Category

Renice 2: Even Nicer With Arrays

Tuesday, September 1st, 2009 by tburns

I put an entry out here that detailed how to find the process ID of a running process and then how to re-nice it from within a bash shell script. This first script referenced processes that could be identified by a single pid. But what about processes that spawn multiple pids, or multiple processes that are all named the same but are, in fact, separate instances?

Here is a small bash script segment that identifies process PIDs; puts them into an array; then uses that array to renice the processes. In this case, the processes are VoIP applications that use RTP (real time protocol), and, in particular SIP (Session Initiation Protocol) to put voice traffic on the wire and usually need immediate CPU access.

This script does many things before it gets to this point… we pick it up here at the renice process… Note that some of the variables you see here were instantiated early in the script. Also, to get the code to fit neatly on the page here, I took out a lot of the formatting of the echo statements that allows for clear logging…
Read more »

Linux remote backup via ssh

Friday, January 30th, 2009 by tburns

In yesterday’s entry, I posted a backup script that is used to create tarballs that are stored on the filesystem. This is the script that is run remotely that pulls those tarballs across the cloud to another machine.

This is used to run small backups remotely on, in this case, a virtualized machine running on a xen platform at linode.com. The virtualized machines are running primarily as webservers. The size of the backups is less than 20MB each.

Read more »

Linux backup script for on-disk backups

Thursday, January 29th, 2009 by tburns

Here’s a quick and easy backup script for linux servers. I use this to backup a web server without an attached tape drive. It runs a full backup each time – no incrementals… no GFS management… just a straight-ahead full backup written to disk then pulled down via ssh to another, remote server off-hours.

In this case, the server creates a file that is less than 20 MBs. So it’s easy to pull down later. This script allows for on-the-fly directories to be selected or unselected for backup. I always backup the home directories and the etc directories. Anything else can be listed separately.
Read more »

DHCP to static IP and back again

Wednesday, January 28th, 2009 by tburns

I was working on an infrastructure project one day where I needed to do some connectivity testing on select switch ports VLAN’d to a network that didn’t offer DHCP addressing to connected devices. I walked my laptop down to the switch stack and went through the process of setting my nic to a static IP along with static DNS entries and a new gateway. So, I decided it would be a lot easier to script it.

This script backs-up the original DHCP sttings; builds the static IP configs, along with DNS entries and gateway settings; re-initializes the interface to use those settings; then allows the user to restore the settings back to DHCP when done. It’s not complicated, it’s just easier than doing it all manually.

Read more »

adding users to a samba server

Sunday, January 18th, 2009 by tburns

This is an adduser script for a linux machine. I wrote it when I had to migrate a substantial number of users from several windows file servers to a newly-built linux server running samba. This new server, located at corporate headquarters, became the primary company file server, which meant it served files up to a large number of local – as well as remote – users. Many of these users did not need a home directory because their home directories lived elsewhere. But, because they had the need for access to files based on group membership, they needed accounts on the machine along with user-based access to group-managed files. This script made the job of moving them all – and assigning them group membership rights – a lot easier.

#!/bin/sh

   ##############################################
   #                                            #
   #    Adduser script for Linux Servers        #
   #                                            #
   #  Allows for variables to be added for      #
   #  customization: groups, home directories,  #
   #  comments, and more...                     #
   #                                            #
   #                                            #
   #     last modified: 01/18/09 by tburns      #
                      SCRIPT_VERSION="1.2"      #
   #                                            #
   #                                            #
   ##############################################

USERADD=/usr/sbin/useradd

USER_HOME_DEFAULT="0"   # if the home directory is /home enter a 1.
                        # Otherwise, enter "0" and a location below

if [ $USER_HOME_DEFAULT = "0" ]; then
     USER_HOME="/sambaFiles/clientFiles/users"
   else
     USER_HOME="/home"
fi

#Enter user data as prompted:
#First, prompt for userid
echo -e "Enter the userid: \c"
read USER_ID
echo ""

#Second, prompt for user's full name
echo -e "Enter the user's full name to be created: \c"
read USER_FULL_NAME
echo ""

#Third, prompt for primary group
echo -e "Enter the user's primary group name: \c"
read PRIMARY_GROUP
echo ""

#Fourth, find out if they need a home directory or not:
echo -e "Should this user have a home directory on this machine? y/n \c"
read CREATE_HOME_DIR
echo ""

#The following routine is for the fat-fingered:

if [ $CREATE_HOME_DIR = "Y" ] || [ $CREATE_HOME_DIR = "y" ]; then
   echo " Ok... The Home directory will be created here: $USER_HOME "
 elif [ $CREATE_HOME_DIR = "N" ] || [ $CREATE_HOME_DIR = "n" ]; then
   echo " This user will not have a Home directory locally "
 else
   echo "You entered $CREATE_HOME_DIR when prompted for a Y/y or an N/n. Try it again"
   echo ""
   echo -e "Should this user have a home directory on this machine? y/n \c"
     read CREATE_HOME_DIR

#But, if the fat-fingered can't get it right... we offer a bailout option:

    if [ $CREATE_HOME_DIR != "Y" ] || [ $CREATE_HOME_DIR != "y" ] ||[ $CREATE_HOME_DIR != "N" ] || [ $CREATE_HOME_DIR$
       echo ""
       echo "That's enough... check your facts! Here's what you entered:  "
       echo " USERADD = $USERADD "
       echo " USER_ID = $USER_ID "
       echo " USER_FULL_NAME = $USER_FULL_NAME "
       echo " PRIMARY_GROUP = $PRIMARY_GROUP "
       echo " CREATE_HOME_DIR = $CREATE_HOME_DIR "
       echo " Now, go back and try again "
    fi
exit 1
fi
echo ""

# Now, make all that stuff work together
echo "Here's what you've got so far: "
       echo " USERADD = $USERADD "
       echo " USER_ID = $USER_ID "
       echo " USER_FULL_NAME = $USER_FULL_NAME "
       echo " PRIMARY_GROUP = $PRIMARY_GROUP "
       echo " CREATE_HOME_DIR = $CREATE_HOME_DIR "
       echo " USER_HOME = $USER_HOME "

#Assuming we are still moving along... and that we may
#actually get to the end of this thing:
echo -e "Do you want to continue? y/n \c"
read CONTINUE
if [ $CONTINUE = "Y" ] || [ $CONTINUE = "y" ]; then
   if [ $CREATE_HOME_DIR = "Y" ] || [ $CREATE_HOME_DIR = "y" ]; then
        if [ $USER_HOME_DEFAULT = "0" ]; then
           echo " mkdir $USER_HOME/$USER_ID "
#          mkdir $USER_HOME/$USER_ID
           USER_HOME_DIR="$USER_HOME/$USER_ID"
           echo " $USERADD -G $PRIMARY_GROUP -c \"$USER_FULL_NAME\" -d $USER_HOME_DIR $USER_ID"
           `$USERADD -G $PRIMARY_GROUP -c "$USER_FULL_NAME" -d $USER_HOME_DIR $USER_ID`
           echo " chown $USER_ID $USER_HOME_DIR"
           chown $USER_ID:$USER_ID $USER_HOME_DIR
        else
           echo " $USERADD -G $PRIMARY_GROUP -c \"$USER_FULL_NAME\" $USER_ID"
           `$USERADD -G $PRIMARY_GROUP -c "$USER_FULL_NAME" $USER_ID`
        fi
   else
      echo " $USERADD -M -G $PRIMARY_GROUP -c \"$USER_FULL_NAME\" $USER_ID"
      `$USERADD -M -G $PRIMARY_GROUP -c "$USER_FULL_NAME" $USER_ID`
   fi
  else
    echo "Ok... we'll stop here..."
    echo ""
exit 1
fi

exit 0