Community discussions

MikroTik App
 
ilpastore
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 66
Joined: Sat Mar 24, 2007 1:08 am
Location: Italy

disable/enable ethernet interface script

Tue Oct 20, 2015 11:37 pm

Hi there!

I'd like have to know if there is a script that check if ethernet interface is correctly at the right speed (f.e. 1Gb).
If it was wrong (f.e. 100Mb) shoulda try disable and enable that interface 3 times, if all three times doesn't fix the issue, send an email

SO, i give you an example:

2 RB1100 connect each on ether 1, LAN SPEED 1000Mb. -> EVERYTHING IS OK

NOW

ISSUE (NOT ALWAYS) that LAN at 100MB

TODAY SOLUTION
i manually disable and enable that interface..... (8 on 10 times it fix the problem)

--

BEST SOLUTION: script that check if the LAN is at 1000Mb, if is 100Mb disable and enable interface, for 3 times.... if it doesn't works, send an email


Is it possible?
 
efaden
Forum Guru
Forum Guru
Posts: 1711
Joined: Sat Mar 30, 2013 1:55 am
Location: New York, USA

Re: disable/enable ethernet interface script

Wed Oct 21, 2015 5:18 am

Yeah... entirely possible.
 
angboontiong
Forum Guru
Forum Guru
Posts: 1117
Joined: Fri Jan 16, 2009 9:59 am

Wed Oct 21, 2015 5:20 pm

You can use schedules and script to make it...

Sent from my Che2-L11
 
efaden
Forum Guru
Forum Guru
Posts: 1711
Joined: Sat Mar 30, 2013 1:55 am
Location: New York, USA

Re: disable/enable ethernet interface script

Wed Oct 21, 2015 8:02 pm

Now that I have more time to answer, what I would do is setup a script that has two arrays in it.... One telling the script which ethernet interfaces to check and the second telling the script what speed each interface should be at...

interfaces = { "ether1-gateway", "ether2-local-master", ... etc}
speeds = { "1000", "100", ... etc}

Then loop through the list of interfaces... check the speed that it is supposed to be at... if it is wrong cycle it... until either it is the correct speed OR you cycled it some number of times (say 8 or whatever you want) and give up...

Run the script every 15 minutes or something...

-Eric
 
ilpastore
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 66
Joined: Sat Mar 24, 2007 1:08 am
Location: Italy

Re: disable/enable ethernet interface script

Sat Oct 24, 2015 3:35 pm

this is now.

create the script:

Name check1
/interface ethernet monitor ether8 once do={
:if ($rate = "10Mbps") do={
/interface ethernet set ether8 disabled=yes
:delay 2s
/interface ethernet set ether8 disabled=no
:log info message="check-eth8";
}
}
Duplicate it two times:

Name check2
/interface ethernet monitor ether8 once do={
:if ($rate = "10Mbps") do={
/interface ethernet set ether8 disabled=yes
:delay 2s
/interface ethernet set ether8 disabled=no
:log info message="check-eth8";
}
}
and

Name check3
/interface ethernet monitor ether8 once do={
:if ($rate = "10Mbps") do={
/interface ethernet set ether8 disabled=yes
:delay 2s
/interface ethernet set ether8 disabled=no
:log info message="check-eth8";
}
}
then last chance will send me an email

Name checkNmail

/interface ethernet monitor ether8 once do={
:if ($rate = "10Mbps") do={
/export file=email
/tool e-mail send \
to="myuser@mydomain.com" from=myuser@mydomain.com \
subject=([/system identity get name] . " *** ETH8 at 10Mb") \
 server=1.2.3.4
}
}

create 4 schedule, 5 minutes delay each start with CHECK1, then CHECK2, then CHECK3 and last CHECKNMAIL

--- everything is work!


but what I ask now: how can use only one script instead of 4 scripts separatly? and what can i check EVERY eth speed, instead of one script each?

Thank you so much guys!
 
efaden
Forum Guru
Forum Guru
Posts: 1711
Joined: Sat Mar 30, 2013 1:55 am
Location: New York, USA

Re: disable/enable ethernet interface script

Sat Oct 24, 2015 4:27 pm

