Skip to content

Instantly share code, notes, and snippets.

@rxerium
Created February 26, 2023 16:43
Show Gist options
  • Save rxerium/e247216178a87788d100849f5b9e8fce to your computer and use it in GitHub Desktop.
Save rxerium/e247216178a87788d100849f5b9e8fce to your computer and use it in GitHub Desktop.
Ping Hosts on the network and output to a file
# ----------
#!/bin/bash
# Pings all devices on the .30 ranges and outputs all active hosts to the online_hosts.txt file
# Useful when creating hosts files with Ansible playbooks
# ----------
for i in {1..254}
do
ping -c 1 10.0.30.$i > /dev/null 2>&1
if [ $? -eq 0 ]; then
hostname=$(nslookup 10.0.30.$i | grep "name =" | awk '{print $4}')
echo "10.0.30.$i $hostname" >> online_hosts.txt
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment