disable/enable ethernet interface script

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?

Yeah… entirely possible.

You can use schedules and script to make it…

Sent from my Che2-L11

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

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!

Read the post I posted… If I get a chance I can try to piece together a script from stuff I already have written.

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
		}
	}
}

thx, i will try soon

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.

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

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!

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;
  5. 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… :wink:

: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;
				}
			}
		}
	}
}

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.

Not that curious… As long as you got it working.

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?