Groovy script to download script and run it with parameters - perl

Need help with groovy script.
We have a perl script in github. We need to download and run the perl-script inside groovy script (jenkins) and also pass parameters to the script and get the output.
Example, Perl script in github accepts parameter "item1" "item2", using curl we get the raw format of the script.
def command = "curl -s https://github.com/raw/script.pl?token=%3D"
def proc = command.execute() | "perl /dev/stdin $item1 $item2".execute()
proc.waitFor()
def roles = []
roles = "${proc.in.text}" .eachLine { line ->
roles << line
}
return roles
Above script does not return expected result. Please help.
Thanks in advance..

Thanks Guys. Found it. Actually following does work..
def command = "curl -s https://github.com/raw/script.pl?token=%3D"
def proc = command.execute() | "perl /dev/stdin $item1 $item2".execute()
proc.waitFor()
def roles = []
roles = "${proc.in.text}" .eachLine { line ->
roles << line
}
return roles

Related

How do I input paramters into a Jenkins perl script from command line?

Background
I am running a Jenkns Job, called Job A, that feeds its build parameters into a perl script, called ScriptA.pl, in the following format below:
#Check the input params
my $PARAM1 = $ENV{ "PARAM1" };
my $PARAM2 = $ENV{ "PARAM2" };
.....more params fed in the same way
if ( $PARAM1 eq "" ) {
print "PARAM1 is a required parameter.\n";
exit 1;
}
if ( $PARAM2 eq "" ) {
print "PARAM2 is a required parameter.\n";
exit 1;
}
.....more param checks done in the same way
###Script then runs a bunch of execution statements####
Problem
I am trying to run this script from Linux command line in cshell in the following way to test that the execution component works:
/% ScriptA.pl jetFuel steelBeams
And it thinks that no parameters have been entered, since this error is returned
PARAM1 is a required parameter.
Question
How do I properly input Jenkins parameters into a Jenkins script from command line?
If you can't modify the Perl script so it reads command-line parameters (which would improve its general usability to boot), maybe create a simple wrapper script:
#/bin/sh
env PARAM1="$1" PARAM2="$2" perl ScriptA.pl
(Yes, sh. Nobody should use csh for anything in 2019.)

Perl to exec a program with arguments containing "#"

