Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1175
  • Last Modified:

Perl script - output to csv file

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
dannygonzalez09
Asked:
dannygonzalez09
  • 5
  • 4
1 Solution
 
wilcoxonCommented:
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
 
dannygonzalez09Author Commented:
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
 
wilcoxonCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
dannygonzalez09Author Commented:
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
 
wilcoxonCommented:
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
 
dannygonzalez09Author Commented:
Same issue, it just hangs doing nothing except for creating the file
0
 
wilcoxonCommented:
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
 
dannygonzalez09Author Commented:
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
 
wilcoxonCommented:
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now