This commit is contained in:
Eliezer Croitoru 2024-11-19 18:26:20 +02:00
commit 16aad23d12
5 changed files with 113 additions and 0 deletions

3
Makefile Normal file
View File

@ -0,0 +1,3 @@
all:
echo OK

59
README.md Normal file
View File

@ -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

9
ping-gre-tunnel.service Normal file
View File

@ -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

23
ping-host.sh Executable file
View File

@ -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

19
setup-systemd-service.sh Executable file
View File

@ -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