Current location: Hot Scripts Forums » Programming Languages » Perl » Execute a perl script from another perl script.


Execute a perl script from another perl script.

Reply
  #1 (permalink)  
Old 08-31-05, 12:37 PM
jkarimi jkarimi is offline
New Member
 
Join Date: Nov 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Execute a perl script from another perl script.

How could I execute the following perl script with its command line options from within another perl script?

cfgmaker --global "Workdir: /var/www/mrtg/mrtg-data"\
--global "Options[_]: growright,bits" \
--ifref=descr \
--$template_type-template=$template_name \
--if-filter=$filter \
$community@$name > /var/www/mrtg/mrtg-configs/$directory/$name.cfg"

I currently run this using a shell script, but would like to migrate it over to perl. Any help would be greatly appreciated.

Thanks
James
Reply With Quote
  #2 (permalink)  
Old 09-04-05, 12:46 AM
Chas Chas is offline
Coding Addict
 
Join Date: Oct 2003
Location: California
Posts: 359
Thanks: 0
Thanked 0 Times in 0 Posts
Hi James,

You can call it using the system function[1] or using the qx or backtick operators[2].

~Charlie

[1] http://perldoc.perl.org/functions/system.html
[2] http://perldoc.perl.org/perlop.html#qx%2fSTRING%2f
Reply With Quote
  #3 (permalink)  
Old 09-05-05, 10:50 PM
jkarimi jkarimi is offline
New Member
 
Join Date: Nov 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
I have been able to get the command to execute but it will not execute against all devices I have in the mysql database, it only runs against the first device. Can anyone please take a look as I know I must have made a mistake somewhere.

Here is the script


#!/usr/bin/perl
use warnings;
use strict;

use DBI;

my $dsn = 'dbi:mysql:mrtg:localhost:3306';
my $user = 'mrtg';
my $pass = 'mrtg';
my $dbh = DBI->connect($dsn, $user, $pass) or die "Cannot connect to the DB: $DBI::errstr\n";

my $sth = $dbh->prepare("select name, community, global1, global2, ifref, ifdesc, option1, option2, option3, option4, option5, template_type, template_name, filter, snmp_port, snmp_timeout, snmp_retries, snmp_backoff, snmp_version, directory from cfgmaker");
$sth->execute or die "Database select failed:" . $sth->errstr;

while (my $row = $sth->fetchrow_arrayref) {
perform_collection(@$row);
}

sub perform_collection {
my ($name, $community, $global1, $global2, $ifref, $ifdesc, $option1, $option2, $option3, $option4, $option5, $template_type, $template_name, $filter, $snmp_port, $snmp_timout, $snmp_retries, $snmp_backoff, $snmp_version, $directory) = @_;


do `cfgmaker $global1 $global2 --ifref=$ifref --ifdesc=$ifdesc $option1 $option2 $option3 $option4 $option5 --$template_type-template=$template_name '--if-filter=$filter' $community\@$name\:$snmp_port:$snmp_timout:$snmp_r etries:$snmp_backoff:$snmp_version > /var/www/mrtg/mrtg-configs/$directory/$name.cfg`
}

Thanks
James
Reply With Quote
  #4 (permalink)  
Old 09-06-05, 11:42 PM
Chas Chas is offline
Coding Addict
 
Join Date: Oct 2003
Location: California
Posts: 359
Thanks: 0
Thanked 0 Times in 0 Posts
Try this:

Code:
sub perform_collection {
  my ($name, $community, $global1, $global2, $ifref, $ifdesc, $option1, $option2, $option3, $option4, $option5, $template_type, $template_name, $filter, $snmp_port, $snmp_timout, $snmp_retries, $snmp_backoff, $snmp_version, $directory) = @_;

  my $output = `cfgmaker $global1 $global2 --ifref=$ifref --ifdesc=$ifdesc $option1 $option2 $option3 $option4 $option5 --$template_type-template=$template_name '--if-filter=$filter' $community\@$name\:$snmp_port:$snmp_timout:$snmp_r etries:$snmp_backoff:$snmp_version > /var/www/mrtg/mrtg-configs/$directory/$name.cfg`;
  
  return $output;
}
If you still get hung up on the first row try sending a string containing the command to stderr and see what's actually getting executed.

~Charlie
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with my largest perl script. Grabbing data from a file. Sunnmann Perl 2 04-23-08 03:27 AM
2 profitable script sites for sale cms-master.com General Advertisements 3 07-03-07 10:17 AM
Long Executed Perl Script not terminating till completion ! learner Perl 7 09-04-05 12:39 AM
converting perl script to php help!! macruddace Perl 1 04-14-05 02:38 PM
Is there any integrity of script rankings? webmaster@atmanager.com Hot Scripts Forum Questions, Suggestions and Feedback 17 08-06-04 12:12 AM


All times are GMT -5. The time now is 04:07 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.