CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Script stopped working...[Resolved]

 



gentoobob
New User

Mar 17, 2018, 6:59 PM

Post #1 of 4 (515 views)
Script stopped working...[Resolved] Can't Post

First off, I've never scripted with Perl. I'm a Network Engineer. This script will only work on the 2010 Debian box. We wanted to migrate the script to a new Oracle linux (redhat) VM server. The Debian box was using Perl 5.10. and the Oracle box is using 5.16. We downloaded the modules that was being used in the script but the script will no longer work on the new linux VM. The script accesses Cisco's Call Manager (phone system), it pulls data off the pages. It then parses it (XML) into two columns, users and extensions, and dumps it into a .CSV file.
When this script is ran on the new linux VM, it gives an error trying to access the URL. I found that it was getting denied for two reasons 1) authentication 2) SSL Cert. So after troubleshooting, I learned that I needed to use LWP::UserAgent, not LWP::Simple (or at least I think thats the case). Once I got the UserAgent set up correctly to log in and ignore the SSL cert, I was able to access the site. Then I learned that something with the XML::Simple wasn't working right on the new linux VM. I keep getting a "Not a GLOB reference" error. I've gone as far as trying to get Data::Dumper to work. I'm at a complete lost at this point. To be honest, I can't seem to grasp this GLOB reference or where in the script is the true problem.

Glob error I get..."Not a GLOB reference at /usr/share/perl5/vendor_perl/XML/SAX/PurePerl/Reader/UnicodeExt.pm line 10."

I do not know if something has changed in the versions of code or modules from the old Debian linux box and the newer Oracle linux VM. Any help would be greatly appreciated.

I will list the original script, then list what I have modified.

---Original (works ONLY on the Debian linux Perl 5.10 box)...


Code
#!/usr/bin/perl 

use LWP::Simple;
use XML::Simple;


my $xml = new XML::Simple;
my @userdata;

$page = 1;

#Get Unity subscriber data from cucxnpub one page at a time, 2000 records to a page
while(1)
{
#Uses ADQuery as the Unity service account
my $url = "https://User:password\@cucxnpub/vmrest/users?rowsPerPage=2000\&pageNumber=$page";
my $content = get($url);
die "Error getting $url" unless defined $content;

my $data = $xml->XMLin($content);
$size = @{$data->{User}};

#If we don't get at least one user end the loop
if(@{$data->{User}} < 1)
{
last;
}

#Build the userdata array, each entry contains "username,extension"
$start = (($page-1) * 2000);
for($i=$start;$i<=$start + @{$data->{User}} - 1;$i++)
{
push(@userdata,"$data->{User}->[$i-$start]->{Alias},$data->{User}->[$i-$start]->{DtmfAccessId}");
}
$page++;
}

#Create a timestamp containing year, month, and day
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year+=1900;
$mon++;
$timestamp = sprintf("%4d%02d%02d",$year,$mon,$mday);

#Dump the results of the unity query to a file
open(UNITY,">/usr/scripts/unityLDAP/$timestamp-unity.csv");
for(@userdata)
{
print UNITY "$_\n";
}
close(UNITY);



--Modified by me, I get at it to login to the site but get the GLOB error mentioned above...


Code
use LWP::UserAgent; 
use LWP::Protocol::https;
use XML::Simple;

$xml = new XML::Simple;
@userdata;

$userName = 'user';
$passwd = 'password';

$page = 1;

#Get Unity subscriber data from cucxnpub one page at a time, 2000 records to a page
while(1)
{

my $url="https://cucxnpub/vmrest/users?rowsPerPage=2000\&pageNumber=$page";

$ua = LWP::UserAgent->new(ssl_opts => {
SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE,
verify_hostname => 0,
});

$header = HTTP::Headers->new;
$req = HTTP::Request->new(GET => $url);
$req->authorization_basic($userName,$passwd);
$req->content_type('application/xml');

$content = $ua->request($req);

my $data = $xml->XMLin($content);

$size = @{$data->{User}};

#If we don't get at least one user end the loop
if(@{$data->{User}} < 1)
{
last;
}

#Build the userdata array, each entry contains "username,extension"
$start = (($page-1) * 2000);
for($i=$start;$i<=$start + @{$data->{User}} - 1;$i++)
{
push(@userdata,"$data->{User}->[$i-$start]->{Alias},$data->{User}->[$i-$start]->{DtmfAccessId}");
}
$page++;
}

#Create a timestamp containing year, month, and day
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year+=1900;
$mon++;
$timestamp = sprintf("%4d%02d%02d",$year,$mon,$mday);

#Dump the results of the unity query to a file
open(UNITY,">/usr/scripts/unityLDAP/$timestamp-unity.csv");
for(@userdata)
{
print UNITY "$_\n";
}
close(UNITY);



(This post was edited by gentoobob on Mar 18, 2018, 10:29 AM)


Zhris
Enthusiast

Mar 18, 2018, 4:04 AM

Post #2 of 4 (508 views)
Re: [gentoobob] Script stopped working... [In reply to] Can't Post

Hi,

LWP::UserAgent's request method returns a HTTP::Response object. I don't believe theres any overloading in place and you'll have to manually fetch the content.


Code
$content = $ua->request($req); 

my $response= $ua->request( $req );
die $response->status_line unless $response->is_success;
my $content = $response->decoded_content;



If you look at LWP::Simple's source code, its get method is as follows:


Code
sub get ($) 
{
my $response = $ua->get(shift);
return $response->decoded_content if $response->is_success;
return undef;
}


Chris


gentoobob
New User

Mar 18, 2018, 10:28 AM

Post #3 of 4 (499 views)
Re: [Zhris] Script stopped working... [In reply to] Can't Post

That worked! Thank you so much. I kind of figured it was a decode issue or something had changed in one of the modules over the years. I was even trying to use Data::Dumper and was have no success. I just got lost. 2 weeks of long hours trying to figure this thing out. I greatly appreciate it. If work has me look at another Perl script I might have to buy a Perl book. lol. Thanks again!


Zhris
Enthusiast

Mar 18, 2018, 11:47 AM

Post #4 of 4 (494 views)
Re: [gentoobob] Script stopped working... [In reply to] Can't Post

Hi,

You're very welcome. We've all been there, a fresh pair of eyes is often all it takes.

Welcome to the forum and goodluck.

Chris

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives