Skip to content

Instantly share code, notes, and snippets.

@porteusconf
Last active January 30, 2024 00:54
Show Gist options
  • Save porteusconf/3cfa76fb293ae303a0f34714fcca15df to your computer and use it in GitHub Desktop.
Save porteusconf/3cfa76fb293ae303a0f34714fcca15df to your computer and use it in GitHub Desktop.
Internet connectivity test. Dumb linux or windows shell script that logs to csv file only those pings that fail. Likely ok to leave running for days.
#!/bin/sh
echo "for each ping that fails, adds one line to csv log file with iso-8601 date and host"
echo "Usage: ./pinglog.sh host-to-ping i.e. ./pinglog.sh google.com "
# to do: if continually fails, don't log each failure, just start and stop of failures.
# i guess something like if previous fail was less than X secs from current, don't log.
while true ;
do
# change -I arg from wlan1 to whatever interface you want to use
ping -c1 -I wlan1 $1 > /dev/null
### ping -n 1 $1 > /dev/null ### for windows with gitbash (see comment below)
if [ $? -eq 0 ]
then
echo -n ""
else
echo $(date --iso-8601=ns) " , ping-$1-failed" | tee -a pinglog.csv
fi
sleep 5 # change to number of seconds you want between pings
done
@porteusconf
Copy link
Author

porteusconf commented Jan 29, 2024

I am sure there are much much better ways to do this, so please link to them here. For example, the chrome/edge extension https://internetmon.org/ But i just needed something quick and dirty for where user can choose host, and which interface to use (like wifi, wlan1, vs wired eth0).,

@porteusconf
Copy link
Author

WINDOWS... I looked for equivalent script in windows, but all I found were long obtuse hard-to-read powershell scripts that did not work ;-) like this: https://community.spiceworks.com/topic/2216374-powershell-script-to-continuously-ping-hosts-and-log-failures-with-timestamps. Seems far easier just to run the shell script in a git-bash shell on windows, as shown below

student@VITA-655 MINGW64  mkdir ~/pinglog
$ bash --version
GNU bash, version 5.2.21(1)-release (x86_64-pc-msys) ...
$ date
Mon Jan 29 19:26:23 EST 2024
$ mkdir ~/pinglog
$ cd  ~/pinglog
$ nano pinglog.sh  (paste in the above script, and save as  ~pinglog/pinglog.sh (or use Notepad from CMD/ps).
$ chmod a+x pinglog.sh 
$ ./pinglog.sh

Note: options for ping in windows are different, like -n 1 for just one ping (instead of -c 1 for ping in linux).

$ ping --help
Bad option --help.

Usage: ping [-t] [-a] [-n count] [-l size] [-f] [-i TTL] [-v TOS]
            [-r count] [-s count] [[-j host-list] | [-k host-list]]
            [-w timeout] [-R] [-S srcaddr] [-c compartment] [-p]
            [-4] [-6] target_name

Options:
    -t             Ping the specified host until stopped.
                   To see statistics and continue - type Control-Break;
                   To stop - type Control-C.
    -a             Resolve addresses to hostnames.
    -n count       Number of echo requests to send.
    -l size        Send buffer size.
    -f             Set Don't Fragment flag in packet (IPv4-only).
    -i TTL         Time To Live.
    -v TOS         Type Of Service (IPv4-only. This setting has been deprecated
                   and has no effect on the type of service field in the IP
                   Header).
    -r count       Record route for count hops (IPv4-only).
    -s count       Timestamp for count hops (IPv4-only).
    -j host-list   Loose source route along host-list (IPv4-only).
    -k host-list   Strict source route along host-list (IPv4-only).
    -w timeout     Timeout in milliseconds to wait for each reply.
    -R             Use routing header to test reverse route also (IPv6-only).
                   Per RFC 5095 the use of this routing header has been
                   deprecated. Some systems may drop echo requests if
                   this header is used.
    -S srcaddr     Source address to use.
    -c compartment Routing compartment identifier.
    -p             Ping a Hyper-V Network Virtualization provider address.
    -4             Force using IPv4.
    -6             Force using IPv6.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment