Link to home
Start Free TrialLog in
Avatar of dannygonzalez09
dannygonzalez09

asked on

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

Avatar of wilcoxon
wilcoxon
Flag of United States of America image

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.
Avatar of dannygonzalez09
dannygonzalez09

ASKER

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
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).
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
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

Same issue, it just hangs doing nothing except for creating the file
ASKER CERTIFIED SOLUTION
Avatar of wilcoxon
wilcoxon
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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).