search for a port number

I find myself doing google everytime I want to search for port number mapping. So, here is a short script to do just that 🙂

#!/bin/bash -
#===============================================================================
#
# FILE: portfind.sh
#
# USAGE: ./portfind.sh
#
# DESCRIPTION:
#
# OPTIONS: ---
# REQUIREMENTS: ---
# BUGS: ---
# NOTES: ---
# AUTHOR: Amit Agarwal (aka),
# ORGANIZATION:
# CREATED: 08/29/2017 19:00
# Last modified: Tue Aug 29, 2017 07:00PM
# REVISION: ---
#===============================================================================
set -o nounset # Treat unset variables as an error
#This is the directory where you have mappings file downloaded
ODIR=/root
ofile=$ODIR/service-names-port-numbers.xml

if [[ ! -f "$ofile" ]]
then
wget http://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xml -O "$ofile"
fi
which xmlstarlet >/dev/null 2>&1
if [[ $? == 0 ]]
then
echo "xmlstarlet is installed"
else
apt-get install xmlstarlet
fi

#### xmlstarlet el -u service-names-port-numbers.xml
## registry/record - protocol and number
proto=${2:-tcp}
port=${1:-21}
(echo '';sed '1,4d' $ofile) |xmlstarlet sel -t -m "//record[protocol='$proto'][number=$port]" -o "Number(Protocol): " -v number -o '(' -v protocol -o ')' -n -o "Description :" -v description -n

bash ansi codes to html for html reports from shell script

How many times you have felt that there was some simple way to convert bash ansi escape sequences ( colors as well in terminal ) to html equivalent so that you can send the same as html report in email. Here is simple solution. One way is to use ccze if you are using this for logs – colorize your logs and for more generic solution.

 

dnf install python3-ansi2html.noarch

after this is installed, you can use something like this:

/usr/bin/python3-ansi2html -l -f 12 -i

You can see the help for ansi2html to see what parameters mean and which ones suit you best 🙂

ansible with docker dynamic inventory

So, I have a few dockers. Every now and then I want to run some command on all of them. Doing ‘docker exec’ is tiresome. I found this neat solution with ansible that I thought I should share with you.

To get started, you need to have the “docker.py” script. This script will be used as python script inventory for ansible. So, use the following command and get the script:

curl 'https://raw.githubusercontent.com/ansible/ansible/devel/contrib/inventory/docker.py' -o docker.p

Once done, check that all is well, with :

python docker.py

You should not get any errors or warnings and see a list of dockers running on your host. If you get error for docker-py then you can install the same with :

pip install docker-py

And now is the good time to open the docker.py script to check the documentation. It adds all the docker hosts as entry in itself. But cool  thing I liked is the fact that it creates a group for all running dockers – which is very very useful.

 

Now, if I want to set the Timezone (TZ) on all the running dockers in one go, I can do this:

ansible -i docker.py -m shell -a 'rm -f /etc/localtime; ln -s /usr/share/zoneinfo/Asia/Kolkata /etc/localtime' running

And just in case, the above does not work for you and you have a common username/password on all the machines, then another thing you would love is following:

# Export the docker host. If that is localhost, then you do not need this.
export DOCKER_HOST=tcp://192.168.122.1:4243
# This is GOOD to set option. This is the default IP address for docker
# and setting this to first IP address of the docker network is good idea
# that would ensure that ansible can login to each docker.
# And note - you need to have ssh running on all the dockers for the commands to work
export DOCKER_DEFAULT_IP=192.168.122.1
ansible -i docker.py -m setup -u root  -a 'filter=ansible_eth[0-2]'  all