[SOLVED] Perl Script

Alles, was Perl betrifft, kann hier besprochen werden.

[SOLVED] Perl Script

Postby steffenbaier » 15. June 2009 17:34

Hi,

I am trying to build a Web Page and struggle to forward some data into a Perl Script in order to PUSH content to a Destination.

I have a HTML Page:
Code: Select all
<html>
<head>
<title>Polycom Text Push</title>
</head>

<body>

<form method="post" action="http://10.222.22.221/cgi-bin/test.pl">

<h2>Enter the details of the Phone you would like below content being pushed to?</h2>

Realm:   <input type="text" name="realm" value="PUSH Authentication"><br>

Username:   <input type="text" name="user" value="steff"><br>

Password:   <input type="text" name="pw" value="ge1c0"><br> 

IP Address:   <input type="text" name="ip"><br>

Text:   <input type="text" name="reluri" value="Hello!" ><br>

Importance:   <input type="text" name="priority" value="critical" ><br>

<input type="submit" value="Submit">

</form>

</body>
</html>


Above calls the test.pl Perl Script below:

Code: Select all
#!c:/xampplite/perl/bin/perl.exe


print "Content-type: text/html\n\n";

# *** Perl distribution libwww-perl is REQUIRED ***
#

&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><URL';
if ($priority) {
   $httpcontent = $request{'reluri'}.' priority="'.$request{'priority'}.'"';
}
$httpcontent = $httpcontent.'>'.$request{'reluri'}."</URL></PolycomIPPhone>\n";

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;
}
}



All I would like to do is for this script to push the following to the specified IP Addess:

Code: Select all
<PolycomIPPhone><Data priority="critical">Hello!</Data></PolycomIPPhone>


Above Perl Script results in a new Web Page with this:

Code: Select all
Here are all the Details that need submitting:

Realm: PUSH Authentication
Username: steff
Password: ge1c0
IP Address: 10.222.22.222
Text: Hello!
Importance: critical

Push Content: Hello!
Sending POST to http://10.222.22.222/push


The Push Content is just the Data from reluri but the below command line Perl Script combines the priority and the reluri:

Code: Select all
#!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><URL';
if ($priority) {
   $httpcontent = $httpcontent.' priority="'.$priority.'"';
}
$httpcontent = $httpcontent.'>'.$reluri."</URL></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;


Any Hints ..?

As above Command Line Example works ...
Last edited by steffenbaier on 17. June 2009 16:52, edited 1 time in total.
steffenbaier
 
Posts: 14
Joined: 14. April 2009 20:46
Location: London - United Kingdom

Re: [Question] Perl Script

Postby steffenbaier » 17. June 2009 16:52

Solved:

HTML Page:

Code: Select all
<html>
<head>
<title>Polycom Text Push</title>
</head>

<body>

<form method="post" action="http://192.168.178.10/cgi-bin/test.pl">

<strong>Please Enter or modify the details below in order to push the Text to the Phone</strong><br>

Realm:   <input type="text" name="realm" value="PUSH Authentication"><br>

Username:   <input type="text" name="user" value="test"><br>

Password:   <input type="text" name="pw" value="ge1c0"><br> 

IP Address:   <input type="text" name="ip"><br>

Text:   <input type="text" name="reluri" value="Hello!" ><br>

Importance:   <input type="text" name="priority" value="critical" ><br>

<input type="submit" value="Submit">

</form>

</body>
</html>


Perl Script:

Code: Select all
#!c:/xampplite/perl/bin/perl.exe

print "Content-type: text/html\n\n";
&getFormData;
print "<h2>Here are all the Details that have been submitted:</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";
print "<br>\n";



require LWP::UserAgent;
my $ua = LWP::UserAgent->new;
$ua->timeout(10);
$ua->credentials($request{'ip'}.':80', $request{'realm'}, $request{'user'}, $request{'pw'});

my $response = $ua->post($phoneuri, Content => $httpcontent);
print "<br>\n";
print "Response from host: ------------------------------------\n";
print "<br>\n";
print $response->as_string;
print "<br>\n";
print "----------------------------------------------------------\n";

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;
}
}
steffenbaier
 
Posts: 14
Joined: 14. April 2009 20:46
Location: London - United Kingdom

Re: [SOLVED] Perl Script

Postby johndavid » 31. August 2009 08:09

Hello,

does anyone know the usage of the statfs function in perl ?

my($ftype, $bsize, $blocks, $bfree, $files,$ffree, $bavail) = statfs("/tmp");

the above does not seem to work for some reason !



regards,
johndavid
 
Posts: 1
Joined: 31. August 2009 07:49

Re: [SOLVED] Perl Script

Postby samtha » 23. April 2010 14:22

I would have loved to help you with your problem. But the later part of your code seems to be very complicated.

Why don't you use this sample code :

<?php header( 'Location: http://www. http://'.$phoneip.'/push ) ; ?>

at the top of your code page instead of

my $phoneuri = 'http://'.$phoneip.'/push',

with a little bit of customization. Hope it helps you.
samtha
 
Posts: 1
Joined: 23. April 2010 13:16


Return to Perl

Who is online

Users browsing this forum: No registered users and 6 guests