Simple Time Saving Scripts

by Ostatic Staff - Jul. 12, 2010

We've all got them, the one liners, the aliases, the time savers that we use to ever so slightly tweak our environment. The handful of shell scripts that we use every day to speed up simple tasks. I have several, but these three are by far my most used. Many of my scripts rely on a master list of server names in /etc/servers that I put on my control system, and keep up to date with servers that I'm responsible for. These simple scripts have saved me thousands of keystrokes over the years.

 

Saving keystrokes for my number one most common task is the first scripts job, named go. Go opens an ssh session to the server name, so instead of typing “ssh username@server.domain.com”, I type “go server”.


#!/bin/sh  
# go - ssh into the specified server  
#  
############################################################ 
# Make sure the user actually entered something here  
if [ -z "$1" ]; then  
	echo "Usage: go [servername]" 
  exit 1  
else  
	# Set the variables  
	SERVERNAME=`echo $1`  
	USERNAME=`whoami`  
	# run the command	  
	ssh -Y $USERNAME@$SERVERNAME  
fi  
############################################################ 
# EOF: go


Super simple, and super useful.


The next script I run on a regular basis is named “all_serv”, and as its name suggests, it's purpose is to take commands as arguments that need to be run on all of the servers listed in the /etc/server file. Today I ran this script to find which servers had a filesystem with a certain name, the command looked like “all_serv 'hostname; df | grep filesystem_name'”. I waited for a second, and the script printed the server names to the screen.


#!/bin/sh  
# all_serv -  ssh to each server in /etc/servers and run a command  
#  
############################################################ 
for each in `cat /etc/servers`; do  
	ssh $each $*  
done  
############################################################ 
# EOF: all_serv

 

The third script is a simple one for looking up the IP address of a server. Named “ipl” for IP Lookup, this one comes in handy for the times when I need to put a name to the IP address listed in the logs.


#!/bin/sh  
# ipl - IP Lookup, lookup the IP address in DNS  
#  
############################################################ 
# Make sure the user acutally entered something here  
if [ -z "$1" ]; then  
        echo "Usage: ipl [servername]"  
  exit 1  
else  
        # Set the variables  
        SERVERNAME=`echo $1`  
        # run the command        
        nslookup -sil $SERVERNAME.primarydomain.com | grep Address | egrep -v 192.168.0.1  
        if [ $? -ne 0 ]; then  
                nslookup -sil $SERVERNAME.secondarydomain.com | grep Address | egrep -v 192.168.0.1 
        fi  
fi  
############################################################ 
# EOF: ipl


The great thing about these three scripts is that they produce easily parseable results, so they can be chained together, or called from other scripts. I know I said only three scripts, but here's a fourth one, just as a bonus.


#!/bin/sh  
# newscript - Creates a new file, makes it executable, and  
# opens vi for editing.   
#  
############################################################ 
if [ -z "$1" ] ; then  
	echo "Usage: $0 filename" >&2; exit 0  
fi  
# Check to see if that file name exists 
if [ -f ./$1 ] ; then  
	echo "That file name already exists, please choose another." >&2  
else  
	touch $1  
	chmod +x $1  
	vi $1  
fi  
############################################################ 
# EOF: newscript     


I use this script in combination with a customized exrc file to make creating a new script a matter of a few keystrokes.


None of these scripts are at all complicated, but I absolutely love how much time they save me every day. They've become so much a part of my workflow that I don't even think about them anymore, and that's the sign of a good tool.


Do you have any one-liners or small, time-saving scripts you'd like to share? Let us know about them in the comments! I'm always looking for ways to make common tasks faster and more efficient.