Perl script running multiple commands at once


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl script running multiple commands at once
# 1  
Old 04-02-2015
Perl script running multiple commands at once

Hi All,
I have put a perl script together to go and collect some information from multiple nodes/endpoints. The script works absolutly fine however I want to make it quicker.

You will see in the below that my script calls an expect script called ssh_run_cmd2.exp followed by the IP of the node in question. Once that has finished it moves on to the next node.

Is there a way of getting it to call all the commands such as the below
Code:
ssh_run_cmd2.exp xxx.xxx.xxx.xxx $log "show association peer-address $ip

to trigger at once rather than waiting for each one to finsih.

None of the commands are dependant on the previous one.

Code:
        for (`./ssh_run_cmd2.exp xxx.xxx.xxx.xxx $log "show association peer-address $ip"`) {
                if (/^\s+ID       : (\S+)/) {
                        $ID = $1;
                        $peerfound = 'YES';
                } elsif (/(No matching peer)\S+/) {
                        $peerfoundoutcome = "$1"
                } elsif (/\S{7}(\S+)\s/) {
                        $hostname = $1;
                        }
                }
        for (`./ssh_run_cmd2.exp xxx.xxx.xxx.xxx $log "show association peer-address $ip"`) {
                if (/^\s+ID       : (\S+)/) {
                        $ID1 = $1;
                        $peerfound1 = 'YES';
                } elsif (/(No matching peer)\S+/) {
                        $peerfoundoutcome1 = "$1"
                } elsif (/\S{7}(\S+)\s/) {
                        $hostname1 = $1;
                        }
                }

Thanks in advance
# 2  
Old 04-02-2015
Hi,

i dont know it is that what you want, but easy way to do it is:
you can try run everything in background with outputs to files (for exmpl first line> file all other lines >> file last line >> end) and then create loop to collect info from created files and if you want loop till all files got end
# 3  
Old 04-02-2015
Hi bacarrdy, Thanks for responding. Im not sure i understand your comment. would you mind explain how exactly I would do what your mentioned ?
# 4  
Old 04-02-2015
you can run each command ./ssh_run_cmd2.exp $xxx.xxx.xxx.xxx $log > $xxx.xxx.xxx.xxx & in background and then read outputs from $xxx.xxx.xxx.xxx files

so then all your commands (./ssh_run_cmd2.exp) will run in one time and they start to create some files and for thous files you can run another loop to read them
# 5  
Old 04-02-2015
not sure if i can do that as there multiple arguements defined on command line which require to remain in place.

basically i need this to run
./ssh_run_cmd2.exp xxx.xxx.xxx.xxx $log "show association peer-address $ip

