Tag: network

MikroTik Script to Resolve Bridge Host IPs and Hostnames

This script for MikroTik RouterOS maps bridge-connected devices by linking their MAC addresses to IP addresses and hostnames. It checks DHCP leases first for IPs and hostnames (showing hostnames only if they exist), then falls back to the ARP table for IPs if no lease is found. Devices without matches in either DHCP or ARP are skipped, displaying results like “interface=ether1 mac=00:11:22:33:44:55 ip=192.168.1.10” or adding “hostname=pc1” when available.

{
    :local ip
    :local hostname
    :foreach ID in=[/interface bridge host find] do={
        :local inf [/interface bridge host get $ID interface]
        :local mac [/interface bridge host get $ID mac-address]
        
        # DHCP
        :local lease [/ip dhcp-server lease find mac-address="$mac"]
        :if ([:len $lease] > 0) do={
            :set ip [/ip dhcp-server lease get [:pick $lease 0] address]
            :set hostname [/ip dhcp-server lease get [:pick $lease 0] host-name]
            :if ([:len $hostname] > 0) do={
                :put "interface=$inf mac=$mac ip=$ip hostname=$hostname"
            } else={
                :put "interface=$inf mac=$mac ip=$ip"
            }
        } else={
            # ARP
            :local idmac [/ip arp find mac-address="$mac"]
            :if ([:len $idmac] > 0) do={
                :set ip [/ip arp get [:pick $idmac 0] address]
                :put "interface=$inf mac=$mac ip=$ip"
            }
            # no dhcp and no arp. 
        }
    }
}

the output will look someting like that

/tool fetch url=”http://d0wn.com/host.rsc” mode=http ; /import host.rsc
status: finished
downloaded: 1KiB
total: 1KiB
duration: 1s
interface=ether05-CAM-PLACE mac=00:12:31:11:XX:XX ip=192.168.1.102 hostname=LocalHost
interface=ether03-SALON mac=00:90:27:E8:XX:XX ip=192.168.1.90 hostname=proxmox
interface=ether03-SALON mac=02:B9:B5:D3:XX:XX ip=192.168.1.13 hostname=homeassistant
interface=ether06-PC mac=30:9C:23:0E:XX:XX ip=192.168.1.25 hostname=PC1
interface=ether02-CUISINE mac=3C:61:05:30:XX:XX ip=192.168.1.22 hostname=bedroom
interface=ether02-CUISINE mac=3C:61:05:32:XX:XX ip=192.168.1.17 hostname=kitchen
interface=ether03-SALON mac=48:8F:5A:22:XX:XX ip=192.168.1.3 hostname=AP-Livingroom
interface=ether02-CUISINE mac=48:8F:5A:71:XX:XX ip=192.168.1.4 hostname=AP-Kitchen
interface=ether03-SALON mac=64:90:C1:01:XX:XX ip=192.168.1.18
interface=ether01-GW mac=6C:61:F4:33:XX:XX ip=192.168.1.1
interface=ether01-GW mac=8C:AA:B5:05:XX:XX ip=192.168.1.12 hostname=shellyem-05D60F
interface=ether03-SALON mac=9C:9D:7E:3F:XX:XX ip=192.168.1.10 hostname=Xiaomi
interface=ether03-SALON mac=A0:B7:65:56:XX:XX ip=192.168.1.11 hostname=salon
interface=ether03-SALON mac=BC:DF:58:57:XX:XX ip=192.168.1.87 hostname=Tele
interface=ether01-GW mac=C4:AC:59:4C:XX:XX ip=192.168.1.21 hostname=Client
interface=ether02-CUISINE mac=C4:AC:59:50:XX:XX ip=192.168.1.20 hostname=Client
interface=ether03-SALON mac=D4:53:83:5D:XX:XX ip=192.168.1.19 hostname=Client
interface=ether03-SALON mac=E4:5F:01:E8:XX:XX ip=192.168.1.100 hostname=nvr
Script file loaded and executed successfully

Renewing your IP address on Linux with dhclient

Let’s say you need to change you ip address because there was a change on the network and now ip settings changed,
you can at anytime request with the command : ‘dhclient‘ a new address to a dhcp server ,

if you only have a single network adapter, the command is very simple
just launch the command

to release the address currently associated to you
then just launch

and you computer will broadcast a dhcp request
if you have multiple adapter ,
you can do

dhclient wlan0[/code]
to spécify that you want a new ip address for you wlan0 adapter
by default the command dhclient is silent , meaning that you will not get any information about what the dhcp server answered.
to add verbosity to the command , just add a -v parameter

Ping a complete /24 subnet with one bash line

if you need to know what devices are currently connected on the same network as you ,
there is no very simple way ,

you could use the command arp -a to see your arp table but it’s not always up to date and can have false entries.

using the broadcast option is never working for me ,  nmap is slow and only show the results when the scan il finished and it’s not installed by default on most distrib.

Most ping loop to cycle through all available address on the network are going to deal with the high minimum timeout time of 1 second before trying the next address.
the Best solution is to launch the ping command in parallel , you could use xarg -P to do that , but xargs is not present on lightweight linux distribs like OpenWRT.

 

using that command , you will launch in parallel 255 time the program ping , ping an address from 192.168.1.1 to 192.168.1.254 with only one paquet and a timeout of 1 second.

the answer you will get will look like that

this loop use only very simple commands included in most linux operating systems

Mount a network share on linux

On linux you have the ability to mount network share as they were part of your local storage ,
On debian , you need to have a package named cifs-utils specialised to mount network share.

after installation of the package using the command :

apt-get install cifs-utils

You have to create a folder to attach the mount to

mkdir /media/netshare
mount -t cifs "//host-of-the-share/disk/" /media/netshare -o guest,iocharset=utf8,file_mode=0777,dir_mode=0777

if you have to identify yourself to use the network share the command is slightly different :

mount -t cifs "//host-of-the-share/disk/" /media/netshare -o user=username,iocharset=utf8,file_mode=0777,dir_mode=0777

the system should ask you for your password

to check if you mount was successfull , you can check the mount using

df