Community discussions

MikroTik App
 
viteralex
just joined
Topic Author
Posts: 11
Joined: Wed Dec 01, 2021 8:21 pm

get mac address from host name

Wed Jun 29, 2022 5:36 pm

Hi guys. I need to WOL by hostname. Wrote this script:
####################################################################
# tg_cmd_wolByName - WakeOnLan by hostname
#  Input: 
#     $1 — script name (information only)
#     params — hostname to Wake-On-LAN
##########################################################################
:put "Command $1 is executing";
:local hostname $params;
#get mac from dhcp lease
:local mac ([/ip dhcp-server lease print as-value where host-name=$hostname]->0->"mac-address");
:if ([:typeof $mac]="nothing") do={ 
  #no mac in dhcp lease. Try dns static
  :local ip ([/ip dns static print as-value where name=$hostname]->0->"address");
  :set $mac ([/ip arp print as-value where address=$ip]->0->"mac-address")
  :if ([:typeof $mac]="nothing") do={
    :return "<$hostname> is not found neither in dhcp lease no in dns static";
  }
}
:put $mac;
#get interface from arp
:local ifc "bridge";
:local macs [/ip arp print as-value where mac-address=$mac];
:if ([:len $macs]!=0) do={ 
  :set $ifc ($macs->0->"interface");
 }

/tool wol mac=$mac interface=$ifc;
:return "success";
It works but I'm not sure if I predicted every possible mistake.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: get mac address from host name

Wed Jun 29, 2022 6:08 pm

Why you do not use directly the MAC?
Hostname can have also the lease expired, and deleted,
and also the ARP cache can be empty and wake fail.
Instead with MAC, if the device have a working WoL mode, work everytime.
 
viteralex
just joined
Topic Author
Posts: 11
Joined: Wed Dec 01, 2021 8:21 pm

Re: get mac address from host name

Wed Jun 29, 2022 8:16 pm

because this script runs from telegram bot. And client adds PC's with hostnames, not with macs
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: get mac address from host name

Wed Jun 29, 2022 8:24 pm

"this script runs from telegram bot"

Where is writed on first post?
 
viteralex
just joined
Topic Author
Posts: 11
Joined: Wed Dec 01, 2021 8:21 pm

Re: get mac address from host name

Wed Jun 29, 2022 8:43 pm

nowhere cause it doesn't matter.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: get mac address from host name

Wed Jun 29, 2022 8:56 pm

Everything matter, also your last reply, and also this reply.

Who is online

Users browsing this forum: Google [Bot], zandhaas and 16 guests