http://www.perlmonks.org?node_id=955780

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

I've got 2 routers I want to monitor (basically to see if a website goes down). If the website goes down, I want to navigate into the router, and reboot it (which fixes the problem 75% of the time).

I've got a javascript form on the router webpage that requests a password. What's the best way to do this automatically using Perl?

Also have another router site that requests a username & password, but it looks like it comes from a windows prompt. How can I use Perl to automatically enter a username & password into that field?

TIA!

I love it when a program comes together - jdhannibal
  • Comment on Need to fill password in with perl on Javascript?

Replies are listed 'Best First'.
Re: Need to fill password in with perl on Javascript?
by Marshall (Canon) on Feb 23, 2012 at 17:47 UTC
    Here are some more links with LWP info.. read this one and look at Planetscape's reply to my post with more online books.. Re: Perl code to check and count count if a word exists on a webpage.

    Perl cannot run Javascript, but most of the time you don't need to do that. Basically with the various LWP techniques and modules, you are emulating a browser. Figure out what needs to go into the "post" back to the server and just stick the password in there. The webserver has no way to tell whether or not you actually executed some Javascript code - it just looks at what is posted back to it. Usually with some experimentation (sometimes with LOTS of experimentation), I can figure this out by looking at the source code to the webpage.

    Update: When you update a post, add some kind of tag like this so that we can tell that something new has been added. I would stay away from WWW::Mechanize::Firefox unless you really need it. This thing is pretty cool and I've done some experimentation with it, but this introduces another layer of complexity into the implementation.

    Another Update:
    I looked back at one of my LWP programs.

    (1) You are emulating a browser. You can claim to be different versions or types of browsers. I find it helpful to claim to be the "stupidest thing" that the website will allow. Many websites serve up different pages depending upon who you are claiming to be - dumber browsers get different pages than smarter browsers. Below I am claiming to be an ancient version of Netscape.

    (2) Often it is not necessary to even parse what the server sends you. Below I just checked that I got a valid page (no 404 error or whatever), and then I immediately just post back my credentials. I know that this is the log-in page.

    (3) In the post(), I send what I need to. Figuring out what should go into the post() is where the looking at the webpage source and experimentation comes in. The server has no way to figure out what (if any) Javascript code I ran or didn't run.

    my $ua = LWP::UserAgent->new or die "Problem with the new UserAgent\n" +; $ua->cookie_jar(HTTP::Cookies->new); $ua->agent("Mozilla/4.76 [en] (Windows NT 5.0; U)"); print "And now I'm calling myself ", $ua->agent( ), "!\n"; # # Get the log on page to the Secure Server # my $response = $ua->get($url) or die "Problem with the get $url\n"; $response->is_success or die "Failed to GET '$url': ", $response->status_line; my $html_page = $response->content( ); # # post the secure log-on with my credentials # $response = $ua->post( 'https://services....someurl', [ 'type' => "ABCDEF", #weird parameters that are page specific "prev" => "GHIJK", 'p201' => "XYZZY", 'p1' => 'some_user_name', 'p2' => 'some_domain', 'p3' => 'some_password' ], );
    Of course as with all of these things, mileage varies.

      Perl cannot run Javascript, but most of the time you don't need to do that.

      Actually it can, see WWW::Scripter::Plugin::JavaScript/JE, but writing browser is hard work and these are one-man "alpha" releases, firefox/webkit are more mature/reliable

        That's good input. However, as you said: "writing browser is hard work". It is! I recommend to avoid the complexities as much as possible. If something "straightforward" will work, then I would do that.
      My remark is a bit off topic but your note that "Perl cannot run Javascript"
      made me think that with Parrot will come better interop between languages,which means that you could control the browser using Perl6 calling Javascript libraries,since everything bolts down to PIR acting as the common language denominator.
      There is an attempt to port Javascript on Parrot
      Someone with Parrot experience please correct me if I am wrong
        If you have a question about Parrot, I would ask a new question in different thread. I think that everything that was needed to be said, has already been said in this thread. A new question, in a new thread would give your question more focus.
Re: Need to fill password in with perl on Javascript?
by Anonymous Monk on Feb 23, 2012 at 17:31 UTC
    "Best" by which criterium?

    If best means the least amount of moving parts, then let's by-pass JavaScript altogether. Figure out with Wireshark, Firebug or similar how the actual HTTP request looks like. Then use a HTTP client library such as WWW::Mechanize to duplicate the essential parts of the request. In the majority of cases, these are submitted form contents only.

      I got firebug. Can you please explain what parts of the html request I should look for and how to find them using firebug? Thanks!
      I love it when a program comes together - jdhannibal
        Open the Net tab. Click the disclosure widget to see the details of a request/response pair.
Re: Need to fill password in with perl on Javascript?
by runrig (Abbot) on Feb 23, 2012 at 17:35 UTC
Re: Need to fill password in with perl on Javascript?
by osbosb (Monk) on Feb 23, 2012 at 17:35 UTC
    This is not going to be perl related, but why are you trying to fix a different problem? I understand monitoring a router but...man, don't just "reboot" it. Fix the problem and then plug monitoring tools on top of it.
Re: Need to fill password in with perl on Javascript?
by Anonymous Monk on Feb 23, 2012 at 17:35 UTC
    You edited the top node while I was answering, so I missed the additional question.

    That dialog sounds like HTTP basic authentication. See the credentials method in WWW::Mechanize to deal with that.

Re: Need to fill password in with perl on Javascript?
by McDarren (Abbot) on Feb 24, 2012 at 13:34 UTC
    Does your router support other (non-HTTP) management protocols, such as SNMP or Telnet/SSH?

    Most routers do. SNMP is probably the best option, if your router supports it. Net::SNMP will be of assistance with that.

    But I must echo the comment made by osbosb, you should be addressing the root cause of your problem, rather than blindly re-booting when something goes wrong.

    Cheers,
    Darren