Skip to content

For when a script is willing to wait for listening service ports

License

Notifications You must be signed in to change notification settings

network-utilities/await-service-port

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 

Repository files navigation

Await Service Port

await-service-port.sh, contains a function that returns listening ports for a given service, even if it has to wait a few seconds, and even if the listening ports are not known in advance; such as in the case with librespot service.

The following covers how to install this branch as a submodule within your own project, and parameters that await-service-port.sh currently responds to.

Byte size of await-service-port.sh Open Issues Open Pull Requests Build Test Status Latest commits


Table of Contents


Quick Start

Bash Variables

_module_https_url='https://github.com/network-utilities/await-service-port.git'
_module_relative_path='modules/await-service-port'

Git Commands

cd "<your-git-project-path>"

git checkout gh-pages
git submodule add -b master --name await-service-port "${_module_https_url}" "${_module_relative_path}"

git submodule update
cd "${_module_relative_path}"
git checkout master
git pull

Version Locking; recommended for those that audit every dependency...

git checkout tags/<tag_name> -b <branch_name>

... replace <tag_name> with the tag to checkout and <branch_name> with a custom name, eg...

git checkout tags/v0.0.1 -b loc-v0.0.1

Edit Your ReadMe File

Suggested additions so everyone has a good time with submodules

Clone with the following to avoid incomplete downloads


    git clone --recurse-submodules <url-for-your-project>


Update/upgrade submodules via


    git submodule update --init --recursive
    git submodule update --merge

Example Usage

Example of sourcing and utilize await_service_port features

example_usage.sh

#!/usr/bin/env bash


## Find true directory this script resides in
__SOURCE__="${BASH_SOURCE[0]}"
while [[ -h "${__SOURCE__}" ]]; do
    __SOURCE__="$(find "${__SOURCE__}" -type l -ls | sed -n 's@^.* -> \(.*\)@\1@p')"
done
__DIR__="$(cd -P "$(dirname "${__SOURCE__}")" && pwd)"


## Source module code within this script
source "${__DIR__}/modules/await-service-port/await-service-port.sh"


#
# Two examples of waiting for SSHD listening ports
#
_sshd_ports_list=($(await_service_port 'sshd' 'tcp'))
if [ "${#_sshd_ports_list[@]}" -gt '0' ]; then
    printf 'SSHD Port: %s\n' "${_sshd_ports_list[@]}"
else
    printf 'No ports found this time\n' >&2
fi

_sshd_ports_string="$(await_service_port 'sshd' 'tcp')"
if [ -n "${_sshd_ports_string}" ]; then
    printf 'SSHD Port(s): %s\n' "${_sshd_ports_string[@]}"
else
    printf 'No ports found this time\n' >&2
fi

Test that things work!

Commit and Push

git add .gitmodules
git add modules/await-service-port
git add README.md


git commit -F- <<'EOF'
:heavy_plus_sign: Adds network-utilities/await-service-port dependency


**Edits**


- `README.md` file, documentation updates for submodules


**Additions**

- `.gitmodules` file, tracks other Git repository code utilized by this project

- `modules/await-service-port` submodule, Git tracked dependency
EOF


git push origin master

🎉 Excellent 🎉 your project is now ready to begin unitizing code from this one!


Await Service Port API

Lists listening ports for given service and protocol via netstat

Param Type Description
$1 string required Name of service to grep for
$2 string `<tcp udp>`
$3 number 1 Number of seconds to sleep between checks
$4 number 10 Max number of loops before function fails silently

Returns: <int|list>, number or space separated list of numbers

Throws Parameter_Error: no service name supplied to await_service_port, when first parameter is not defined

Example: as an array

_sshd_ports_list=($(await_service_port 'sshd'))

printf 'SSHD listening port: %s\n' "${_sshd_ports_list[@]}"
#> SSHD Port: 22
#> SSHD Port: 2222

Example: as a string

_sshd_ports_string="$(await_service_port 'sshd')"

printf 'SSHD listening port(s): %s\n' "${_sshd_ports_string}"
#> SSH Ports: 22 2222

Notes

Opening new Issues is supper! However, to avoid attention fragmentation be certain to search for related Issues that could be added to instead. Pull Requests are excellent! However, please clone to a separate directory, then checkout the example branch which tracks the master branch as a submodule, and prior to issuing a Pull Request check the Community for any relevant updates.


License

Await Service Port submodule quick start documentation
Copyright (C) 2019  S0AndS0

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation; version 3 of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.
by `jesin`
on

About

For when a script is willing to wait for listening service ports

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Sponsor this project

Languages