How to search aviable \up computer in your LAN network


We want to get your lan address and check which computer are online in your own network .

COPY AND PASTE CODE ON TERMINAL

 IFS=. read -a addr <<<"$(ifconfig | mawk '/inet addr:192/{gsub(/inet addr:/,""); print $1}')" ; lanaddress=${addr[0]}.${addr[1]}.${addr[2]}; echo this is your lanaddress $lanaddress ; for ip in $(seq 1 254); do ping -c 1 $lanaddress.$ip>/dev/null; if [[ $? -eq 0 ]]; then echo "192.168.1.$ip UP" ;else echo "$ip DOWN"; fi; done

EXPLANATION

so we need ifconfig that prints everything we need like this

nowardev@kubuntu:~$ ifconfig
eth0      Link encap:Ethernet  HWaddr 00:a0:d1:69:b6:f3
inet addr:192.168.1.5  Bcast:192.168.1.255  Mask:255.255.255.0
inet6 addr: fe80::2a0:d1ff:fe69:b6f3/64 Scope:Link
UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
RX packets:4824955 errors:0 dropped:0 overruns:0 frame:0
TX packets:3418805 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1724098825 (1.7 GB)  TX bytes:468753231 (468.7 MB)

lo        Link encap:Local Loopback
inet addr:127.0.0.1  Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING  MTU:65536  Metric:1
RX packets:82058 errors:0 dropped:0 overruns:0 frame:0
TX packets:82058 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:8380425 (8.3 MB)  TX bytes:8380425 (8.3 MB)

My lan ip is 192.168.1.5

  •  we need to print just  192.168.1 and for that we will use awk
  • we need to loop from 192.168.1.1  to  192.168.1.254 checking if the ip is UP or DOWN

For the first problem  we will use awk to get just the ip from ifconfig’s outout and we will use an  array based on the field separator “.”  we will call it  addr  and so we will print

 IFS=. read -a addr <<<"$(ifconfig | mawk '/inet addr:192/{gsub(/inet addr:/,""); print $1}')" ; lanaddress=${addr[0]}.${addr[1]}.${addr[2]}; echo $lanaddress

that prints correctly
192.168.1

explained :

IFS=. read -a addr <<<

create an array called addr , and use like field separator this “.”  the symbol <<<  feeds read with the outout of ifconfig piped with awk

we are interested to this line from ifconfig’s output :

inet addr:192.168.1.5  Bcast:192.168.1.255  Mask:255.255.255.0

 “$(ifconfig | mawk  ‘/inet addr:192/{gsub(/inet addr:/,””); print $1}’)”

it runs ifconfg  pipes the output to awk that will detect only the line with inet addr:192   and replace inet addr: with nothing after that it prints the first field based on the standard field separator that is a space …

 IFS=. read -a addr <<<"$(ifconfig | mawk '/inet addr:192/{gsub(/inet addr:/,""); print $1}')" ; lanaddress=${addr[0]}.${addr[1]}.${addr[2]}; echo this is your lanaddress $lanaddress ; for ip in $(seq 1 254); do ping -c 1 $lanaddress.$ip>/dev/null; if [[ $? -eq 0 ]]; then echo "192.168.1.$ip UP" ;else echo "$ip DOWN"; fi ; done

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: