#!/usr/bin/perl # # svc This script pulls out info from /etc/services for # specified port numbers. # # usage: # svc [-... | -help | -Version] ... # # Revision History # 1.0 Initial revision. 181011 # # Written by Wayne Morrison, 181011. # # Copyright 2018 Wayne Morrison # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # use strict; use Getopt::Long qw(:config no_ignore_case_always); # # Version information. # my $NAME = "svc"; my $VERS = "$NAME version: 1.0"; ############################################################################ # # Options fields. # my %opts = (); # Options. # # Command line arguments. # my @opts = ( 'help', # Give a help message. 'Version', # Display the program version. ); my $svcfile = '/etc/services'; # Services file. ############################################################################ main(); exit(0); #----------------------------------------------------------------------------- # Routine: main() # sub main { $| = 1; # # Munch on the options and arguments. # optsandargs(); # # Check for the named services. # foreach my $svc (sort(@ARGV)) { svcchk($svc); } } #---------------------------------------------------------------------- # Routine: optsandargs() # # Purpose: Parse the command line for options and arguments. # sub optsandargs { # # Parse the options. # GetOptions(\%opts,@opts) || usage(); # # Check for some immediate-action options. # usage() if(defined($opts{'help'})); version() if(defined($opts{'Version'})); usage() if(@ARGV == 0); } #---------------------------------------------------------------------- # Routine: svcchk() # # Purpose: Search /etc/services for specified port numbers. # # This is fairly inefficient if multiple services are # to be checked in a single execution. # sub svcchk { my $unknown = shift; # Unknown service. my @services; # Lines from services file. # # Get the contents of the services file. # open(SVCS, "< $svcfile"); @services = ; close(SVCS); # # Check each line for a matching port number. # foreach my $svc (@services) { my @atoms; # Pieces of service line. my $line; # Copy of service line. # # Skip comments and blank lines. # next if($svc =~ /^\s*#/); next if($svc =~ /^\s*$/); # # Copy the service line and shrink consecutive whitespace # to a single blank. # $line = $svc; $line =~ s/\s+/ /g; # # Divide the line into its elements. # @atoms = split / /, $line; # # Skip this line if the port number doesn't match # the requested port number. # $atoms[1] =~ /(\d+)\//; next if($unknown != $1); print "$svc"; } } #---------------------------------------------------------------------- # Routine: version() # # Purpose: Print the version number(s) and exit. # sub version { print STDERR "$VERS\n"; exit(0); } #---------------------------------------------------------------------- # Routine: usage() # # Purpose: Give usage message and exit. # sub usage { print STDERR "usage: svc [options] ... \n"; print STDERR "\n"; print STDERR "\twhere [options] are:\n"; print STDERR "\t\t-help\n"; print STDERR "\t\t-Version\n"; exit(0); } 1; ############################################################################## =pod =head1 NAME B - display selected entries from B =head1 SYNOPSIS svc [-... | -help | -Version] ... =head1 DESCRIPTION B pulls out info from B for specified port numbers. The service numbers are looked up in the file and lines with matching service numbers are displayed. This B matches on port numbers, not on any other part of the entries. Note: This functionality can be accomplished with somewhat more complicated shell commands. This script just makes it easy. =head1 OPTIONS B takes the following options: =over 4 =item I<-Version> Display the version information for B. =item I<-help> Display a help message. =back =head1 COPYRIGHT Copyright 2018 Wayne Morrison. All rights reserved. =head1 SEE ALSO B =head1 AUTHOR Wayne Morrison, wayne@waynemorrison.com =head1 LICENSE Copyright 2018 Wayne Morrison Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. =cut