Get Days in a month from a bash script

Useful listing of days in a month to populate directories or run scripts for backing up getting the correct dates of days in a month and year.

 

 


YEAR=2020
MONTH=2

for DAY in $(DAYS=`cal $MONTH $YEAR | awk 'NF {DAYS = $NF}; END {print DAYS}'` && seq -f '%02G' $DAYS) ;do
DATE="$YEAR-$MONTH-$DAY"
echo $DATE
done

nJoy πŸ˜‰

Snapshot backup using cp -al and rsync

This script requires genuine cp -al capable gnu and rsync commands together with a hardlink capable FS + OS.

 

 

#!/bin/bash

[ $# -ne 2 ] && echo "Incorrect usage : $0 <source path> <target path>" && exit 128 ;

SOURCEFOLDER=$1
TARGETFOLDER=$2

SF_LEN=${#SOURCEFOLDER}-1
TF_LEN=${#TARGETFOLDER}-1

#echo "Last character in source folder is ${SOURCEFOLDER:SF_LEN}"
if [ "${SOURCEFOLDER:SF_LEN}" != "/" ] ; then
   echo "Adding trailing slash"
  SOURCEFOLDER=$SOURCEFOLDER"/"
fi



#echo "Last character in target folder is ${TARGETFOLDER:TF_LEN}"
if [ "${TARGETFOLDER:TF_LEN}" != "/" ] ; then
   echo "Adding trailing slash"
  TARGETFOLDER=$TARGETFOLDER"/"
fi



echo $SOURCEFOLDER
echo $TARGETFOLDER

LOCKFILE=/tmp/`echo $0 $SOURCEFOLDER $TARGETFOLDER | sed "s/[^[:alnum:]]/_/g"`.lck
echo "Lockfile : $LOCKFILE"



[ ! -d $SOURCEFOLDER ] && echo "Source does not exist !! $SOURCEFOLDER exitting with error" && exit 1;

TIMESTAMP=$(date --utc +%Y%m%d%H%M )
#echo $TIMESTAMP



if [ ! -d $TARGETFOLDER ]; then 

mkdir $TARGETFOLDER

rsync -av --delete  $SOURCEFOLDER $TARGETFOLDER/$TIMESTAMP/

else 

[ -d $TARGETFOLDER/$TIMESTAMP/ ] && echo "Folder already there !! Leaving.. " && exit 0;  

 LASTBACKUP=$(ls $TARGETFOLDER | sort -rn | head -1)
 echo "Link copying $TARGETFOLDER/$LASTBACKUP to $TARGETFOLDER/$TIMESTAMP/"
 cp -al $TARGETFOLDER/$LASTBACKUP $TARGETFOLDER/$TIMESTAMP/
 rsync -av $SOURCEFOLDER $TARGETFOLDER/$TIMESTAMP/

fi

echo " OK !! Done"

 

 

 

test Graylog GELF UDP input from Bash

for i in {1..100} ; do echo '{"version": "1.1","host":"david.org","short_message":"A short message that helps you identify what is going on","full_message":"Backtrace here\n\nmore stuff","level":1,"_user_id":9001,"_some_info":"foo","_some_env_var":"bar"}' | nc -w 1 -u graylog.mydomain.com 12201  ; done

 

nJoy πŸ˜‰

Script to verify that a path is synchronized across multiple machines via ssh

This is a script I wrote for work to look through a number of remote servers via ssh (shared keys or include a .pem (id_rsa) file to compare a paths and all it’s subfolders.

The result is a report of which files are out of synch and if duplicates found they are listed separately.

#!/bin/bash
## Parameters  sync_check [-i id.pem] [-u <user for ssh>] -p <path_to_diff>  <target-system-IP1> <target-system-IP2> ...

# David Saliba [dx@sudoall.com]

RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color


PEMFILE=""
SSH_USER="root"

function usage {
	echo 
	echo -e "\t$0 usage as follows :"
	echo -e "\t$0 [-i <pem_file>] [-u <ssh_user_name>] -p <path_to_diff> <ref-system-IPADDR1> <target-system-IP1> <target-system-IP2>..."
	echo 
}


[ $# -le 2 ] && echo && echo -e "${RED}Incorrect number of parameters ${NC}" && usage && exit 128


while getopts "i:u:p:" opt; do
echo $opt $OPTARG
  case $opt in
  	i ) PEMFILE=$OPTARG; shift $((OPTIND-1)); OPTIND=1 ; 
          if [[ -f "$PEMFILE" ]]; then 
              PEMFILE="-i ""$PEMFILE" 
          else
          	  echo "Pemfile : $PEMFILE not found. Quitting .. " 
          	  exit 1  
          fi
          ;;
	u ) SSH_USER=$OPTARG ; shift $((OPTIND-1)); OPTIND=1 ;;
	p ) PATH_TO_CHECK=$OPTARG ; shift $((OPTIND-1)); OPTIND=1 ;;
    * ) usage
       exit 128
  esac
done



extfile="_target.md5.sync_check"

		echo; echo

rm -f /tmp/*.sync_check
for i in $* ; do
	ipcalc -cs $i
	if [ $? == 0 ] ; then
		echo -n "Retrieving Signature List from ..  :[$i] : " 
		ssh $PEMFILE $SSH_USER@$i "hostname"

		COMMAND="ssh $PEMFILE $SSH_USER@$i \"find  $PATH_TO_CHECK -type f -exec md5sum {} \\; | sed -e 's/^/$i\t/'| sort -k 2 \""
		#echo "Running : $COMMAND  /tmp/$i""_target.md5.sync_check"
		bash -c "$COMMAND" > /tmp/$i$extfile
		
	else
		echo "Parameter $i not a valid IP skipping .."
	fi
done

echo ;echo; echo "Comparing data ... "; echo

 OLDIFS=$IFS; 
 IFS=$'\n'; 

 for i in `cat /tmp/*.sync_check  | sort -k3 | uniq -f 1 -c | sort | egrep -v "^[\ \t]*$#" | sort -k4 ` ;do 
 	
 	NUMBER_OF_HITS=`echo "$i" | awk '{print ($1)}'`

 	echo $i

 	if [ $NUMBER_OF_HITS -gt 1 ] ; then
 		
 		MD5Onward=`echo "$i" | awk '{print substr($0, index($0,$3))}'`
		echo -e -n "${YELLOW}"	
			grep -h "$MD5Onward" /tmp/*.sync_check  | awk '{print "\t\t",NR, $0}'
		echo -e -n "${NC}"	


 	fi


 done ;

 IFS=$OLDIFS


echo
echo




link to file here:Β http://jist.sudoall.com/sync_check/sync_check.sh

nJoy πŸ˜‰

Bash test if port is open no external tools

Assume you do not have curl/telnet/netcat (or nc does not support -z as in later versions ?? why did they remove it ?? ) the following will work on any recent Bash version.


(echo > /dev/tcp/skinner/22) >/dev/null 2>&1 && echo "Port is open" || echo "Port is closed"

nJoy πŸ˜‰

Using document here in Bash script for sending a message to all who is loaded.

Β 
#!/bin/bash

wall <<zzz23EndOfMessagezzz23
E-mail your noontime orders for pizza to the system administrator.
    (Add an extra dollar for anchovy or mushroom topping.)
# Additional message text goes here.
# Note: 'wall' prints comment lines.
zzz23EndOfMessagezzz23

# Could have been done more efficiently by
#         wall <message-file
#  However, embedding the message template in a script
#+ is a quick-and-dirty one-off solution.

exit

 

nJoy πŸ˜‰

Sample parameter validation script.sh

#!/bin/bash


echo "`basename $0` Tool"


[[ $# != 3 ]] && echo "Invalid number of arguments" && echo "Sample Call : create_ftp.sh <numeric_partner_id> <username_alphanum> <password_alphanum>" && exit 128

#number re
re='^[0-9]+$'

if [[ $1 =~ $re ]] ; then
		PARTNERID=$1
                echo "Partner id set to $1"
        else
        	echo "Invalid param #1 should be numeric" 
        	echo "Sample Call : create_ftp.sh <numeric_partner_id> <username_alphanum> <password_alphanum>" 
         	exit 128
         	
fi

re='^[a-zA-Z0-9][-a-zA-Z0-9]{0,61}[a-zA-Z0-9]$'
if [[ $2 =~ $re ]] ; then
		USERNAME=$2
                echo "Username set to $2"
        else
        	echo "Invalid param #2 should be alphanumeric" 
        	echo "Sample Call : create_ftp.sh <numeric_partner_id> <username_alphanum> <password_alphanum>" 
         	exit 128
         	
fi

if [[ $3 =~ $re ]] ; then
		PASSWORD=$3
                echo "Password set to $3"
        else
        	echo "Invalid param #3 should be alphanumeric" 
        	echo "Sample Call : create_ftp.sh <numeric_partner_id> <username_alphanum> <password_alphanum>" 
         	exit 128
         	
fi


 

njoy πŸ˜‰

Decoding parameters in Bash script (1 space delimited)

This is a small script that takes a bash command and decodes the parameters in non sequential order except definers.

#!/bin/bash

if [ "$#" -ne 1 ] || ! [ -d "$1" ]; then
  echo "Usage: $0 " >&2
  exit 1
fi




while [[ $# > 1 ]]
do
  key=”$1β€³
  echo $key
  shift
  case $key in
   -c|–color|color)
     COLOR=$1
     shift
     ;;
   -s|–section|section)
     SECTION=”yes”
     ;;
    *)

;;
esac
done

nJoy πŸ˜‰

Replacing a piece of XML with awk

This script searches for an initial tag and and closing one and replaces the content.

# !/bin/bash
awk ‘
BEGIN {pr = 1;one = 0}
/<Name>OPENING<\/Name>/ {pr = 0;}
{ if (pr) print }
{ if (!pr && !one) {print (“\t\t <Name>OPENING</Name> \n \t\t\t <Value>false</Value> \n \t\t<Type>STOPHERE</Type> \n ” ); one =1 ;}}
/<Type>STOPHERE<\/Type>/ {pr = 1;}
‘< $1

Not the most elegant solution but lends itself to multiple replacements.

 

find . | grep file.xml | xargs -I {J} -n 1 bash -c ‘ ./aw.sh {J} > {J}.new ; mv -f {J}.new {J} Β ‘

nJoy πŸ˜‰