My old ADSL internet modem HG2701was burned for some reason and I requested for a replacement to my ISP and it gave me a Thomson TG585-V8 modem which makes useles a little python script that I found around to change the IP and keep downloading… backup copies of something.

As a coincidence some days ago I read about a library called mechanize and of course using one of my favorite languages(ruby) I decided to write a little program to change my IP address.

Here, the description of mechanize library stolen from it’s website:

«The Mechanize library is used for automating interaction with websites. Mechanize automatically stores and sends cookies, follows redirects, can follow links, and submit forms. Form fields can be populated and submitted. Mechanize also keeps track of the sites that you have visited as a history.»

After read some examples and write some testing code I reach to a very simple script that does the magic:

require 'mechanize'

USER = 'USERNAME'
PASSWORD = 'PASSWORD'

URL = 'http://192.168.1.254/cgi/b/is/_pppoe_/ov/?be=0&l0=2&l1=2&name=Internet'

BUTTONS = ['13', '12'] #First is to deconnect, next for reconnect

FORM = 'PPPConn'
SNAME = 'Internet'

m = Mechanize.new

m.user_agent_alias = 'Windows IE 7'
m.auth(USER, PASSWORD)

BUTTONS.each do |bn|
 page = m.get(URL)
 f = page.form_with(FORM)

 f['0'] = bn
 f['1'] = SNAME

 b = f.button_with(:name => bn)
 f.click_button(b)
end

A basic explanation of the above code:

  • We create an instance of the Mechanize class
  • Now we inform to the website which interact to us that we are some kind of webbrowser
  • And then we need to autenticate to the site
  • Then we get the page, the form and fill the correct values
  • Now we click the button to kill the connection
  • So, we repeat the page, form, button, and click the connect button to get a new IP address

Of course, the code has no error checking, it’s a really basic version. But I like it ’cause it’s just a few code.

You can get the code from my bitbucket utils repository and of course I hope to add some features, not so much, it does what suppose to do.

Happy hollidays!.

3 responses

  1. […] and change the information of the given subdomain. The use Mechanize library it’s exposed in other post, you can read it if you wanna know the […]

  2. Sam Mike says:

    Hello! Your site is very nice, and I came here generey and for many visitis. Very good website really! Thanks!

  3. I’ve learn several just right stuff here. Definitely worth bookmarking for revisiting. I surprise how a lot effort you place to make this sort of fantastic informative web site.