Community discussions

MikroTik App
 
helipos
Member Candidate
Member Candidate
Topic Author
Posts: 132
Joined: Sat Jun 25, 2016 11:32 am

Reading POE status with script

Mon Apr 22, 2019 5:22 am

Hello,

Normally I can bash my way through a script but this one has me stumped.

I'm trying to modify my autoupdate script so it can detect if there are POE powered devices.
If it supplies POE I'll make it delay its own reboot by some minutes to ensure the fed device can install it's firmware without the supplying device rebooting and cutting power off half way through.

What I'm struggling with is how can I can detect if there are POE devices.
Generally the supplying devices are 951Ui-2nD's

I was thinking to use something like this
:global FWstatus [/system package update get status];
but i can't find an equivalent for POE status. I can get the information using
/interface ethernet poe monitor 0
[admin@Work Router] > /interface ethernet poe monitor
numbers: 0
            name: ether5
         poe-out: auto-on
  poe-out-status: waiting-for-load
but I have to get that and still get it in to a variable.

If anyone can provide any assistance that would be greatly appreciated.
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3291
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: Reading POE status with script

Mon Apr 22, 2019 9:47 am

Try this:
[
:local variable ([/interface ethernet poe monitor]->"poe-out")
:put $variable 
]
 
helipos
Member Candidate
Member Candidate
Topic Author
Posts: 132
Joined: Sat Jun 25, 2016 11:32 am

Re: Reading POE status with script

Mon Apr 22, 2019 1:42 pm

[
:global test ([/interface ethernet poe monitor];0;->"poe-out";0)
:put $test
]
0
Q
The zero is required to tell the monitor command which port to look at.
The Q tells it to quit what looks like a live monitor session.
If I don't put the 0 and Q in it doesn't create the "test" variable.

So I tried that in a script.
Unfortunately it doesn't write anything to the 'test' variable.
If I can read the code correctly I guess you wanted to run "/interface ethernet poe monitor" but filter the result for "poe-out" and enter that in to the test variable?
I think the issue lies in the fact the monitor command needs extra entries 0 and Q to operate correctly.
 
nostromog
Member Candidate
Member Candidate
Posts: 226
Joined: Wed Jul 18, 2018 3:39 pm

Re: Reading POE status with script

Mon Apr 22, 2019 2:06 pm

This works for me:
{
  :local test ([/interface ethernet poe monitor ether5 once as-value ]->"poe-out");
  :put $test
}
It needs once to ensure it finishes, and as-value to return the resulting data structure.
 
helipos
Member Candidate
Member Candidate
Topic Author
Posts: 132
Joined: Sat Jun 25, 2016 11:32 am

Re: Reading POE status with script

Thu Apr 25, 2019 12:01 pm

Ok, I'm not sure where my retard skill set let me down, but its working. I'll trying it in the new autoupdate script and when I'm happy with it I'll post it back up here.
 
helipos
Member Candidate
Member Candidate
Topic Author
Posts: 132
Joined: Sat Jun 25, 2016 11:32 am

Re: Reading POE status with script

Sun Jul 14, 2019 7:37 am

Right, after waiting ages for an update to come out from Mikrotik the script ran and failed.
So I found some inspiration https://wiki.mikrotik.com/wiki/Manual:S ... ime_errors and modified it.to fit in my script.

:do {:global POEstatus ([/interface ethernet poe monitor ether5 once as-value ]->"poe-out-status");} on-error={:global POEstatus "fail"}
That will work and if POE capability doesn't exist in the unit, instead of aborting the script it will inject a "fail" message.

The auto update script is here.
viewtopic.php?f=8&t=122839
 
snarkus
just joined
Posts: 1
Joined: Mon Dec 07, 2020 10:04 am

Re: Reading POE status with script

Mon Dec 07, 2020 10:51 am

Last year we have installed about 1000 swithes as managed PoE sources to wireless Aps.

As you noted, PoE related commands are not very useful or informational.
Winbox UI is more frendly, until you need to power cycle all connected devices.

So, i made this code.

