search for a port number

2017-09-11 1 min read bash Linux

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
comments powered by Disqus