zoukankan      html  css  js  c++  java
  • Rex::Misc::ShellBlock 内容

    # (c) Jan Gehring <jan.gehring@gmail.com>
    # 
    # vim: set ts=3 sw=3 tw=0:
    # vim: set expandtab:
       
    package Rex::Misc::ShellBlock;
    
    use strict;
    use warnings;
    
    use Rex::Commands;
    use Rex::Commands::Fs;
    use Rex::Commands::File;
    use Rex::Commands::Run;
    
    require Exporter;
    
    use vars qw(@EXPORT);
    @EXPORT = qw(shell_block);
    use base qw(Exporter);
    
    sub shell_block {
    
       my ($shebang, $code) = @_;
    
       if(! $code) {
          $code = $shebang;
          $shebang = "#!/bin/bash";
       }
    
       if($shebang !~ m/^#!//) {
          $shebang = "#!$shebang";
       }
    
       my @lines = split(/
    /, $code);
       if($lines[0] !~ m/^#!//) {
          # shebang not there, so add /bin/bash for default
          unshift(@lines, $shebang);
       }
    
       my $rnd_file = "/tmp/" . get_random(8, 'a' .. 'z') . ".tmp";
    
       file $rnd_file,
          content => join("
    ", @lines),
          mode => 755;
    
       my $ret = run "$rnd_file 2>&1";
    
       my $ret_code = $?;
    
       unlink $rnd_file;
    
       $? = $ret_code;
    
       return $ret;
    
    }
    
    1;
    
    =pod
    =head1 NAME
    Rex::Misc::ShellBlock - Module to execute a shell block.
    =head1 DESCRIPTION
    This module exports a function called I<shell_block>. This function will upload your shell script to the remote system and executes it. Returning its output as a string.
    =head1 EXPORTED FUNCTIONS
    =over 4
    =item shell_block($code)
    This function will add a default shebang of '#!/bin/bash' to your code if no shebang is found and return its output.
     my $ret = shell_block <<EOF;
     echo "hi"
     EOF
    =item shell_block($shebang, $code)
    This function will add $shebang to your code and return its output.
     my $ret = shell_block "/bin/sh", <<EOF;
     echo "hi"
     EOF
    =back
    =head1 USAGE
     use Rex::Misc::ShellBlock;
        
     task "myexec", sub {
        shell_block <<EOF;
     echo "hi"
     EOF
      
     };

  • 相关阅读:
    Struts2的OGNL的用法
    详解DataTable DataSet以及与数据库的关系
    ModBus 协议
    STM32串口接收中断溢出问题解决
    STM32 GD32 时钟设置
    STM32 中断
    STM32 中断系统
    STM32中断系统(NVIC和EXTI)
    STM32中断系统
    GD32E230 GPIO 时钟
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349748.html
Copyright © 2011-2022 走看看