Solved

Perl script - output to csv file

Posted on 2013-11-07
9
934 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 5

Author Comment

by:dannygonzalez09
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 26

Expert Comment

by:wilcoxon
Comment Utility
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
Comment Utility
Same issue, it just hangs doing nothing except for creating the file
0
 
LVL 26

Accepted Solution

by:
wilcoxon earned 350 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now