restart ethernet interfaces in script

Posted on
Tue Dec 31, 2013 10:30 am
kw123 offline
User avatar
Posts: 8363
Joined: May 12, 2013
Location: Dallas, TX

restart ethernet interfaces in script

My mac is acting up and every other day or so the ethernet stops working. It looks all green (connected etc) but no packages go through.

The attached applescript tests the ethernet with a ping to 3 different devices once per minute.
If all 3 devices do not answer the ethernet interfaces on the MAC (both en0 and en1) get reset.
After a reset everything seems to work fine.

This is just FYI if someone has a similar problem

Karl


in applescript editor export this script as app with option "stay open after run handler" then add to login item

Code: Select all
-- Karl Wachs
-- Dec 31 2013
-- version 1.0
-- please use as you see fit
--
-- ========>   reset both ethernet interfaces if no responses from routers ..
--
--
-- test 3 hubs once a minute, if ONE answers ethernet is ok, testagain in 1 minute
--
-- save as  "app" with option   "stay open after run handler"
-- set no icon option in package plist if you like
-- add to login auto launch
-- this app will listen to quit command
--
-- ping .. grep will return:   "2 packets transmitted, 0 packets received, 100.0% packet loss"    if no response  ... look for "0 packets" to detect failure
-- ping .. grep will return:   "1 packets transmitted, 1 packets received, 0.0% packet loss"        if successful

--reseting is essentially done by (for en0):
--  ifconfig en0 down
--  ifconfig en0 up 
--
-- plus sudo, piping password and suppressing output to make it work in a script

on run
   global theIP1
   global theIP2
   global theIP3
   global thePassword
   global ethernetInterface1
   global ethernetInterface2

   set theIP1 to "192.168.1.6" -- this is the router IP .......... put the IP numbers of devices to be tested  here
   set theIP2 to "192.168.1.7" -- this is the wifi hub 1
   set theIP3 to "192.168.1.8" -- this is the wifi hub 2
   set ethernetInterface1 to "en1" -- this is the "short name" of ethernet interface adopt to your environment
   set ethernetInterface2 to "en0" --  ...
   
   set thePassword to "your password goes here"
   
   idle 60 -- send to idle immediately
end run

on idle
   global theIP1
   global theIP2
   global theIP3
   global thePassword
   global ethernetInterface1
   global ethernetInterface2
   
   try
      set outputByPing to do shell script "ping -o -t 2 -c 2 " & theIP1 & "  2>/dev/null | grep received " -- test router connection, reduce output,  2 sec wait,  2 pings, dump error messages    
   end try
   if outputByPing contains "0 packets" then --  0 packets received, test next hub
      
      try
         set outputByPing to do shell script "ping -o -t 2 -c 2 " & theIP2 & "  2>/dev/null | grep received " -- test wifi hub 1 connection, reduce output,  2 sec wait,  2 pings, dump error messages
      end try
      if outputByPing contains "0 packets" then --  0 packets received, test next hub
         
         try
            set outputByPing to do shell script "ping -o -t 2 -c  2 " & theIP3 & "  2>/dev/null | grep received " -- test wifi hub 2 connection, reduce output,  2 sec wait,  2 pings, dump error messages    
         end try
         if outputByPing contains "0 packets" then --  0 packets received  by all 3 devices,  reset both ethernets
            
            
            -- reset interface en1
            do shell script " echo '" & thePassword & "' |  sudo -S ifconfig " & ethernetInterface1 & " down 2>&1 &" --   stop interface:  pip in password , options:  dont wait, dump output messages
            do shell script " echo '" & thePassword & "' |  sudo -S ifconfig " & ethernetInterface1 & " up  2>&1 &" --   start interface:    pip in password,  options:  dont wait, dump output messages
            
            delay 1 -- let it settle
            
            -- reset interface en0   
            do shell script " echo '" & thePassword & "' |  sudo -S ifconfig " & ethernetInterface2 & " down 2>&1 &" --   stop interface:  pip in password , options:  dont wait, dump output messages
            do shell script " echo '" & thePassword & "' |  sudo -S ifconfig " & ethernetInterface2 & " up  2>&1 &" --   start interface:    pip in password,  options:  dont wait, dump output messages
            
            
         end if
      end if
   end if
   
   return 60 -- test again in 60 seconds
end idle

on quit
   continue quit
   return
end quit


Page 1 of 1

Who is online

Users browsing this forum: No registered users and 4 guests