Menu

(Solved) : Write Script Named Findmacsh Help Find Mac Address Devices Lan Given Ip Addresses Note Ll Q37250474 . . .

write a script named find_mac.sh that will help youfind the MAC address of devices in your LAN given their IPaddresses. Note that we’ll cover all of the relevant tools in thelab on networking. To do this, you’ll need to use both theping and ip n commands. The basic idea is to pingthe IP address and then look in the ARP cache on your system tofind the MAC address. This only works for devices in your LANbecause ARP and MAC addresses are only meaningful inside the LAN.To do this properly will require some research on your own andquite a bit of error checking code in the script. The actualping and ip n steps are fairly straight forward.Here’s an outline to help get you started:

  1. First, use ping and ip n by hand to convinceyourself how to do this. Look at the output of ip nspecifically since you’ll need to parse it later.
  2. You only need to send a single ping, so use the –cargument to ping.
  3. You don’t need to look at the output of ping, soredirect it to /dev/null.
  4. Since we’re using IP addresses (not hostnames), you’ll want tolook for IP addresses in the ARP cache list.
  5. If everything works, you’ll see an entry in the ARP cachemapping the IP address to a MAC address. Use awk to getthe MAC address out of the line.

Start by just getting the ping and ip ncommands right. Then you’ll need to add a bunch of error checking.Here’s how I would proceed (testing at every step like always):

  1. Make sure there is only one argument.
  2. Make sure that the only argument is in fact an IP address. Usegrep with a regular expression to test if the argument isactually an IPv4 address or not.
  3. Next, if you try to ping an address that’s not up, the ARPcache will contain a line for the IP address that contains thestring “FAILED“. Give one a try by hand. You need to adderror checking that looks for that string for the desired IPaddress and tells the user that the MAC address can’t currently befound.
  4. Finally, this method only works if the IP address is in thesame LAN. You should verify that before doing any of theping/ip n stuff. You can hard code in the LANprefix of 10.0.2. for the VirtualBox LAN. If the givenaddress is not in the LAN, then tell the user that and exit.

Here are some example runs:

[user@user eclab]$ ./find_mac.sh

usage: ./find_mac.sh IP

[user@user eclab]$ ./find_mac.sh blahblah

usage: ./find_mac.sh IP

[user@user eclab]$ ./find_mac.sh1.2.3

that is not an IP address!

[user@user eclab]$ ./find_mac.sh1.2.3.4

this only works for IPs in the LAN!

[user@user eclab]$ ./find_mac.sh10.0.2.2

52:54:00:12:35:02

[user@user eclab]$ ./find_mac.sh10.0.2.3

52:54:00:12:35:03

[user@user eclab]$ ./find_mac.sh10.0.2.4

52:54:00:12:35:04

[user@user eclab]$ ./find_mac.sh10.0.2.5

10.0.2.5 did not respond to ping

[user@user eclab]$

Expert Answer


Answer to write a script named find_mac.sh that will help you find the MAC address of devices in your LAN given their IP addresses…

OR