Solved

Perl script - output to csv file

Posted on 2013-11-07
9
968 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
delete query using perl dbi 3 100
work on ods spreadsheet with perl in ubuntu 4 76
collecting information 2 158
.properties file to call function/method 9 61
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.

776 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