along with this (this IP's will be different)
./ssh_run_cmd2.exp xxx.xxx.xxx.xxx $log "show association peer-address $ip

but they need to run simultaniously rather than waiting for one to complete and then the other.
# 6  
Old 04-02-2015
You could open both at once, perhaps, using open() instead of backticks. How about this:

Code:
open(CMD1, "./ssh_run_cmd2.exp xxx.xxx.xxx.xxx $log "show association peer-address $ip", "-|");

open(CMD2, "./ssh_run_cmd2.exp xxx.xxx.xxx.xxx $log "show association peer-address $ip", "-|");

while($line=<CMD1>) { chomp($line); ... }
while($line=<CMD2>) { chomp($line); ... }

close(CMD1);
close(CMD2);

It doesn't read from them from the same time, but if the amount of data is smaller than 64 kilobytes, buffering should let the network communication happen simultaneously anyway. When you finish one, data may already be waiting for you on the second.

The chomp is to remove linefeeds and such, since unlike backticks open() doesn't do that for you. You just get a stream.

Running a handful of these at once is fine. Trying to run hundreds at once is not recommended.
This User Gave Thanks to Corona688 For This Post:
# 7  
Old 04-02-2015
Thanks Corona688
where would I put the regex that you can see from my initial code based on your previous advice.

You advised to try the below however where would i put my expressions shown in the 2nd code box below (this regex is just for the first command one also exists for the second.

Code:
open(CMD1, "./ssh_run_cmd2.exp xxx.xxx.xxx.xxx $log "show association peer-address $ip", "-|");

open(CMD2, "./ssh_run_cmd2.exp xxx.xxx.xxx.xxx $log "show association peer-address $ip", "-|");

while($line=<CMD1>) { chomp($line); ... }
while($line=<CMD2>) { chomp($line); ... }

close(CMD1);
close(CMD2);

Code:
{
                if (/^\s+ID       : (\S+)/) {
                        $ID = $1;
                        $peerfound = 'YES';
                } elsif (/(No matching peer)\S+/) {
                        $peerfoundoutcome = "$1"
                } elsif (/\S{7}(\S+)\s/) {
                        $hostname = $1;
                        }
                }



Thanks again

Last edited by mutley2202; 04-03-2015 at 04:47 AM.. Reason: more info added
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check status of long running multiple curl commands in shell script

I am working on script. it reads a file which contains multiple lines Ex; curl --write-out %{http_code} --silent --output /dev/null http://hostname:port/input=1 curl --write-out %{http_code} --silent --output /dev/null http://hostname:port/input=2 curl --write-out %{http_code} --silent ... (2 Replies)
Discussion started by: oraclermanpt
2 Replies

2. Shell Programming and Scripting

Issue with running multiple commands withing su command

RHEL 6.2/Bash shell root user will be executing the below script. It switches to oracle user and expect to do the following things A. Source the environment variables for BATGPRD Database (the file used for sourcing is shown below after the script) B. Shutdown the DB from sqlplus -- The... (13 Replies)
Discussion started by: omega3
13 Replies

3. Shell Programming and Scripting

Get multiple values from an xml file using one of the following commands or together awk/perl/script

Hello, I have a requirement to extract the value from multiple xml node and print out the values to new file to compare. Would be done using either awk/perl or some unix script. For example sample input file: ..... ..... <factories xmi:type="resources.jdbc:DataSource"... (2 Replies)
Discussion started by: slbmind
2 Replies

4. Shell Programming and Scripting

Running multiple commands stored as a semi-colon separated string

Hi, Is there a way in Korn Shell that I can run multiple commands stored as a semi-colon separated string, e.g., # vs="echo a; echo b;" # $vs a; echo b; I want to be able to store commands in a variable, then run all of it once and pipe the whole output to another program without using... (2 Replies)
Discussion started by: svhyd
2 Replies

5. Programming

Running Multiple Unix commands in qx

Hi All, Is there anything wrong with below syntax? qx {perldoc -v ModuleName.pm | grep -i Description } BTW, this question is related to Perl. Thanks. (3 Replies)
Discussion started by: jal_capri
3 Replies

6. Shell Programming and Scripting

Running unix commands in a perl script

Executing two unix commads via perl script one after another e.g: make clean bsub -i -q short make have tried using exec but the second command doesnt executes (1 Reply)
Discussion started by: rajroshan
1 Replies

7. Shell Programming and Scripting

Running multiple unix commands in a single script

Hi, I would like to write a script with include more than 6 unix commands. my script like below: echo " script started" ls -ld bdf | grep "rama" tail -10 log.txt ... .. ... now, i want to run above unix commands one by one. example: first the ls -ld command will be... (3 Replies)
Discussion started by: koti_rama
3 Replies

8. UNIX for Dummies Questions & Answers

running a simple script file with multiple commands

I'm trying to run a script file with multiple commands that I would normally type into the command line. The commands are: #!/bin/bash diff Test1.o0 /usr3/ronelso4/Desktop/verificationKPC/Test1.o0 > differences2 diff Test1a.o0 /usr3/ronelso4/Desktop/verificationKPC/Test1a.o0 >> differences2... (1 Reply)
Discussion started by: knelson
1 Replies

9. Shell Programming and Scripting

Running unix commands through perl

Hi all, In the directory '/temp/chris' the following files exist: chris.tar, chris.txt What i am trying to do is to assign the 'chris.tar' filename in an argument through perl, in order to do that i use the system command: $file=system("ls /temp/chris/*.tmp), but in the '$file' the exit... (2 Replies)
Discussion started by: chriss_58
2 Replies

10. Shell Programming and Scripting

[PERL] Running unix commands within Perl Scripts

I understand that in order to run basic unix commands I would normally type at the prompt, I would have to use the following format system(ls -l); or exec(ls -l); But when I actually try to use the command, the script fails to compile and keeps telling me there is an error with this line. ... (1 Reply)
Discussion started by: userix
1 Replies
Login or Register to Ask a Question