
SteffenBaier
Novice
Jun 17, 2009, 5:16 AM
Post #1 of 2
(1493 views)
|
[SOLVED] Using Perl Script from a Webpage to POST HTTP Data
|
Can't Post
|
|
Hi, I am trying to create a Perl Script that enable me using a Web Page running on Xampplite to POST Information to a Micro Browser running on a Polycom Phone. I have absolutely no Perl Knowledge but have already a basic working Perl Script that can be called from a Windows Command Line. Working Example: #!c:/xampplite/perl/bin/perl.exe use strict; use warnings; # *** Perl distribution libwww-perl is REQUIRED *** require LWP::UserAgent; if (scalar @ARGV < 5) { print "\nSends an authenticated POST request, containing a URI to fetch, to an IP phone.\n\n"; print "Usage:\n"; print "digest-uripush-ua.pl <auth_realm> <auth_user> <auth_pw> <phone_ip> <relative_uri> [<priority>]\n"; print "\n"; print "e.g. digest-uripush-ua.pl \"PUSH Authentication\" JoeUser MyPassword 172.24.44.135 apps/apptest.plcm critical\n"; print " Sends a POST to 172.24.44.135 with content <URI priority=\"critical\">apps/apptest.plcm</URI>\n\n"; exit; } my $realm = $ARGV[0]; my $user = $ARGV[1]; my $pw = $ARGV[2]; my $phoneip = $ARGV[3]; my $reluri = $ARGV[4]; my $priority = 0; if (defined $ARGV[5]) { $priority = $ARGV[5]; } print "\nAuthentication Realm: ", $realm; print "\nAuthentication Username: ", $user; print "\nAuthentication Password: ", $pw; print "\nTarget Phone IP: ", $phoneip; print "\nPush URI: ", $reluri; print "\nPriority: "; if ($priority) { print $priority; } else { print '<default>'; } print "\n\n"; my $phoneuri = 'http://'.$phoneip.'/push'; my $httpcontent = '<PolycomIPPhone><Data'; if ($priority) { $httpcontent = $httpcontent.' priority="'.$priority.'"'; } $httpcontent = $httpcontent.'>'.$reluri."</Data></PolycomIPPhone>\n"; print "Push Content:\n".$httpcontent."\n"; print "\nSending POST to ", $phoneuri, "\n"; my $ua = LWP::UserAgent->new; $ua->timeout(10); $ua->credentials($phoneip.':80', $realm, $user, $pw); my $response = $ua->post($phoneuri, Content => $httpcontent); print "- Response from host: ------------------------------------\n"; print $response->as_string; print "----------------------------------------------------------\n"; exit; above would be called like this: digest-uripush-data.pl "PUSH Authentication" test ge1c0 192.168.253.100 Hello! critical and output this: Authentication Realm: PUSH Authentication Authentication Username: test Authentication Password: ge1c0 Target Phone IP: 192.168.253.100 Push URI: Hello! Priority: critical Push Content: <PolycomIPPhone><Data priority="critical">Hello!</Data></PolycomIPPhone> Sending POST to http://192.168.253.100/push - Response from host: ------------------------------------ HTTP/1.1 200 OK Connection: keep-alive Date: WED, 17 JUN 2009 11:24:12 GMT Server: Polycom SoundPoint IP Telephone HTTPd Content-Length: 43 Content-Type: text/html Client-Date: Wed, 17 Jun 2009 11:24:16 GMT Client-Peer: 192.168.253.100:80 Client-Response-Num: 1 Push Message will be displayed successfully ---------------------------------------------------------- My Attempt: I collect the $request{'realm'} etc Data via a Web Page (Example.html attached) and then parse them to the Perl Script (Test.pl attached) #!c:/xampplite/perl/bin/perl.exe print "Content-type: text/html\n\n"; &getFormData; print "<h2>Here are all the Details that need submitting:</h2>"; print "<br>\n"; print "Realm: $request{'realm'}"; print "<br>\n"; print "Username: $request{'user'}"; print "<br>\n"; print "Password: $request{'pw'}"; print "<br>\n"; print "IP Address: $request{'ip'}"; print "<br>\n"; print "Text: $request{'reluri'}"; print "<br>\n"; print "Importance: $request{'priority'}"; print "<br>\n"; print "\n\n"; my $phoneuri = 'http://'.$request{'ip'}.'/push'; my $httpcontent = '<PolycomIPPhone><Data priority="'.$request{'priority'}.'">'.$request{'reluri'}.'</Data></PolycomIPPhone>'; print "<br>\n"; print "Push Content:\n".$httpcontent."\n"; print "<br>\n"; print "\nSending POST to ", $phoneuri, "\n"; require LWP::UserAgent; my $ua = LWP::UserAgent->new; $ua->timeout(10); $ua->credentials($request{'ip'}.':80', $request{'realm'}, $request{'user'}, $request{'pw'}); exit; # Subroutine below this line. sub getFormData { read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); @pairs = split(/&/, $buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $value =~ s/\n/ /g; $request{$name} = $value; } } Will return this on a Web Page the script creates in order to check the Output: Here are all the Details that need submitting: Realm: PUSH Authentication Username: chad Password: ge1c0 IP Address: 192.168.253.100 Text: Hello! Importance: critical Push Content: Hello! Sending POST to http://192.168.253.100/push As you can see in above the Push Content only shows the $request{'reluri'} and has not joined the $request{'priority'} to it. It also does not POST the Data to the IP Address specified. In the Beginning I only had xampplite installed and I now added the PERL add on as an error: Can't locate LWP/UserAgent.pm in @INC (@INC contains: .) pointed towards a not fully Installed LWP Packet. Could anyone help me with above? THX Steffen
(This post was edited by SteffenBaier on Jun 17, 2009, 6:00 AM)
|