#!/bin/sh # # Lookup ports. # # Copyright (c) 2006 Lorance Stinson LoranceStinson AT gmail DOT com #************************************************************************** # Defaults. #************************************************************************** SERVICES="/etc/services" # The file with ports. PROTOCOL="(udp|tcp)" # The default protocol search. #************************************************************************** # usage() - Display the command line help text. #************************************************************************** usage(){ if [ "$1" ] ; then echo "$1" echo "" fi cat< Lookup a port or service in the file $SERVICES and print it's use. By default both TCP and UDP ports are retrieved. Options: -f Use an alternate ports file. -h This text. -t Only list the TCP use. -u Only list the UDP use. EOHELP exit 1 } #************************************************************************** # Check and handle commandline options. #************************************************************************** while [ "$#" -gt 0 ] ; do case "$1" in # Alternate services file. -f) [ "$2" ] && SERVICES="$2" || usage "$1 requires an argument." shift 2;; # Print the help text. -h) usage;; # List only TCP. -t) PROTOCOL="tcp"; shift;; # List only UDP. -u) PROTOCOL="udp"; shift;; # End of options --) [ "$2" ] && SEARCH="$2"; break;; -*) usage "Invalid option $1";; # Anything else should be the search string. *) SEARCH="$1"; shift;; esac done [ "$SEARCH" ] || usage "Specify a port to lookup." # Perform the search. if [ "`expr "$SEARCH" : '[0-9]*$'`" -ne 0 ] ; then egrep "[^0-9]$SEARCH/$PROTOCOL" $SERVICES else egrep "^.*$SEARCH.*[ ]*[0-9]*/$PROTOCOL" $SERVICES fi