Am a newbie in Perl and need help with a small problem
Situation:
I have to execute a command line program through perl.
The arguments to this command line are email addresses
These email addresses are passed to me through another module.
Problem:
I have written the code to create the argument list from these email addresses but am having problem in running exec().
NOTE: If I pass hardcoded strings with escaped "#" character to the exec() as command args,it works perfectly.
Sub creating cmd args map
sub create_cmd_args {
my($self, $msginfo) = #_;
my #gd_args_msg = ('--op1');
my $mf = $msginfo->sender_smtp;
$mf =~ s/#/\\#/ig; ## Tried escaping #, incorrect results.
push #gd_args_msg, '-f="'.$mf.'"';
for my $r (#{$msginfo->per_recip_data}) {
my $recip = $r->recip_addr_smtp;
$recip =~ s/#/\\#/ig; ## Tried escaping #, incorrect results.
push #gd_args_msg, '-r="'.($recip).'"';
}
return #gd_args_msg;
}
Sub that uses this args map to exec the program
sub check {
my($self, $msginfo) = #_;
my $cmd = $g_command;
my #cmd_args = create_cmd_args($self, $msginfo);
exec($cmd, #cmd_args); ### ******* fails here
}
Sample run:
INPUT:
sender_smtp: <ashish#isthisreal.com>
receiver_smtp: <areyouarealperson#somedomain.com>
Could someone please guide me what is wrong here?
As an argument to a command in the shell,
-f="<ashish#isthisreal.com>"
causes the the string
-f=<ashish#isthisreal.com>
to be passed to the program. Your program passes
-f="<ashish\#isthisreal.com>"
to the program. The problem isn't the #; the problem is the " and \ you are adding.
my $mf = $msginfo->sender_smtp;
push #gd_args_msg, "-f=$mf"; # Assuming $mf is <ashish#isthisreal.com>
If you look at the post at Trying to convert Perl to PHP and the code within the md5sum implementation that calls the command line you will see an approach that will save you from needing to worry about escaping characters.

Expect script output at Perl console

I have a exp script -script.exp to enter a name and password. it calls test.sh file like below.
set timeout -1
spawn ./test.sh -create
match_max 100000
expect -exact "Enter the name: "
send -- "abcd\r"
expect -exact "\r
Please confirm password: "
send -- "xxy\r"
expect -exact "\r
expect eof
It gives me a output at expect window -
"Successfully entered the name"
if some issue there it throws exception or error message.
I run this expect script in a Perl file-- like below
$cmd="expect script.exp";
system($cmd);
$outputfromexp=?
I need a output of fail or passed status of exp at Perl console after running the script.
How can i do this? Please help me.
I tried calling as suggested in my Perl script--
use strict;
use warnings;
sub sysrun {
my ($command) ="expect script.exp";
my $ret_code;
$ret_code = system("$command");
if ( $ret_code == 0 ) {
# Job suceeded
$ret_code = 1;
}
else {
# Job Failed
$ret_code = 0;
}
return ($ret_code);
}
my $ret_code=sysrun();
print "reuturmsfg- $ret_code\n";
But its printing nothing-- just reuturmsfg-.
I made the changes-
my $ret_code=sysrun();
it gives me 0 and 1 return code.
If I understand the question correctly, to get the return code you want to do:
system($cmd);
my $ret_code = $?;
To get STDOUT output from the execution:
my #out = `$cmd`;
my $ret_code = $?;
I strongly suggest using strict and warnings in your code.

Using Bitcoin's sendmany function with perl

I have been struggling with this one for a while and will be grateful if someone could offer a solution. Basically, I am trying to use bitcoin's 'sendmany' function (https://en.bitcoin.it/wiki/Original_Bitcoin_client/API_calls_list) to send mass payment with a perl script. I am running bitcoind on a vps and other functions are working fine but sendmany isn't. This is the code that I have :
use Finance::Bitcoin::API;
$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0;
use Data::Dumper;
my %recipients = ("ADDRESS1" => sprintf("%.8f",0.00005460)+0, "ADDRESS2" => sprintf("%.8f",0.00005460)+0);
my $uri = "https://$rpcuser:$rpcpass\#$rpcip:8332/";
my $api = Finance::Bitcoin::API->new( endpoint => $uri );
my $action = $api->call('sendmany','',%recipients);
if ($api->error)
{
print Dumper($api->error->{message})."\n";
}
else
{
print Dumper($action)."\n";
}
I am able to send single payments using the 'sendtoaddress' function and I am able to use the sendmny function directly in the vps that is running bitcoind by executing it from the shell, but it fails when I try it using the perl script above. There is no error message, I just get the instructions for using sendmany from shell and using curl.
I am also open to scripts in any other language that will let me execute sendmany.
Thanks for the help.
I finally figured out the error in the above code. Replace the line my $action = $api->call('sendmany','',%recipients); with my $action = $api->call('sendmany','',\%recipients);
Basically just add a forward slash before %recipients. Hope this helps someone.

Perl Net::SNMP returns noSuchName when snmpwalk works

I am new to perl, but I am trying to write a plug-in for nagios. I have a simple get request that fails, but if I try the same request with snmpwalk it works.
My code is:
#!/usr/bin/perl -w
use strict;
use Net::SNMP;
my $host = '10.10.10.203';
my $community = 'myComm';
my $session;
my $error;
my $response = undef;
($session, $error) = Net::SNMP->session(
-hostname => $host,
-version => 2,
-community =>$community,
-port => 161,
-timeout => 20
);
my $uptimeOID = '1.3.6.1.2.1.1.3.0';
my $myOID = '1.3.6.1.4.1.7933';
if( !defined( $response = $session->get_request($myOID)))
{
if( $session->error_status == 2)
{
my $sessionError = $session->error;
print ("($sessionError) OID not supported ($myOID).\n");
}
}
else
{
print ("$response");
}
If I run this script it will fail saying noSuchName, but if a run:
snmpwalk -v 2c -c myComm 10.10.10.203 1.3.6.1.4.1.7933
I get the response I want. Does anybody know why this wont work?
If I check the uptime OID with this script it will work the way it should.
You've already identified that via the command-line you're doing a "walk" rather than a "get". If there's a specific value you want to "get" in your script, put in the full OID identifying the target.
There's something in a table record that you probably want to get at (and it seems like everything in FASTTRAKIDERAID-MIB is in fact tabular), so a simple get isn't enough. Look at the snmpwalk.pl script that comes with Net::SNMP or see if SNMP::Util can easily provide the functionality you're looking for.
Use get_next_request, not get_request. It will return the first valid oid after the one you pass it.
I found my problem. When I use snmpwalk, it will grab the whole tree and return a value. The perl module will not. It doesn't traverse the tree to the end even thought there is only one thing below it, it just says no.