Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Script stopped working...

by Marshall (Canon)
on Mar 18, 2018 at 06:45 UTC ( #1211177=note: print w/replies, xml ) Need Help??


in reply to Script stopped working...

Evidently HTTPS stuff changed with LWP version 6. I read Crypt-SSLeay.

I agree with NetWallah that you should be using both strict and warnings. This will spot compile and run time errors. With strictures, each variable has to be declared at first use with "my". I did some of that below to get you started. The use of a while(1) as a loop structure and burying the exit condition in the body is in my opinion a bad idea. More common would be to have the looping exit condition appear right as the loop starts. However, I didn't change that. I did clean up a bit of this $size stuff although I guess that should have been called $n_users? This C-style for loop is a bit weird, but I didn't change that either.

The first step is to clean up any "my" declarations that I might have missed and get this thing to compile cleanly and start running. We can help with any obscure error messages. Then, the next step is to make sure that you are actually getting the first page (i.e. that you ua modifications are working). I added a "die" message if $content is not defined. Stuffing an undefined value into the XML gizmo could cause the GLOB error that you are seeing - maybe. See below and un-comment the print to see the $content and give us enough of it for us to get the idea of what that is (probably don't need the whole thing). Hope this helps.

Update: See the suggestion by hippo at Re: Script stopped working.... Hippo is correct. The response is an object, not a simple scalar. I modified my efforts below with his suggestion and added in the right kind of die message showing the response->status if the req did not succeed. The code doesn't do re-tries but I don't think you need to do that. What I remember from my last LWP effort in 2016, I saw a transient error maybe every 3,000 requests and I implemented re-tries because I had to do a lot of requests. I think it is worthwhile to check if the request succeeded, but I don't recommend mucking with this code any more than necessary. Note: the right place to end the loop may not be if($size<1){} depends upon what the server does when you "run out of valid pages".

use strict; use warnings; use LWP::UserAgent; use LWP::Protocol::https; use XML::Simple; my $xml = new XML::Simple; my @userdata; my $userName = ‘user’; my $passwd = ‘password’; my $page = 1; #Get Unity subscriber data from cucxnpub one page at a time, 2000 reco +rds to a page while(1) { my $url="https://cucxnpub/vmrest/users?rowsPerPage=2000\&pageNumber= +$page"; my $ua = LWP::UserAgent->new(ssl_opts => { SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE, verify_hostname => 0,}); my $header = HTTP::Headers->new; my $req = HTTP::Request->new(GET => $url); $req->authorization_basic($userName,$passwd); $req->content_type('application/xml'); # changed this line in update re: [hippo]'s post # this ain't right #my $content = $ua->request($req) or die "unable to get content for +$url $!"; ### need defined content my $response = $ua->request ($req); #response is an object if ( !($response->is_success)) { die $response->status_line; } my $content = $response->decoded_content; my $data = $xml->XMLin ($content); #print "$content\n"; ### un-comment for to see print of xml documen +t #exit; my $size = @{$data->{User}}; #If we don't get at least one user end the loop if($size < 1) { last; ### <- THIS IS HOW LOOP ENDS } #Build the userdata array, each entry contains "username,extension" my $start = (($page-1) * 2000); my $i; for($i=$start;$i<=$start + $size - 1;$i++) { push(@userdata,"$data->{User}->[$i-$start]->{Alias},$data->{User}- +>[$i-$start]->{DtmfAccessId}"); } $page++; } #Create a timestamp containing year, month, and day my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(t +ime); $year+=1900; $mon++; my $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);

Replies are listed 'Best First'.
Re^2: Script stopped working...
by gentoobob (Novice) on Mar 18, 2018 at 17:25 UTC

    Yes. Like I was telling NetWalla I do not want to run bad code, especially when I'm purely amateur at Perl scripting. I will definitely look over everything in more detail Monday morning. I really appreciate your help and will be looking into more resources and learning tools. I kind of wondered if something had changed in one of the modules. Makes sense. Thank you so much again. I've been beating my head in for about 2 weeks and a lot of hours. haha

      As compared with the average complete amatuer question, you get an A+++.
      Your question was clear and it was immediately obvious to me and others that you have some brain cells that are working hard. PerlMonks is a great place to learn, but this only works if you also work. You are working hard and I figure that the result will be great!

      I think you are very close to "working code".
      There are some fine points to be dealt with and some obvious things like "don't push when you can print!". The code creates an array of strings and then later the code loops over that array to print the strings. No need to do that.. just barf it out right away (e.g. print)

        lol. Well thanks! I've taken programming classes in college (Java and VB), I've also read several Python books on my own and I do write small scripts for automation in Python since I'm a Network Engineer but nothing as complex as this Perl script you saw from an ex-coworker who no longer works there. My python scripts are pretty much "SSH into a switches or routers, get info or push commands". Maybe read from a file, etc. Anyways...diving into Perl was a little odd for me due to the ->, =>, @ symbols. Just trying to piece it together. I will definitely be back on this site to read the learning documents. I appreciate it.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1211177]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2020-12-05 05:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How often do you use taint mode?





    Results (63 votes). Check out past polls.

    Notices?