curl -u 'login:password' 'http://192.168.1.1/setup.cgi?todo=reboot'
<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>
mkdir /opt/ext/router_reboot_tool cd /opt/ext/router_reboot_tool npm install -g coffeescript npm install phantomjs phpjs
#!/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
#!/bin/bash coffee -c ./phantom.coffee ./node_modules/phantomjs/bin/phantomjs ./phantom.js
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
#!/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
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
Source: https://habr.com/ru/post/200328/
All Articles