zentara has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I just got SBC/Yahoo dsl, using a Speedstream 4100 modem. To connect and disconnect, you access it's internal web server at http://192.168.0.1 thru a web browser, but sometimes I would like to do this from the commandline using LWP or WWW::Mechanize.

The problem is that sometimes the modem prompts you to fill in a form to enter the "modem access number", which is printed on the bottom of the modem. Sometimes it dosn't.

So I've discovered a few ways to control it:

#!/usr/bin/perl use warnings; use strict; use WWW::Mechanize; my $url = 'http://192.168.0.1/'; my $agent = new WWW::Mechanize; $agent->get($url); $agent->submit_form( 'form_number' => 1, 'fields' => { 'username' => 'myuser@sbcglobal.net', 'password' => 'goombah', } ); print $agent->content; #sometimes it asks for the access code, sometimes not if ($agent->content =~ /Access Code Required/){ print "Access Code Required\n"; } else{print "sucess\n"}

Once the access code has been entered, it can be controlled with a simple get, passing ?conn=0 or ?conn=1

my $ua = new LWP::UserAgent; my $request = new HTTP::Request( "GET", "http://192.168.0.1/connect.cg +i?conn=0" ); my $response = $ua->simple_request( $request ); my $contents = $response->content(); print "$contents\n";

I'm trying to figure out how to use WWW::Mechanize to handle the second page which prompts for the access number and "posts" it to accessLogin.cgi

Has anyone done this yet, and can show the way?


I'm not really a human, but I play one on earth. flash japh

Replies are listed 'Best First'.
Re: Accessing a DSL modem with LWP
by linuxfan (Beadle) on Sep 02, 2005 at 19:47 UTC
    I've not used WWW::Mechanize before, but I have done something similar to what you are looking for using Net::SSLeay

    My webserver sent a sessionID variable which was required for all POST requests (this is sent only for the first page, and is verified for all subsequent POST requests sent). After posting to the initial page, did you check for any <form> embedded in the resultant html ($contents in your code). With my webserver, each post request would result in an HTML with another <form> element in it, and using the same cookie, I was able to sent a post request to the CGI script embedded in the HTML.

    I would check for some kind of cookie/sessionID being sent by the server with the initial HTML...

Re: Accessing a DSL modem with LWP
by gman (Friar) on Sep 02, 2005 at 20:11 UTC
    Why not have more control by putting the modem in bridge mode and running pppoe session on your system? That would give you even more scripting, and depending on what OS / pppoe software maybe API, control.
    Just a thought
Re: Accessing a DSL modem with LWP
by zentara (Cardinal) on Sep 03, 2005 at 10:40 UTC
    Yes, I was a little tired when I posted this. The modem's output was a bunch of deeply nested tables, which made me go crosseyed, and there was some unneccessary hidden fields.

    I woke up this morning and it was obvious what to do. :-) As it turned out, I only needed HTTP::Request

    #!/usr/bin/perl use warnings; use strict; use HTTP::Request::Common qw(GET POST); use LWP::UserAgent; my $ua = new LWP::UserAgent(timeout=> 5); #now actually post the form my $request = POST "http://192.168.0.1/login.cgi", [ username => 'myuser@sbcglobal.net', password => 'goombah', ]; my $response = $ua->request($request); my $content = $response->as_string(); #print "$content\n"; #sometimes it asks for the access code, sometimes not if ($content =~ /Access Code Required/){ print "Access Code Required\n"; my $request = POST "http://192.168.0.1/accessLogin.cgi", [ pw0 => 4589235692, #from bottom of modem ]; $response = $ua->request($request); $content = $response->as_string(); print $content; }else {print "sucess\n"}
    Once that has been done, I can use a simple get to turn it on and off.
    #!/usr/bin/perl use warnings; use strict; use LWP::UserAgent; my $ua = new LWP::UserAgent; my $request = new HTTP::Request( "GET", "http://192.168.0.1/connect.cg +i?conn=0" ); #conn=1 to connect my $response = $ua->simple_request( $request ); my $contents = $response->content(); print "$contents\n";

    I'm not really a human, but I play one on earth. flash japh
Re: Accessing a DSL modem with LWP
by spiritway (Vicar) on Sep 03, 2005 at 05:44 UTC

    I have the same setup - SBC/Yahoo! DSL using the 4100 modem. I have not had to access the server or anything even remotely similar. I simply plug in the computer and go.

    Your problem might be related to the software that SBC sends you with their modem. It appears to be designed to hamper users, instead of helping them. You might want to consider removing that software and simply using the modem as it is. I have done this with both Windows and Linux, with good results.

Re: Accessing a DSL modem with LWP
by PodMaster (Abbot) on Sep 03, 2005 at 09:18 UTC
    You seem a little tired :|
    Mechanize allows you to see what forms are on the page, links, etc. So you just check to see if the second page contains the access code form, and if it does you submit it.

    WWW::Mechanize::Shell is quite useful in getting a WWW::Mechanize script started.

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.