Solved

Perl script - output to csv file

Posted on 2013-11-07
9
985 Views
Last Modified: 2013-12-10
Can any of you please help me getting the output of this perl script to a csv file

I'm trying to execute it using a batch file with the following cmd

C:\Perl64\bin\perl.exe "D:\Test.pl" pwd > D:\Test.csv

Its doing nothing except for creating the file and when i use the below cmd, it asks for pwd, gets the output and closes the cmd prompt

C:\Perl64\bin\perl.exe "D:\Test.pl" pwd


#!/bin/perl
use strict;
use warnings;
use HTTP::Cookies;
use LWP::UserAgent;
use HTTP::Request::Common;
use Term::ReadKey;


sub login {
  my ($login_url, $user, $pass) = @_;
  my $ua = LWP::UserAgent->new;
  $ua->ssl_opts('verify_hostname' => '0');
  my $res = $ua->request(POST $login_url, [
					    destination => '/',
					    credential_0 => $user,
					    credential_1 => $pass,
					    login => 'Log In',
					   ]);
  my $cookie_jar = HTTP::Cookies->new;
  if ($res->header('set-cookie')) {
    $cookie_jar->extract_cookies($res);
    $ua->cookie_jar($cookie_jar);
    #return $cookie_jar;
    return $ua;
  } else {
    return undef;
  }
}

sub gets {
  my $prompt = shift;
  print $prompt;
  my $s = ReadLine;
  chomp $s;
  return $s;
}


# main
my $base_url = 'https://Test.mgmt.slb.net';
ReadMode('noecho');
my $pw = gets('Password: ');
ReadMode('normal');
print "\n";
my $ua = login("${base_url}/LOGIN", 'test', $pw);
#if (!$cookie_jar) {
if (!$ua) {
  print "Bad login\n";
  exit;
}

#my $ua;
#$ua = LWP::UserAgent->new;
#$ua->ssl_opts('verify_hostname' => '0');

#$ua->cookie_jar($cookie_jar);
#my $res = $ua->get ("${base_url}/visualrf/site.xml");
#print $res->content . "\n";

my $res = $ua->get("${base_url}/nf/csv_export.csv?csv_export_uri=%2Fap_list&csv_export_list_namespace=ap_list&csv_export_list_args=ap_folder_id%3D1%26expand_all%3D1%26limit_to_down%3D1");
print $res->content . "\n";

Open in new window

0
Comment
Question by:dannygonzalez09
  • 5
  • 4
9 Comments
 
LVL 26

Expert Comment

by:wilcoxon
ID: 39632096
I don't quite follow.  Are you saying that
C:\Perl64\bin\perl.exe "D:\Test.pl" pwd

Open in new window

does the right thing and you just want it in a file instead of output to the screen?

If not, what is the above command not doing properly?  If so, then adding "> filename" should work.
0
 
LVL 5

Author Comment

by:dannygonzalez09
ID: 39632130
I'm thinking that the above command is getting me the output i need but not sure because the cmd window closes automatically as it finishes getting the data from the URL

is there a way to tell it not to close the window after pulling the data by passing any parameter?

Based on little knowledge i've on Perl,i assumed that adding this to the end of the cmd as you mentioned  "> filename" will work but it is not doing anything apart from creating the file
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 39632139
Are you running the Perl command from the command line or from a batch script or something else?  If you're not running it from the command line, try that and post what output you are getting (or at least if it is the output you are expecting and want in the file).
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 5

Author Comment

by:dannygonzalez09
ID: 39632217
I've been using bat file but just tried it using a cmd line ... it prompted me for the pwd and returned the expected output

name, datetime, value, Zone etc

i ran it again this time to output the data to a file ..it did create the file but not sure its doing anything else...looks like its hung up
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 39632419
That's very strange.  If it works from the command line then it should work from the command line with a file redirect.

As an alternative, you could write to a file within the script...
#!/bin/perl
use strict;
use warnings;
use HTTP::Cookies;
use LWP::UserAgent;
use HTTP::Request::Common;
use Term::ReadKey;

# hack to redirect STDOUT to a file (I think)
open STDOUT, ">Test.csv" or die "could not write Test.csv: $!";

sub login {
  my ($login_url, $user, $pass) = @_;
  my $ua = LWP::UserAgent->new;
  $ua->ssl_opts('verify_hostname' => '0');
  my $res = $ua->request(POST $login_url, [
					    destination => '/',
					    credential_0 => $user,
					    credential_1 => $pass,
					    login => 'Log In',
					   ]);
  my $cookie_jar = HTTP::Cookies->new;
  if ($res->header('set-cookie')) {
    $cookie_jar->extract_cookies($res);
    $ua->cookie_jar($cookie_jar);
    #return $cookie_jar;
    return $ua;
  } else {
    return undef;
  }
}

sub gets {
  my $prompt = shift;
  print $prompt;
  my $s = ReadLine;
  chomp $s;
  return $s;
}


# main
my $base_url = 'https://Test.mgmt.slb.net';
ReadMode('noecho');
my $pw = gets('Password: ');
ReadMode('normal');
print "\n";
my $ua = login("${base_url}/LOGIN", 'test', $pw);
#if (!$cookie_jar) {
if (!$ua) {
  print "Bad login\n";
  exit;
}

#my $ua;
#$ua = LWP::UserAgent->new;
#$ua->ssl_opts('verify_hostname' => '0');

#$ua->cookie_jar($cookie_jar);
#my $res = $ua->get ("${base_url}/visualrf/site.xml");
#print $res->content . "\n";

my $res = $ua->get("${base_url}/nf/csv_export.csv?csv_export_uri=%2Fap_list&csv_export_list_namespace=ap_list&csv_export_list_args=ap_folder_id%3D1%26expand_all%3D1%26limit_to_down%3D1");
print $res->content . "\n";

Open in new window

0
 
LVL 5

Author Comment

by:dannygonzalez09
ID: 39634111
Same issue, it just hangs doing nothing except for creating the file
0
 
LVL 26

Accepted Solution

by:
wilcoxon earned 350 total points
ID: 39634915
But it works fine without the change and outputs the information you expect?

If so, I'm pretty sure it's not a perl problem.  It seems like there's some sort of issue file system issue (odd permissions?).
0
 
LVL 5

Author Comment

by:dannygonzalez09
ID: 39634943
Yeah, that's what i was thinking..its probably unable to write to the file but its not even prompting for the pwd when i run the command to output the data to the file.. so i'm not sure what really is going on
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 39634953
The not prompting is likely because of the STDOUT redirect though I would expect that sort of prompt to go to STDERR.  Try moving the "open STDOUT" from where it is now to just before line 63 (just before the $ua->get).
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question