Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

log into a website via perl client

by shrubbery (Acolyte)
on Feb 06, 2004 at 17:20 UTC ( #327147=perlquestion: print w/replies, xml ) Need Help??

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

Here's a question thats pretty simple yet I can find a single place that answers it.

How do I get perl to login a website ? I'm trying to make a dynamic DNS update script that logs into my router. The HTML source basically just has a password field like this:

<td><INPUT type="password" maxLength=12 size=9 name=pws></td>

and a submit:

<td><input type="Submit" value="LOGIN">&nbsp;<input name="Close" onClick="self.close();return false" type="button" value="CANCEL"><BR><BR></td>

All I want to do is have the perl script "type" in the password and submit it so I can access the webpages in the router. I'm hitting my head on the wall since the problem is so simple and I can't find a solution.

Replies are listed 'Best First'.
Re: log into a website via perl client
by CountZero (Bishop) on Feb 06, 2004 at 17:26 UTC
    Have a look at the LWP-family of modules.

    I'm quite sure you will find amongst them a module which will deal with all the nitty gritty of logging into a website and do all kinds of useful things to it.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: log into a website via perl client
by cees (Curate) on Feb 06, 2004 at 19:46 UTC

    I'm surprised no one has mentioned WWW::Mechanize. This is a very handy wrapper around LWP and HTML::Form that simplifies walking through multiple web pages, by letting you fill in forms and click on buttons and links without needing to parse the HTML yourself.

    A very simple example that should get you started for your situation follows:

    use WWW::Mechanize; my $url = 'http://192.168.1.1/'; my $a = WWW::Mechanize->new; $a->get( $url ); $a->submit_form( form_number => 1, fields => { pws => 'dummy' } );
Re: log into a website via perl client
by newrisedesigns (Curate) on Feb 06, 2004 at 17:56 UTC

    Simple question, no simple solution.

    If your router is like mine (dLink) it uses Basic Authentication (mentioned by Sol-Invictus) to allow access to the router's functions. What you'll have to do is provide your password like mentioned above, then use LWP to do either a GET or POST (no difference, really) to the page on your router in which you want to modify settings. You'll send the variables in the GET/POST request, and what should be returned is the HTML of the page confirming the changes.

    Pseudo-code:

    use LWP; use HTTP::Request::Common; my $ua = LWP::UserAgent->new(); my $resp = $ua->request(GET 'http://192.168.0.1/admin.cgi'); if($resp->is_success){ # returned 200; request sent/page received print $resp->content; #content of page returned, check this } #to see if it looks like your router's "settings changed" page. else{ print "Couldn't fetch page. Something's wrong!!!\n"; }

    Read the documentation on LWP. It's really useful stuff.
    Hope that helps. Best of luck to you.

    John J Reiser
    newrisedesigns.com

Re: log into a website via perl client
by jbware (Chaplain) on Feb 06, 2004 at 17:28 UTC
    You might want to try LWP. That should have the functionality you're looking for. Try "perldoc lwp" as a start on docs.

    -jbWare
Re: log into a website via perl client
by Sol-Invictus (Scribe) on Feb 06, 2004 at 17:35 UTC
    install libwww-perl-5.76.tar.gz

    then type perldoc lwpcook for your own copy of this handy recipe :)

    ACCESS TO PROTECTED DOCUMENTS Documents protected by basic authorization can easily be access +ed like this: use LWP::UserAgent; $ua = LWP::UserAgent->new; $req = HTTP::Request->new(GET => 'http://www.linpro.no/secret +/'); $req->authorization_basic('aas', 'mypassword'); print $ua->request($req)->as_string;

    Sol-Invictus

    You spend twenty years learning the spell that makes nude virgins appear in your bedroom, and then you're so poisoned by quicksilver fumes and half-blind from reading old grimoires that you can't remember what happens next.

Re: log into a website via perl client
by derby (Abbot) on Feb 06, 2004 at 17:35 UTC
    Submitting the login page is easy - use LWP. The hard part is going to be figuring out what to do after login (since no two apps quite handle it the same way). You may need to catch and store some cookies for future posts and gets.
    -derby
      Thanks for the replies. No luck yet. I'm trying to use the post method in LWP::UserAgent. Anyone have any sort of example?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://327147]
Approved by sschneid
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2022-01-21 10:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (57 votes). Check out past polls.

    Notices?