Find out IP addresses from MACs in a Local Area Network
Script for Linux to map all devices with their assigned IP in a LAN
Overview
This is a guide to know which IP address is assigned to which computer based in a mapping of device-MAC addresses.
In this way we can know which IP has been assigned by a DHCP server in a LAN, if we don’t have access to the DHCP server but we know the address of each device that can be connected.
1. Map IP and MACs
We need to have a list of DEVICE NAME - MAC like a comma separated
values file ~/map-dev-mac.csv
with three columns:
- custom_dev_id: our assigned name to the device
- mac: MAC address of the device
- dev_name: the name set by the device itself
For example:
custom_dev_id,mac,dev_name
router,25:vj:b2:gk:9d:2f,tp-link
main-server,00:12:b2:d9:10:5a,zeus
mike-phone,54:ba:91:3d:d1:0b,REDMIphone
2. Install scanner
Install the scanner: arp-scan
which will be used in the script.
sudo apt install arp-scan
2. Script
Now we use the following bash script lan-ips.sh
:
#!/bin/bash
LOCAL_IP_MACS=$(sudo arp-scan --plain 192.168.1.0/24)
while read -r LINE; do
# skip comments
DEV_NAME=$(echo $LINE | cut -d',' -f1 -s)
DEV_MAC=$(echo $LINE | cut -d',' -f2 -s)
#if mac not empty
if [ -n "$DEV_MAC" ]; then
DEV_IP=`echo "$LOCAL_IP_MACS" | grep $DEV_MAC | cut -f1 # --zero-terminated `
echo -e "$DEV_IP \t $DEV_NAME "
fi
done < ~/map-dev-mac.csv
And make it executable: chmod +x lan-ips.sh
2.1 Note
Executing the script will look for all the devices connected in a LAN,
typically 192.168.1.0/24
, and then look each device in our text
file, generating a list of IP-DEVICE_NAME
3. Example output
After executing the script:
$ lan-ips.sh
[sudo] password for user:
custom_dev_id
192.168.1.1 router
192.168.1.27 main-server
192.168.1.26 mike-phone
Conclusion
This is a simple way to access other devices when not having access to a DHCP server, it would need to be run often as the DHCP may assign new IPs to the devices connected in a LAN.
Feel free to propose any improvements to the script.
- Find out IP addresses from MACs in a Local Area Network
- Choose any key as the modifier in i3wm in 6 stepsJanuary 20, 2021
- Adding a swap memory to Linux from command line in 6 stepsApril 2, 2020
- Free up space in Linux (Ubuntu)March 27, 2020
- Switch between languages in Linux. Write in multiple languages with same keyboard.March 21, 2020
- How to make Ubuntu display emojisFebruary 12, 2020
- Detect and mount USB devices in Linux from consoleJanuary 24, 2019
- How to make screencasts in Ubuntu LinuxJanuary 21, 2019
- Using i3 window manager in LinuxJanuary 7, 2019
- Setting Up A Fresh Linux ServerAugust 25, 2018
- How To Download A Website With Wget The Right WayJune 30, 2017
- Replicate Installed Package Selections From One Ubuntu System To AnotherApril 24, 2017
- Using Clamav Antivirus In UbuntuJanuary 25, 2017
- How to Type Spanish Characters, Accents and Symbols in LinuxJune 6, 2016
Ubuntu
- How to activate tap to click touchpad's feature in Ubuntu in 4 stepsMarch 4, 2021
- Difference between suspend and hibernate in Ubuntu and how to execute them from command lineApril 12, 2020
- Solving Google Chrome's gpu-process error message in Ubuntu LinuxJanuary 7, 2019
- Solving Google Chrome's secret service operation error message in Ubuntu LinuxJanuary 7, 2019
- Start Emacs In Ubuntu The Right WayJune 10, 2017
Unix Shell
- Connect to a Bluetooth device from command line in Ubuntu LinuxJune 23, 2020
- Add Infolinks Script To An Existing Website From Console With Sed CommandApril 4, 2017
- How to change all files permissions to 644 and directories to 755January 10, 2017
- Shell Redirect Output And Errors To The Null Device In BashDecember 9, 2016
- Prevent Running Of Duplicate Cron JobsDecember 8, 2016
- Delete All Backup Files Recursively In BashNovember 28, 2016
- Bash Script to Find Out If MySQL Is Running Or NotNovember 9, 2016
Articles
Subcategories
Except as otherwise noted, the content of this page is licensed under CC BY-NC-ND 4.0 . Terms and Policy.
Powered by SimpleIT Hugo Theme
·