Sleeping Cyborg

Jonathan David Page talks about whatever he happens to be thinking about. Sometimes other people join in.

Email · @parathetic (Twitter) · @jdpage (Github)
Subscribe to feed


A collection of cool people and projects.

Bash code for finding active IP addresses

by on 10 May 2011
in , ,
with some comments, maybe.

I sometimes have to ssh into my brother's laptop. This can be a painful procedure for all involved, because I need to know the IP address, but he can never remember the command to get his computer to tell him. I normally solve this by just pinging everything from to or so, and then attempting to ssh into any that respond, but the other day I got slightly fed up with this.

So without further ado, I give you:

for i in {100..120}
do ping -c 1 192.168.1.$i | \
    grep -B 1 ' 0% packet loss' | \
    sed 's/^.*\(192\.168\.1\....\).*$/\1/g;/192/!d'

This "simple" bash code just loops through the address range given on the first line, pings each one, uses grep to find ones that responded, and then sed to format nicely. I could probably do it all in sed, but hey. If you think of any improvements message me and I'll add them.