Read the post I posted... If I get a chance I can try to piece together a script from stuff I already have written.
 
efaden
Forum Guru
Forum Guru
Posts: 1711
Joined: Sat Mar 30, 2013 1:55 am
Location: New York, USA

Re: disable/enable ethernet interface script

Sat Oct 24, 2015 4:52 pm

Had a spare minute.... THIS IS NOT TESTED.... but should be pretty close....

Basically you set ethernetSpeeds to an array.... where the key is the ethernet name... and the value is the targeted speed.

cycleNumber specifies how many times to try before it gives up, downtime specifies how long to keep the interface down, sleepBetween specifies how long to wait before the script checks again, and sendEmail is a boolean to send an email if the script hits the cycleNumber and is still not at the target speed.
#.* by RouterOS
#
# Ethernet Speed Detector
#

#
# Variables
# Example { "ether1"="100Mbps" ; "ether2"="1000Mbps" }
:local ethernetSpeeds {}

:local cycleNumber 3
:local downtime 2
:local sleepBetween 5
:local sendEmail true

/interface ethernet {
	:foreach ethName,targetSpeed in=$ethernetSpeeds do={
		:local loopCounter 0

		:local currentSpeed [get $ethName speed]

		while (($currentSpeed != $targetSpeed) && ($loopCounter < $cycleNumber) do={
			set $ethName disabled=yes
			:delay $downtime
			set $ethName disabled=no
			:set $loopCounter ($loopCounter + 1)
			:sleep $sleepBetween
			:set currentSpeed [get $ethName speed]
		} 

		:if ($sendEmail && ($currentSpeed != $targetSpeed)) do={
			/tool e-mail send to="myuser@mydomain.com" from=myuser@mydomain.com \
				subject=([/system identity get name] . " *** $ethName at $currentSpeed, Target $targetSpeed") \
 				server=1.2.3.4
		}
	}
}
 
ilpastore
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 66
Joined: Sat Mar 24, 2007 1:08 am
Location: Italy

Re: disable/enable ethernet interface script

Mon Oct 26, 2015 4:36 pm

thx, i will try soon
 
Genkun
newbie
Posts: 33
Joined: Mon Jul 27, 2015 11:32 am

Re: disable/enable ethernet interface script

Tue Feb 02, 2016 11:03 am

Brilliant Script

Took an hour or so to understand how an array works but eventually got it working like a charm.
/system script
add name="Port Rate" policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive comment="Check if Ports are Syncing Correctly" source={
:local "ethernetSpeeds" {"ether2"="100Mbps" ; "ether3"="1000mbps"}

:local cycleNumber 3
:local downtime 2
:local sleepBetween 5
:local sendEmail true

/interface ethernet {
   :foreach ethName,targetSpeed in=$ethernetSpeeds do={
      :local loopCounter 0
      :local currentSpeed [get $ethName speed]
	  
	  :while (($currentSpeed != $targetSpeed) && ($loopCounter < $cycleNumber)) do={
         set $ethName disabled=yes
         :delay $downtime
         set $ethName disabled=no
         :set $loopCounter ($loopCounter + 1)
         :delay $sleepBetween
         :set currentSpeed [get $ethName speed]
      }

      :if ($sendEmail && ($currentSpeed != $targetSpeed)) do={
         /tool e-mail send to="someone@someone.com" from=someone@someone.com \
            subject=([/system identity get name] . " *** $ethName at $currentSpeed, Target $targetSpeed") \
             server=1.2.3.4
      }
   }
}
}

/system scheduler
add name="Port Rate" start-time=startup on-event="Port Rate" interval=00:15:00 comment="Check if Ports are Syncing Correctly"
Just edit the ether names and speeds to match your board and don't forget the e-mail details.
 
efaden
Forum Guru
Forum Guru
Posts: 1711
Joined: Sat Mar 30, 2013 1:55 am
Location: New York, USA

Re: disable/enable ethernet interface script

Wed Feb 03, 2016 10:28 pm

Glad it worked.

-Eric
 
evandromac
just joined
Posts: 12
Joined: Thu Sep 01, 2016 3:19 am

Re: disable/enable ethernet interface script

Thu Sep 01, 2016 5:24 am

Glad it worked.

-Eric
Hi, Efaden!

Your script is a piece of work. Congratulations.

But (and I know it's not an error in your script) here in my testing environment, the speed reading always return "100Mbps", no mater if the interface is at 10Mbps or 1Gbps.

Have you heard something about this ? It appears to be an routerOS FW issue, since there is no errors in your logic. I am using 6.36.2
 
efaden
Forum Guru
Forum Guru
Posts: 1711
Joined: Sat Mar 30, 2013 1:55 am
Location: New York, USA

Re: disable/enable ethernet interface script

Fri Sep 02, 2016 2:29 am

Interesting... I haven't seen anything about that....

run

/interface ethernet { :foreach i in=[find] do={:put [get $i speed]} }

and tell me what you get back....
 
evandromac
just joined
Posts: 12
Joined: Thu Sep 01, 2016 3:19 am

Re: disable/enable ethernet interface script

Fri Sep 02, 2016 6:43 am

Interesting... I haven't seen anything about that....

run

/interface ethernet { :foreach i in=[find] do={:put [get $i speed]} }

and tell me what you get back....
Hi, Efaden!

In RB951G-@2HnD (5 gigabit ports, 4 at 1Gbps, 1 at 100Mbps) it returns five prints of "100Mbps".

On a SXT SA with the same FW version, it returns always "100Mbps", either when the interface is at 10Mbps or even disabled!
 
efaden
Forum Guru
Forum Guru
Posts: 1711
Joined: Sat Mar 30, 2013 1:55 am
Location: New York, USA

Re: disable/enable ethernet interface script

Sat Sep 03, 2016 3:25 pm

Hmmm... can you give me access to the devices?... I just want to see what data I can get off the interfaces. That's really odd though? Email me efaden@gmail and I can try to figure out the right command to pull the data.
 
evandromac
just joined
Posts: 12
Joined: Thu Sep 01, 2016 3:19 am

Re: disable/enable ethernet interface script

Tue Sep 06, 2016 6:08 am

Hmmm... can you give me access to the devices?... I just want to see what data I can get off the interfaces. That's really odd though? Email me efaden@gmail and I can try to figure out the right command to pull the data.
Efaden: sorry for answering just today, but the past few days we were working on a ceragon link and I barely had time to sit near a computer...

About the interface rate, I researched here and there and found a solution, which was use the "/interface ethernet monitor" command.

I worked in your code, added some code by my own, and the result:

1. The script checks if the interface speed matches the target speed for that interface
2. If yes, the script ends;
3. if not, it logs the event and start the reset procedure, logging every attempt;
4. If any attempt succeds, it logs it and stop trying;
4. if all the attempts failed, it also logs it and sends an email

Please, feel free to look into my code and if you have any improvements (I'm not as good as you are!) please suggest it for us !

PS: I'm brazilian, and maybe I forgot the translation of some variable or function from portuguese to english... the code are reviewed, but i'ts late here... ;)
:local "ethernetSpeeds" { "eth_uplink_HP"="100Mbps" ; "ether2"="1Gbps" }
:local cycleNumber 3
:local downtime 2
:local sleepBetween 5
:local trying false;

#Define variables for sending email
:local mailServerName		"PUT_YOUR_MAIL_SERVER_NAME_HERE";
:local mailServerIp			[:resolve $mailServerName];
:local mailServerPort		PUT_YOUR_MAIL_SERVER_PORT_HERE;
:local mailFrom					"PUT_MAIL_FROM_HERE";
:local mailTo						"PUT_MAIL_TO_HERE";
:local mailSubject			"WRITE_YOUR_MAIL_SUBJECT_HERE";
:local mailUser					"PUT_YOUR_MAIL_USER_HERE";
:local mailPass					"PUT_YOUR_MAIL_PASSWORD_HERE";
:local mailBody					"PUT_MAIL_BODY_HERE";

# define sendMail function
:global sendMail do={
	/tool e-mail send server=$mailServerIp port=$mailServerPort from=$mailFrom to=$mailTo subject=$mailSubject body=$mailBody user=$mailUser password=$mailPass;
}

:foreach ethName,targetSpeed in=$ethernetSpeeds do={
	/interface ethernet {
		monitor $ethName once do { :set $currentSpeed $rate }
		:local loopCounter 0

		:if ($currentSpeed != $targetSpeed) do { :log info "Port $ethName current speed: $currentSpeed, target = $targetSpeed: [FAILURE]"	}

		:while (($currentSpeed != $targetSpeed) && ($loopCounter < $cycleNumber)) do={
			:if (!trying) do {	:log info "Starting interface reset procedure >>> " }

			set $ethName disabled=yes
			:delay $downtime
			set $ethName disabled=no
			:set $loopCounter ($loopCounter + 1)
			:delay $sleepBetween

			monitor $ethName once do { :set $currentSpeed $rate }
			:if ($currentSpeed = $targetSpeed) do { :log info "Interface target speed restored." } else { 
				:log info "Attempt $loopCounter of $cycleNumber failed.";
				:set $trying true;
				:if ($loopCounter = $cycleNumber) do {
					:log info "Trying to send email alert...";
					$sendMail;
				}
			}
		}
	}
}
 
efaden
Forum Guru
Forum Guru
Posts: 1711
Joined: Sat Mar 30, 2013 1:55 am
Location: New York, USA

Re: disable/enable ethernet interface script

Tue Sep 06, 2016 7:25 pm

Nice. I'm still curious why that one command I was using is returning 100 all the time.
 
evandromac
just joined
Posts: 12
Joined: Thu Sep 01, 2016 3:19 am

Re: disable/enable ethernet interface script

Tue Sep 06, 2016 9:29 pm

Nice. I'm still curious why that one command I was using is returning 100 all the time.
If you want to investigate deeper, yes I can give you access to the devices returning 100Mbps. Tell me if you want and I'll send it to your by mail.
 
efaden
Forum Guru
Forum Guru
Posts: 1711
Joined: Sat Mar 30, 2013 1:55 am
Location: New York, USA

Re: disable/enable ethernet interface script

Wed Sep 07, 2016 2:53 am

Not that curious... As long as you got it working.
 
User avatar
HiltonT
Frequent Visitor
Frequent Visitor
Posts: 76
Joined: Mon Feb 07, 2011 4:24 am
Location: 'Srayamate
Contact:

Re: disable/enable ethernet interface script

Thu Jun 14, 2018 6:41 am

Out of interest, on 6.42.3 when I run that port speed query, I get the same - every Interface is 100Mbps except for sfp1 (CRS-125-24G-1S-2HnD) which is 1Gbps - and all interfaces are configured as "Auto" with all speeds advertised.

What's even weirder is if I run it even with some ports set to "1Gbps Full" and not on Auto, I still get the reported speed being 100Mbps on all ether ports.
Regards,
Hilton Travis
 
evandromac
just joined
Posts: 12
Joined: Thu Sep 01, 2016 3:19 am

Re: disable/enable ethernet interface script

Sat Jan 12, 2019 9:24 pm

Out of interest, on 6.42.3 when I run that port speed query, I get the same - every Interface is 100Mbps except for sfp1 (CRS-125-24G-1S-2HnD) which is 1Gbps - and all interfaces are configured as "Auto" with all speeds advertised.

What's even weirder is if I run it even with some ports set to "1Gbps Full" and not on Auto, I still get the reported speed being 100Mbps on all ether ports.
Did you tried my solution?
 
Ahmedsaleemqazi
just joined
Posts: 1
Joined: Tue Dec 05, 2017 6:36 pm

Re: disable/enable ethernet interface script

Fri Apr 26, 2019 1:29 am

Hi Everyone,

Hope you are all doing well!

I am using PCC load balancing on my workplace I have two ISP connections which are dividing load equally but the issue is when one of my ISP goes down or have packet loss for a few seconds all of my users face slow internet connectivity experience until I physically disabled one of the WAN link which is down. so I am trying to write a script that will disable my Wan interface automatically when ISP goes down or it has packet loss and enables the Wan interface again when Wan link gets mature depending on the ISP gateway ping. looking forward to your kind response.


Your quick response will be highly appreciated.

Who is online

Users browsing this forum: No registered users and 32 guests