Lucas Theisen > IPC-Open3-Callback-1.01 > IPC::Open3::Callback::Command



Annotate this POD


New  1
Open  1
View/Report Bugs
Module Version: 1.01   Source   Latest Release: IPC-Open3-Callback-1.19


IPC::Open3::Callback::Command - A utility class that provides subroutines for building shell command strings.


version 1.01


  use IPC::Open3::Callback::Command qw(command batch_command mkdir_command pipe_command rm_command sed_command);
  my $command = command( 'echo' ); # echo

  # ssh foo "echo"
  $command = command( 'echo', {hostname=>'foo'} ); 

  # ssh bar@foo "echo"
  $command = command( 'echo', {username=>'bar',hostname=>'foo'} ); 
  # plink -l bar foo "echo"
  $command = command( 'echo', {username=>'bar',hostname=>'foo',ssh=>'plink'} ); 
  # cd foo;cd bar
  $command = batch_command( 'cd foo', 'cd bar' ); 
  # ssh baz "cd foo;cd bar"
  $command = batch_command( 'cd foo', 'cd bar', {hostname=>'baz'} ); 
  # ssh baz "sudo cd foo;sudo cd bar"
  $command = batch_command( 'cd foo', 'cd bar', {hostname=>'baz',command_prefix=>'sudo '} ); 
  # ssh baz "mkdir -p \"foo\" \"bar\""
  $command = mkdir_command( 'foo', 'bar', {hostname=>'baz'} ); 

  # cat abc|ssh baz "dd of=def"
  $command = pipe_command( 
          'cat abc', 
          command( 'dd of=def', {hostname=>'baz'} ) 

  # ssh fred@baz "sudo -u joe rm -rf \"foo\" \"bar\""
  $command = rm_command( 'foo', 'bar', {username=>'fred',hostname=>'baz',command_prefix=>'sudo -u joe '} ); 
  # sed -e 's/foo/bar/'
  $command = sed_command( 's/foo/bar/' ); 
  # curl|sed -e 's/google/gaggle/g'|ssh fred@baz "sudo -u joe dd of=\"/tmp/\"";ssh fred@baz "sudo -u joe rm -rf \"/tmp/\"";
  $command = batch_command(
              sed_command( {replace_map=>{google=>'gaggle'}} ),
              command( 'dd of="/tmp/"', {username=>'fred',hostname=>'baz',command_prefix=>'sudo -u joe '} )
          rm_command( '/tmp/', {username=>'fred',hostname=>'baz',command_prefix=>'sudo -u joe '}) 


The subroutines exported by this module can build shell command strings that can be executed by IPC::Open3::Callback, IPC::Open3::Callback::CommandRunner, ``, system(), or even plain old open 1, 2, or 3. There is not much point to shelling out for commands locally as there is almost certainly a perl function/library capable of doing whatever you need in perl code. However, If you are designing a footprintless agent that will run commands on remote machines using existing tools (gnu/powershell/bash...) these utilities can be very helpful. All functions in this module can take a \%destination_options hash defining who/where/how to run the command.


All commands can be supplied with \%destination_options. destination_options control who/where/how to run the command. The supported options are:


The ssh command to use, defaults to ssh. You can use this to specify other commands like plink for windows or an implementation of ssh that is not in your path.


As it sounds, this is a prefix to your command. Mainly useful for using sudo. This prefix is added like this $command_prefix$command so be sure to put a space at the end of your prefix unless you want to modify the name of the command itself. For example, $command_prefix = 'sudo -u priveleged_user ';.


The username to ssh with. If using ssh, this will result in, ssh $username@$hostname but if using plink it will result in plink -l $username $hostname.


The hostname/IP of the server to run this command on. If localhost, and no username is specified, the command will not be wrapped in ssh

command( $command, \%destination_options )

This wraps the supplied command with all the destination options. If no options are supplied, $command is returned.

batch_command( $command1, $command2, ..., $commandN, \%destination_options )

This will join all the commands with a ; and apply the supplied \%destination_options to the result.

mkdir_command( $path1, $path2, ..., $pathN, \%destination_options )

Results in mkdir -p $path1 $path2 ... $pathN with the \%destination_options applied.

pipe_command( $command1, $command2, ..., $commandN, \%destination_options )

Identical to batch_command except uses \| to separate the commands instead of ;.

rm_command( $path1, $path2, ..., $pathN, \%destination_options )

Results in rm -rf $path1 $path2 ... $pathN with the \%destination_options applied. This is a VERY dangerous command and should be used with care.

sed_command( $expression1, $expression2, ..., $expressionN, \%destination_options )

Constructs a sed command


An arrayref of files to apply the sed expressions to. For use when not piping from another command.


If specified, the -i option will be supplied to sed thus modifying the file argument in place. Not useful for piping commands together, but can be useful if you copy a file to a temp directory, modify it in place, then transfer the file and delete the temp directory. It would be more secure to follow this approach when using sed to fill in passwords in config files. For example, if you wanted to use sed substitions to set passwords in a config file template and then transfer that config file to a remote server:



  use IPC::Open3::Callback::Command qw(batch_command command pipe_command sed_command);
  use IPC::Open3::Callback::CommandRunner;
  use File::Temp;
  my $temp_dir = File::Temp->newdir();
  my $temp_script_file = File::Temp->new();
          "cp /my/config/passwords.cfg $temp_dir->filename()/passwords.cfg",
              "cat $temp_dir->filename()/passwords.cfg",
              command( "dd of='/remote/config/passwords.cfg'", {hostname=>'remote_host'} ) );

A map used to construct a sed expression where the key is the match portion and the value is the replace portion. For example: {'key'=>'value'} would result in 's/key/value/g'.


Specifies a file to write the sed script to rather than using the console. This is useful for avoiding generating commands that would get executed in the console that have protected information like passwords. If passwords are issued on the console, they might show up in the command history...


Lucas Theisen (


Copyright 2013 All rights reserved.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

syntax highlighting: