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

Hi, Folks

I am a newbie to the Perl Programming. I'm attempting to write a Perl script that can upload a file to a web server based on the following file upload form:

<form method="post" action="/cgi-bin/luci/engineer/advanced/diagnostic +s/upgrade/"> <div class="cbi-map"> <h2><a id="content" name="content">Authorization Required</a>< +/h2> <div class="cbi-map-descr"> Please enter your username and password. </div> <fieldset class="cbi-section"><fieldset class="cbi-section-nod +e"> <div class="cbi-value"> <label class="cbi-value-title">Username</label> <div class="cbi-value-field"> <input class="cbi-input-user" type="text" name="us +ername" value="" /> </div> </div> <div class="cbi-value"> <label class="cbi-value-title">Password</label> <div class="cbi-value-field"> <input class="cbi-input-password" type="password" +name="password" /> </div> </div> </fieldset></fieldset> </div> <div> <input type="submit" value="Login" class="cbi-button cbi-butto +n-apply" /> <input type="reset" value="Clear" class="cbi-button cbi-button +-reset" /> </div> </form>

And here's my Perl code (URL, username, and password redacted):

use LWP; use HTTP::Cookies; use WWW::Mechanize; my $userAgent = LWP::UserAgent->new(); my $request = POST 'http://192.168.1.1/cgi-bin/luci/root/advanced/diag +nostics/upgrade', Content_Type => 'multipart/form-data', Content => [ +file_0 => ['upload.zip']]; $request->authorization_basic('root', '1234'); my $response = $userAgent->request($request); print $response->error_as_HTML . "\n" if $response->is_error; my $cj = HTTP::Cookies->new( autosave => 1, ignore_discard => 1 ); my $mech = WWW::Mechanize->new( cookie_jar => $cj ); $mech->get( 'http://192.168.1.1/cgi-bin/luci/root/advanced/diagnostics +/upgrade' ); if ($mech->success()) { $mech->submit_form( form_name => 'sysauth', fields => { os_usernam +e => 'root', os_password => '1234' } ); }
The output says:"There is no form named"sysauth"". Can anyone tell me how to fix this? I found another thing, when I login, the source code of form changed:
<form method="post" action="/cgi-bin/luci/;stok=4DB4115975EF48FD742227 +BAD3580034/engineer/advanced/diagnostics/upgrade" enctype="multipart/ +form-data"> <table width="100%"> <tr>&nbsp;</tr> <tr> <td width="15%" align="right">Firmware image:</td> <td width="25%" align="left"> <input type="file" size="30" name="image" /> </td> <td width="10%">&nbsp;</td> <td width="40%" align="right"> <input class="cbi-input-save" type="submit" value="Fla +sh the specified firmware file" /> </td> <td width="10%">&nbsp;</td> </tr> </table> &nbsp; </form>
A random token was generated to the URL, so maybe I need use HTTP:Cookies to keep this change, any suggestions from u, monks?

Update: I have tried another method please jump to topic:Control IE to upload file issue

Replies are listed 'Best First'.
Re: Upload Issue
by zentara (Cardinal) on Jan 12, 2010 at 15:46 UTC
    I'm a bit rusty on cgi fileuploads, but maybe you don't need WWW::Mechanize just to upload a file with basic_auth handled by the webserver. You seem to have a redundant attempt at auth. Try this:
    #!/usr/bin/perl use warnings; use strict; use LWP::UserAgent; use HTTP::Cookies; use HTTP::Request::Common qw(POST); #test on plain http my $https_post = 'https://zentara.zentara.net/~zentara/cgi-bin/uploads +/up1.cgi'; my $https_user = 'zentara'; my $https_pass = 'foobar'; my $file = 'testout.tgz'; &postHTTPS(); sub postHTTPS { my $ua = new LWP::UserAgent; $ua->protocols_allowed( [ 'http'] ); $ua->cookie_jar(HTTP::Cookies->new(file =>".cookies.txt",autos +ave => 1)); #setup request my $req = POST($https_post, Content_Type => 'multipart/form-data', Content =>[ file =>[ $file ], ], ); my $user = 'zentara'; my $pass = 'foobar'; $req->authorization_basic($user, $pass); #do post my $response = $ua->request($req); if ($response->is_error()) { printf " %s\n", $response->status_line; print "https request error!\n"; } else { my $content = $response->content(); print "$content\n"; } if ( $response->is_success ) { print $response->as_string; }else { print $response->status_line; } }

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku
Re: Upload Issue
by FloydATC (Deacon) on Jan 12, 2010 at 20:19 UTC
    The enctype="multipart/form-data" bit is important, without this the file won't get uploaded. I've never used WWW::Mechanize but with CGI you should be able to recover the file size, type, filename and contents using something like this (untested) code:
    use CGI; my $query = new CGI; my @names = $query->param; foreach my $name (@names) { print "$name => ".$query->param($name)."\n"; }
    Also note that the CGI module has methods for creating the upload form, and make sure you read about how to limit file sizes.

    -- Time flies when you don't know what you're doing
Re: Upload Issue
by apl (Monsignor) on Jan 12, 2010 at 15:54 UTC
    And?

    That is to say, does the program

    • not do the upload
    • produce an error when run
    • not run at all
    • produce a Server error
    You might also want to produce an error if any of your calls (e.g. the new, the get) fails...
      Answer to apl. Issue is that it not do upload after login the upload page. I found the URL changed after login and need a token. I have opened another question named "Post Error (upload files)".
      Answer to apl. Issue is that it not do upload after login the upload page. I found the URL changed after login and need a token. I have opened another question Post Error (upload files)
Re: Upload Issue
by owenhhs007 (Initiate) on Jan 13, 2010 at 10:07 UTC
    Yes. I found this upload page is an encryption form. When I login the cookie is generated as
    Set-Cookie: sysauth=ACA415DA69DD4170D34D2DDCE2B610F5; path=/cgi-bin/lu +ci/;stok=44F89C9B8E1EB34F9D379B5162763EAF Location: /cgi-bin/luci/;stok=44F89C9B8E1EB34F9D379B5162763EAF/enginee +r
    The URL changed when post the upload form. I used the upload file is around 15M with .ZIP form. I am still working on this, Thanks for your information