📜 ⬆️ ⬇️

Automation of reset of a router of CISCO RVS4000

It so happened that after two years of bug-free work, the home router began to hang from time to time. This manifested itself in a WAN port hang and in the absence of the Internet on the entire subnet, the LAN works fine. The decision was not as simple as it might seem at first.

Before using this manual, backup your router settings.
Finding the right minimum solution requires several options to consider.
The first solution, which was subsequently dropped due to the complexity and duration of implementation - arduino + ethernet shield / raspberry PI + relay. This is the case when you do not need to solve all the problems in the same usual way, even if you really want to. At a minimum, you would have to wait for either an ethernet shield or a raspberry PI. And did not want to wait. Further, it would be logical to assume that since it is impossible to perform a reboot by hardware, then it can be done programmatically. Indeed, the router did not hang completely, and provided an opportunity to enter the admin panel without any problems. 5 minutes google

curl -u 'login:password' 'http://192.168.1.1/setup.cgi?todo=reboot' 


The link works fine in the browser, but through curl, the recipe at close range did not want to work and gave out.
')
  <HTML> <HEAD><TITLE>401 Unauthorized</TITLE></HEAD> <BODY BGCOLOR="#cc9999" TEXT="#000000" LINK="#2020ff" VLINK="#4040cc"> <H4>401 Unauthorized</H4> Authorization required. <HR> <ADDRESS><A HREF=""></A></ADDRESS> </BODY> </HTML> 


Variations with post parameters, using the request library did not give a positive result, the server’s response is still the same. I really didn’t want to understand the intricacies of authorization. Virtual machine + script on sikuli - from the gun on the sparrows. The final solution: use nodejs + coffeescript + phantomjs. It so happened historically that the home linux distribution “for experimenting” with my gentoo. Until the last moment, there was a broken ebuild for phantomjs in gentoo: at first, it just wasn't going to, and then it began to get together, but it still refused to work. It's a shame, but the attempt is not torture.

Create a working environment for crafts and install dependencies:

 mkdir /opt/ext/router_reboot_tool cd /opt/ext/router_reboot_tool npm install -g coffeescript npm install phantomjs phpjs 


And here an interesting fact is revealed. When installing phantomjs, it tightens the ready-made working binary. Here is a miracle, it will not be necessary to dance with a tambourine. Next is a matter of technology.

check.coffee checks the Internet as best it can, and in the event of a fall, calls reboot.sh:

 #!/usr/bin/coffee fs = require('fs') exec= require('child_process').exec php = require('phpjs') _old_console_log = console.log console.log = (t)-> _old_console_log "[#{php.date('dmY H:i:s')}] #{t}" check_internet = (get_result)-> exec 'ping -c1 8.8.8.8', (_skip,result)-> get_result /1 packets transmitted, 1 received/.test result if fs.existsSync 'marker' console.log 'marker detected' fs.unlinkSync 'marker' process.exit() fail_count = 0 max_fail_count = 10 check_count = 0 main_loop = setInterval ()-> check_count++ if check_count > 60*5-10 console.log "wiped (new will started by cron)" process.exit() check_internet (r)-> return if fail_count > max_fail_count #  .  -    # console.log r if r fail_count = 0 else fail_count++ console.log "fail #{fail_count}" if fail_count > max_fail_count console.log "reboot" clearInterval main_loop #      fs.writeFileSync 'marker', '' exec './reboot.sh', (_skip,result)-> console.log result setTimeout ()-> fs.unlinkSync 'phantom_marker' process.exit() , 10000 , 1000 

An additional entry of the marker file is needed in order to not accidentally send 2 reload requests in a row. This may be if the Internet disappeared just before the end of the 5-minute interval. And so we are guaranteed to have a gap of at least 5 minutes between reboots of the router.

Surprise number 1 . clearInterval did not want to kill the interval and reboot.sh was called in some cases 4 times because of what the settings on the router had flown. Suddenly, two consecutive reboot commands at intervals of a second cause a hard reset. The decision crutch remained in the ready decision.

reboot.sh is not very logical logic and cleverness. Simple and banal. Then he will have to fix it .
 #!/bin/bash coffee -c ./phantom.coffee ./node_modules/phantomjs/bin/phantomjs ./phantom.js 

phantom.coffee
 console.log "start..." # fs = require('fs') # if fs.existsSync 'phantom_marker' # console.log "phantom_marker" # phantom.exit() # fs.writeFileSync 'phantom_marker', '' page = require('webpage').create() page.onConsoleMessage = (msg)-> console.log msg url = 'http://192.168.1.1/Reboot.htm' page.settings.userName = 'login' page.settings.password = 'password' page.open url, ()-> page.evaluate ()-> document.getElementsByName('mtenReboot')[0].click() console.log 'waiting...' setTimeout ()-> console.log 'exit' # fs.unlinkSync 'phantom_marker' phantom.exit() , 10000 


Due to the fact that I wanted to add protection against accidental launch of reboot.sh twice, I added an entry to the phantom_marker file as an indicator that the process is already running and there is no need to start another one. But here I was in for a surprise number 2.

Surprise number 2 . phantomjs scripts do not support nodejs modules (either just the fs module, or support, but some old version of the modules didn’t make sense to understand in detail). It's a shame, we comment, we make a check in reboot.sh, which is less beautiful, but it works.

Updated reboot.sh:

 #!/bin/bash if [ -a 'phantom_marker' ] then echo "phantom_marker present" exit fi touch phantom_marker coffee -c ./phantom.coffee ./node_modules/phantomjs/bin/phantomjs ./phantom.js unlink phantom_marker 

Final touches
 chmod +x check.coffee chmod +x reboot.sh crontab -e */5 * * * * cd /opt/ext/router_reboot_tool && ./check.coffee 2>&1 >> ./log /etc/init.d/vixie-cron restart 


We make the rule already with pens (I believe that there is a way to do it from the console, but I didn’t have the task to automate the installation of this craft). After updating crontab, reboot cron, otherwise it will not work.

A couple of tests with manual shutdown of WAN showed that the script works as it should. In total, 2-4 hours were spent on finding a solution and on solutions to surprises, which someone may not have, so I decided to put the solution on a general review. Modifying the scripts under a different router is not difficult; you just need to change the path to the page with the reboot button and the button itself.

What I would like to finish, but did not get around: remove the crutch for unnecessary checking fail_count.

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


All Articles