Hammer script to get output from Job by id

This is a simple script I use to export the details from a Job run in Red Hat Satellite 6.2 using hammer from the console.

#!/bin/bash
# Redhat Satellite Job query by id using hammer

if [ $1 -eq $1 ] 2>/dev/null; then
# get hosts run by ID
JOBHOSTS=$(hammer job-invocation info –id $1 | sed ‘1,/Hosts/d’ | awk {‘print $2’} | awk ‘NF’)

# Loop hosts
for HOST in $JOBHOSTS
do
echo “================================================================================
$HOST
================================================================================”
hammer job-invocation output –id $1 –host $HOST
echo “”
done
else
echo “You need to type the Job ID”
fi

Generate IP-address ranges using simple bash script

This is just a simple script to generate IP-addresses in a IP-range and write the result output to a file.
The script does also remove addresses you would like to exclude from the final output.

#!/bin/bash
OUTFILE="mk-iprange.txt"
IPRANGE="192.168.0 
192.168.1"
EXCLUDE="192.168.0.1
192.168.0.2
192.168.1.1"

# Remove old OUTFILE
rm -f $OUTFILE

# Loop addresses, write to OUTFILE
for IP in $IPRANGE
do
        seq -f "$IP.%g" 1 255 >> ./$OUTFILE
done

# Exclude IP-addresses from file (inplace replacement)
for EX in $EXCLUDE
do
        sed --in-place "/$EX/d" ./$OUTFILE
done

Replace specific HTML tags using sed and regular expression

In this post I would like to replace all <embed> HTML tags in a HTML file with a <strong> tag.

# sed -e 's/<embed[^>]*>/<strong>/g' filename.html > newfile.html

And if you would like to remove the <embed> tag altogether

# sed -e 's/<embed[^>]*>//g' filename.html > newfile.html

To remove all HTML tags in a file

# sed -e 's/<[^>]*>//g' filename.html > newfile.html

The result file newfile.html is now without any < HTML tags >.

IP address change notifier script

This is a simple bash script that is run by crontab every 5 minutes on a linux box.
It e-mails me the new address when a change of IP address is detected.

The script (ipchangemail.sh)

#!/bin/bash 

# Check if IP-address has changed. If a change has occured, mail me the new address
# Add the following line to crontab if you would like it to be run every 5 minutes:
# */5 * * * * ./ipchangemail.sh

# The network interface I want to monitor
NET_INTERFACE=eth0

# File to keep the latest IP address
IP_FILE=myip.txt

# Mail to this address when a change occur
MAILTO=mail@example.com


# Read the previous IP address from file
source $IP_FILE

CURRENT_IP=`/sbin/ifconfig $NET_INTERFACE | sed -n "/inet addr:.*255.255.25[0-5].[0-9]/{s/.*inet addr://; s/ .*//; p}"`

if [ "$CURRENT_IP" != "$OLD_IP" ]
then
        # Send email about address change
        `echo "New IP address detected: $CURRENT_IP" | mail -s "New IP address" $MAILTO`

        # Write new address to file
        `echo "OLD_IP=$CURRENT_IP" > $IP_FILE`
fi

The script can be downloaded here.

Apache web connections pr hour

This is a bash oneliner to show Apache web connections pr hour. It lists up the IPs that has accessed your webserver and the amount og accesses.

# cat /var/log/apache2/access_log_pario.no | grep "21/Jan/2008:.." | awk {' print $4":"$1 '} | sed 's/\[//g' | awk -F : {' print $1":"$2"\t\t"$5 '} | sort | uniq -c

Example output

37 21/Jan/2008:00          192.168.0.10

This shows that I had 37 hits from 00:00 – 01:00 in 20th February 2008.