Geek; Programmer; Pythonista; FOSS enthusiast, evangelist and contributor; Melange developer; opinionated about programming languages; crazy about cars and air-planes; choosy in watching movies; loves Bangalore, San Francisco and Southern California; and most importantly addicted to coffee!
Madhusudan C.S.
map (thoughts) => words;   reduce (words) => this;
April 5, 2008, 3:21 p.m.

<!--break-->
   Suppose you have an Internet connection where you have a particular time in the day when downloads are not counted or it is unlimited. This is especially true in case of BSNL 500 plan users. The following article explains the procedure of scheduling your ubuntu system to reset the connection so that your ISP calculates the usage within the successive resets as downloaded completely within the unlimited hours.

   Here we go. Open a new text file and put the following contents in it.

   #!/usr/bin/env expect

   set username admin
   set pass admin
   set host 192.168.1.1

   spawn telnet ${host}

   expect -re "login:"
   send "${username}\r"

   expect "Password:"
   send "${pass}\r"

   expect -re "#"
   send "kill 1\r"
   send "exit\r"
   expect eof

   Let me explain these lines one by one. This file is a script which can be run from the shell. So there is an interpreter line beginning with # which tells the shell with what interpreter the script should be executed. In this case /usr/bin/env except informs the shell that it should execute the script with except TCL/Tk interpreter. If the shell gives an error saying expect is not found please install expect by typing as below in the terminal:

   sudo apt-get install expect 

   Next three lines sets the values of the variables username, pass and host. Here username and password must be those which you give to login to your modem/router's setup page and host IP must be the default gateway of your modem/router which you type in your browser to login to your modem/router's setup page. In most of the case username and password will be admin and admin. In quite a few modems/routers it will be admin and password respectively. The host IP will be 192.168.1.1 in most of the cases, if its other than that please check your modem/router's manual.

Note : If you are using D-Link GLB-502T router your username must be root and password must be admin even though you give admin and admin as username and password respectively in your router's login page.

   When we try to remote login to the modem/router it prompts for a username. This is accomplished by the use of expect command in the next line which is the form expect -re "login:". This indicates that the shell is expecting for the string "login:" somewhere in the next line in the prompt. Option -re indicates that Regular Expression Search must be performed on the string. The send command which is of the form send "${username}\r" sends the username to the prompt. Same thing holds good for password too.

   Finally upon successful login a # prompt appears. Now we can give commands to the router. Now we have to give the command for the modem/router to restart. The command "kill 1" kills the first process of the router hence restarting it and "exit" command is used to exit from the router/modem.

   With this done we have the script ready. Save it with some name say router-restart and close the text editor. Now we need to schedule it. Say the unlimited hours start at 2AM and ends at 8AM as in case of BSNL users, we need to schedule in such a way that this script runs at 2AM once and at 8AM again. Scheduling on GNU/Linux is done by a daemon (a process which runs at all the times) called cron. We need to add an entry to the crontab file which maintains the scheduling information to be executed by cron daemon. In the terminal type:

    chmod 777 router-restart (This is done to make the script executable.)
    crontab -u your-username -e
   
   Example: crontab -u madhu -e

   This opens a text file which may have some information already if you have scheduled something or else the file will be empty. Now add the following two lines to the file, one line each for one modem restart, one being at 2AM and other being at 8AM:

   mins hrs day-of-month month day-of-week path-to-script
  
   Example:
     00 2 * * * /home/madhu/router-restart
     00 8 * * * /home/madhu/router-restart

   The first line indicates that the script must be executed at 2:00AM on every day of the month in all months and on every day of the week. Similarly the second line indicates that the script must be executed at 8:00AM on all the days and the full path to script is given as the last argument.

   With this you are now ready with a scheduler to restart your modem/router automatically. Additionally if the software you use to download has a built in scheduler you can set it to start the downloads some time after 2AM say 2:02AM and stop downloading at 7:58AM. Now you are fully equipped for scheduling anything on GNU/Linux. Enjoy!!!

   In case of any problems or queries please write back to me at madhusudan@madhusudancs.info.

-- Yours lovingly
Madhusudan.C.S

comments powered by Disqus