commit 16aad23d12530f82e21deb53b8e7ca565fe1783f Author: Eliezer Croitoru Date: Tue Nov 19 18:26:20 2024 +0200 1 diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..1fe68a9 --- /dev/null +++ b/Makefile @@ -0,0 +1,3 @@ +all: + echo OK + diff --git a/README.md b/README.md new file mode 100644 index 0000000..183b3cd --- /dev/null +++ b/README.md @@ -0,0 +1,59 @@ +# Ping-Out +A `bash` script and a `systemd` service that check internet connectivity by pinging `8.8.8.8`. + +The `bash` script `ping-out.sh` pings every 60 seconds to the presumably always available IP address +`8.8.8.8`. If packages may be lost, an error will be logged into a file. + + +## Prerequisites +The script requires `bash`. The service requires also `systemd`. + + +## Usage +Review and adjust the script `ping-out.sh` according to your gusto. E.g. adjust the log file path, +the time interval or the IP adress to which to ping to. + +Start the script like: +```bash +bash ping-out.sh +``` + +You can detach the script from the terminal session and run it in the background: +```bash +nohup ./ping-out.sh & +``` + +### Running `ping-out` as a Service +The script `setup-systemd-service.sh` can set up the service on platforms that support `systemd`, +e.g. `Ubuntu`. +Running `ping-out.sh` as a service allows for autostart and running in the backgorund. + +To install the `systemd` service for `ping-out` on `Ubuntu`, run: +```bash +sudo bash setup-systemd-service.sh +``` + +This copies the script `ping-out.sh` into the directory `/usr/local/bin` and the file +`ping-out.service` into the directories of the `systemd` installation. +On other platforms than `Ubuntu`, you might have to adjust the setup-script. + +After installation, you may control the service via typical `systemd` commands: +```bash +sudo systemctl enable ping-out # run service at system startup +sudo systemctl start ping-out +sudo systemctl stop ping-out +sudo systemctl restart ping-out +sudo systemctl status ping-out +``` + +If you want to make changes to the program, change the script and replace the script file in +`/usr/local/bin/`. + + +## Contributing +Work on your stuff locally, branch, commit and modify to your heart's content. +If there is anything you can extend, fix or improve, please do so! +Happy coding! + + +## TODO diff --git a/ping-gre-tunnel.service b/ping-gre-tunnel.service new file mode 100644 index 0000000..b2b2256 --- /dev/null +++ b/ping-gre-tunnel.service @@ -0,0 +1,9 @@ +[Unit] +Description=Ping a host service + +[Service] +Type=simple +ExecStart=/bin/bash /usr/local/bin/ping-host.sh 192.168.99.2 + +[Install] +WantedBy=multi-user.target diff --git a/ping-host.sh b/ping-host.sh new file mode 100755 index 0000000..364e4aa --- /dev/null +++ b/ping-host.sh @@ -0,0 +1,23 @@ +#!/usr/bin/env bash + +# Ping command line options: +# -c: stop after c answered packages +# -i: seconds wait interval +# -q quiet +# -w: seconds overall deadline + +DESTINATION="$1" +SECONDS="2" +RATE_IN_SECONDS="2" +NUMBER_OF_PINGS="1" +INTERVAL="1" + +while sleep ${RATE_IN_SECONDS}; do + ping_output="$(ping -c ${NUMBER_OF_PINGS} -i ${INTERVAL} -q -w ${SECONDS} ${DESTINATION} 2>/dev/null)" + PING_EXIT_CODE="$?" + if [ "${PING_EXIT_CODE}" -ne 0 ]; then + ping_results="$(grep 'packets transmitted.*received.*packet loss.*time' <<< "$ping_output")" + now="$(date '+%s') $(date)" + printf "%s %s %s\n" "$now" "$ping_exit_code" "$ping_results" + fi +done diff --git a/setup-systemd-service.sh b/setup-systemd-service.sh new file mode 100755 index 0000000..5e798dd --- /dev/null +++ b/setup-systemd-service.sh @@ -0,0 +1,19 @@ +#!/bin/bash +# +# Setup a systemd service that can run the script ping-out.sh. +# +# author: andreasl +set -e + +if ! command -v systemd >/dev/null; then + printf 'Error: systemd not found.\n' + exit 1 +fi + +sudo cp -v 'ping-out.sh' '/usr/local/bin' +sudo cp -v 'ping-out.service' '/lib/systemd/system' + +printf 'systemd service ping-out installed.\n' + +printf 'Running\n systemctl status ping-out...\n' +sudo systemctl status ping-out