Skip to content

Instantly share code, notes, and snippets.

@ssebastianj
Last active May 1, 2019 13:34
Show Gist options
  • Save ssebastianj/234c9cd06a8d21e1a5bc42c68faa7d5b to your computer and use it in GitHub Desktop.
Save ssebastianj/234c9cd06a8d21e1a5bc42c68faa7d5b to your computer and use it in GitHub Desktop.
Run ping, netcat, host (or any other) commands on multiple hosts or IPs or both. Also run many procresses at a time!
#!/usr/bin/env bash
set -euo pipefail
# In some commands, I've used the full parameter names just for the sake of learning/debugging/memorizing.
# From $ man xargs:
# --max-procs
# Run up to max-procs processes at a time; the default is 1.
# If max-procs is 0, xargs will run as many processes as possible at a time.
if [[ ! -r hosts.txt ]]; then
echo 'No input "hosts.txt" file found'
exit 1
fi
echo 'TEST: ping hosts'
xargs --arg-file=hosts.txt -L 1 --max-procs=1 --no-run-if-empty --verbose -- ping -q -c1
echo 'TEST: run DNS lookup'
xargs --arg-file=hosts.txt -L 1 --max-procs=1 --no-run-if-empty --verbose -- host
echo 'TEST: check if TCP port (443) is listening for connections'
xargs --arg-file=hosts.txt -L 1 --max-procs=1 --no-run-if-empty --verbose -I{} -- /bin/nc -zv {} 443
8.8.8.8
8.8.4.4
google.com
github.com
1.1.1.1
1.0.0.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment