lawlRedneck

May 27, 2010

nerdcred++

Filed under: nerdery —— elmidgeto @ 6:41 am

Well, once again, I needed to find a solution to a non-existent problem. Well, almost non-existent.

The other day we had a rather unique error when trying to restart an Apache process. A stop command was issued, the process died off in the process list, but an orphaned session was keeping the http port tied up, so Apache could not restart.

What to do?

Well, given my awakening and increasing knowledge of the various commands and utilities in Linux, the only solution was to over-engineer a fully automated solution I could incorporate into a maintenance script.

With no further adieu, I give you a completely hands off way to kill the process holding on to a given port.

sudo -S kill -9 `sudo -S netstat -lpn < $SFL | grep :80 |  awk ' { split($7,pid,"/"); printf("%s",pid[1]) } '` < $SFL

$SFL is the absolute location of a file containing the sudo password of the account used to run the command(s).

Fairly simple, and overly engineered solution, so the only proper title was of course, nerdcred++. Anyone else have a more elegant solution that can be used in an automated script, and without the httpd.pid being in existence?

July 8, 2009

sftp/scp scripting

Filed under: nerdery —— elmidgeto @ 4:15 pm

The situation: I need to move files from one server to another using only the OpenSSH tools. All authentication/authorization is done via public key handshakes.

The restrictions: I cannot use expect or any other scripting language beyond shell scripts.

The goal: To minimize the number of characters I need to type and provide a platform for our batch guy to build on for the automated transfers.

And without further ado, have at thee. (Note: I did remove username, sftp connection string, and the root paths from my script to eliminate identification)

#!/bin/bash
######################################################
###### Utility script for Pushing/Pulling files ######
###### from the enterprise sftp solution. ############
######################################################

##################################################
############# SETTING THE VARIABLES ##############
##################################################

TYPE=$1
FILENAME=$2

USER=”"
SFTP=”"
BASEPATH=”"

SFTPPATH=$BASEPATH
LOCALPATH=`pwd`

MAX=$#
ARGS=(“$@”)

for ((COUNT=2; COUNT < MAX; COUNT++))
do
 if [ ${ARGS[$COUNT]} == “-s” ];then
      SFTPPATH=$SFTPPATH/${ARGS[$COUNT+1]}
      COUNT=$COUNT+1
 
 elif [ ${ARGS[$COUNT]} == “-l” ];then
      LOCALPATH=${ARGS[$COUNT+1]}
      COUNT=$COUNT+1 
 fi
done

##################################################
########### COPY FROM THE SFTP SERVER ############
##################################################

if [ $TYPE == "GET" ]; then
     scp $USER@$SFTP:$SFTPPATH/$FILENAME $LOCALPATH/$FILENAME

##################################################
############ COPY TO THE SFTP SERVER #############
##################################################

elif [ $TYPE == "PUT" ]; then
     scp $LOCALPATH/$FILENAME $USER@$SFTP:$SFTPPATH/$FILENAME

##################################################
########## DELETE FROM THE SFTP SERVER ###########
##################################################

elif [ $TYPE == "DEL" ]; then
     sftp -b /dev/stdin $USER@$SFTP <<INP
     cd $SFTPPATH
     rm $FILENAME
     ls
     bye
INP

##################################################
############### SCRIPT ARGS HELP #################
##################################################

elif [ $TYPE == "-h" ]; then
     echo “  sftp_util syntax is:”
     echo “  ./sftp_util.sh <GET|PUT|DEL|-h> <FileName> [-s] [-l]”
     echo “  GET GET is used to retrieve a file from the remote server.”
     echo “  PUT PUT is used to place a file on the remote server.”
     echo “  DEL DEL is used to delete a file from the remote server.”
     echo “  -h -h is used to print this help menu.”
     echo “  -s -s <sftp server path of/to file, do not include a leading ‘/’ (ex. test/test2)>”
     echo “  -l -l <sftp server path of/to file>”
 
else
     echo “Please use ./sftp_util.sh -h for help”
fi

Usage goes a little something like this:

server1$> ./sftp_util.sh PUT file -s test/empty_dir -l /home/user

server2$> ./sftp_util.sh GET file -s test/empty_dir -l /here

server2$ ./sftp_util.sh DEL file -s test/empty_dir

 

Nothing overly fancy, just a quick little script to save me time and have a bit of fun on a slow work day.

lawlRedneck is proudly powered by WordPress running on http://blogs.dootdoot.com.
Create a new blog and join in the fun! Entries (RSS) and Comments (RSS).