It defines 2 global function.
So you can place it into scheduler to load on device reload, and next execute directly from cli:
/system scheduler add name=POE on-event="import flash/poe_tools_v1.rsc" policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon start-time=startup
content of .rsc script:
:if ([system routerboard get model]~"CRS328") do={
	:global showPoeST do={
		:put ("Interface\tPoE status ");
		for i from 1 to 24 step 1 do { 
			:local ethpoest ([/interface ethernet poe monitor [find name=("ether" . $i)] once as-value ]->"poe-out-status"); 
			:set ("$test"->("ether" . $i)) $ethpoest ; 
			:put ("ether" . $i . "\t\t" . $ethpoest)   
			}}
	:global poeRELOAD do={
		for i from 1 to 24 step 1 do 	{
			:local ethpoest ([/interface ethernet poe monitor [find name=("ether" . $i)] once as-value ]->"poe-out-status"); 
			if ($ethpoest="powered-on") do=	{
				/interface ethernet poe power-cycle [find name=("ether" . $i)]  duration=30s;
				:put ("Power-reset on interface ether" . $i) }}}
} else={ :if ([system routerboard get model]~"CRS112") do={

			:global showPoeST do={
			:put ("Interface\tPoE status ");
				for i from 1 to 8 step 1 do { 
					:local ethpoest ([/interface ethernet poe monitor [find name=("ether" . $i)] once as-value ]->"poe-out-status"); 
					:set ("$test"->("ether" . $i)) $ethpoest ; 
					:put ("ether" . $i . "\t\t" . $ethpoest)   
					}}

			:global poeRELOAD do={
				for i from 1 to 8 step 1 do 	{
					:local ethpoest ([/interface ethernet poe monitor [find name=("ether" . $i)] once as-value ]->"poe-out-status"); 
					if ($ethpoest="powered-on") do=	{
						/interface ethernet poe power-cycle [find name=("ether" . $i)]  duration=30s;
						:put ("Power-reset on interface ether" . $i) }
						}}
}}
I`ve tried to get interface count directly from cli,
unfortunately "for" cycle, defined by some variables can`t be imported into environment variable (but works perfectly if you execute code directly).
So I have to make model check and define static number of interfaces for each one.


finally, you get something like:
> $showPoeST 
Interface       PoE status 
ether1          powered-on
ether2          powered-on
ether3          powered-on
ether4          powered-on
ether5          powered-on
ether6          powered-on
ether7          powered-on
ether8          powered-on
ether9          powered-on
ether10         powered-on
ether11         powered-on
ether12         powered-on
ether13         powered-on
ether14         powered-on
ether15         powered-on
ether16         powered-on
ether17         powered-on
ether18         powered-on
ether19         waiting-for-load
ether20         powered-on
ether21         powered-on
ether22         powered-on
ether23         waiting-for-load
ether24         short-circuit
Hope my experience would be helpful)
Last edited by snarkus on Mon Dec 07, 2020 1:56 pm, edited 1 time in total.
 
User avatar
eworm
Forum Guru
Forum Guru
Posts: 1070
Joined: Wed Oct 22, 2014 9:23 am
Location: Oberhausen, Germany
Contact:

Re: Reading POE status with script

Sat Dec 19, 2020 12:38 am

This looks over complicated... How about this:
:global showPoeST do={
  :foreach Interface in=[ / interface ethernet poe find ] do={
    :local IntVal [ / interface ethernet poe monitor $Interface as-value once ];
    :put ($IntVal->"name" . " -> " . $IntVal->"poe-out-status");
  }
}
... and...
:global poeRELOAD do={
  :foreach Interface in=[ / interface ethernet poe find ] do={
    :local IntVal [ / interface ethernet poe monitor $Interface as-value once ];
    :if ($IntVal->"poe-out-status" = "powered-on") do={
      / interface ethernet poe power-cycle $Interface duration=30s;
      :put ("Power-reset on interface " . $IntVal->"name");
    }
  }
}

Who is online

Users browsing this forum: No registered users and 19 guests