📜 ⬆️ ⬇️

Dance with DLink router

How annoying these constant cliffs of Ukrtelecom Internet are. It seems that the line is in order, and the router is correctly configured, but the Internet refuses to work stably. Everything would be fine if it did not affect my immediate work. I am a freelancer, which translated means awake - and already at work. That is, any unplanned failures (in my city there are failures according to the plan), to put it mildly, are not very pleased and affect performance.

Well, okay, I will not whine - to the point.

During the next strike, my router decided to automate the process of rebooting it, which usually leads to the connection of a ppp connection. I wrote a bash script that connects to my DLink, logs in and requests the rebootinfo.cgi file. This whole process is successfully placed in the cycle “until there is an Internet”, and I accordingly move to the kitchen to drink tea. It's simple. Vim in hand - and go:

 #! / bin / bash
 host = $ {host: = "4.2.2.2"}
 router_ip = "192.168.1.1"
 router_reboot_path = "/ rebootinfo.cgi"
 router_user = "admin"
 router_pw = "pw_here"
 num_packets = 4
 receiving = ""
 reboots = 1
 connect_wait = 60
 while ["$ receiving" == ""]
 do
         echo -ne `date +% T`" \ t "
         echo "pinging $ host with $ num_packets packets"
         receiving = `ping $ host -c $ num_packets |  grep "$ num_packets received" `
         if ["$ receiving"! = ""]
         then
                 echo -ne `date +% T`" \ t "
                 echo "$ host is alive"
         else
                 echo -ne `date +% T`" \ t "
                 echo "$ host not responding"
                 echo -ne `date +% T`" \ t "
                 echo -n "reboot router - attempt $ reboots"
                 let "reboots + = 1"
                 `curl -s -u $ router_user: $ router_pw" http: // $ router_ip $ router_reboot_path "> / dev / null`
                 sleep 10
                 routerAlive = ""
                 while ["$ routerAlive" == ""]
                 do
                         routerAlive = `ping $ router_ip -c $ num_packets |  grep "$ num_packets received" `
                         if ["$ routerAlive"! = ""]
                         then
                                 echo "done."
                                 echo -ne `date +% T`" \ t "
                                 echo -n "waiting $ connect_wait sec for possible connect"
                                 sec = 0
                                 while ["$ sec"! = $ connect_wait]
                                 do
                                         let "sec + = 1"
                                         if ["$ sec" == $ connect_wait]
                                         then
                                                 echo "."
                                         else
                                                 echo -n "."
                                         fi
                                         sleep 1
                                 done
                         else
                                 echo -n "."
                         fi
                 done
         fi
 done

Approximate output:
')
 $ ./routerReboot
 23:25:48 pinging 4.2.2.2 with 4 packets
 23:26:01 4.2.2.2 not responding
 23:26:01 reboot router - attempt 1 ..... done. 
 23:27:10 waiting 60 sec for possible connect ....................................... .....................
 23:28:11 pinging 4.2.2.2 with 4 packets
 23:28:14 4.2.2.2 is alive

The truth was that I had to understand the bash without Google - I myself am not strong in it, so I searched for scripts in / usr / lib, so that, by the example, I read the manual. In the end, the script earned (although the Internet appeared faster :). After a while he really came in handy to me.

Yes, 4.2.2.2 - pings all the time, so you can use ya.ru instead. In addition, using it you can check DNS.

Happy friday

Source: https://habr.com/ru/post/99245/


All Articles