lib/knife/server/ssh.rb
def run_script(content)
        user_switch = ""

        unless @user == "root"
          user_switch = USER_SWITCH_COMMAND
        end

        wrapper = <<-EOF
        if [ -e /dev/fd/0 ]
        then
          #{user_switch} /bin/sh /dev/fd/0
        elif [ -e /dev/stdin ]
        then
          #{user_switch} /bin/sh /dev/stdin
        else
          echo "Cannot find method of communicating with the shell via stdin"
          exit 1
        fi
        EOF

        result = ""
        exit_status = nil

        Net::SSH.start(@host, @user, @options) do |ssh|
          ssh.open_channel do |ch|
            ch.on_open_failed do |ch, code, desc|
              raise "Connection Error to #{ip}: #{desc}"
            end

            ch.exec(wrapper) do |channel, type, data|
              # spit out the shell script and close stdin so sh can do its magic.
              channel.send_data(content)
              channel.eof!

              # then we just wait for sweet, sweet output.
              channel.on_data do |ch2, data|
                result << data
              end

              channel.on_request("exit-status") do |ch2, data|
                exit_status = data.read_long
              end
            end

            ch.wait
          end

          ssh.loop
        end

        return result, exit_status
      end
# runs a script on the target host by passing it to the stdin of a sh
# process. returns stdout and the exit status. does not care about stderr.
runs a script on the target host by passing it to the stdin of a sh
process. returns stdout and the exit status. does not care about stderr.
runs a script on the target host by passing it to the stdin of a sh process . returns stdout and the exit status . does not care about stderr .
def run_script(content)
        user_switch = ""
        unless @user == "root"
          user_switch = USER_SWITCH_COMMAND
        end
        wrapper = <<-EOF
        if [ -e /dev/fd/0 ]
        then
          #{user_switch} /bin/sh /dev/fd/0
        elif [ -e /dev/stdin ]
        then
          #{user_switch} /bin/sh /dev/stdin
        else
          echo "Cannot find method of communicating with the shell via stdin"
          exit 1
        fi
        EOF
        result = ""
        exit_status = nil
        Net::SSH.start(@host, @user, @options) do |ssh|
          ssh.open_channel do |ch|
            ch.on_open_failed do |ch, code, desc|
              raise "Connection Error to #{ip}: #{desc}"
            end
            ch.exec(wrapper) do |channel, type, data|
              channel.send_data(content)
              channel.eof!
              channel.on_data do |ch2, data|
                result << data
              end
              channel.on_request("exit-status") do |ch2, data|
                exit_status = data.read_long
              end
            end
            ch.wait
          end
          ssh.loop
        end
        return result, exit_status
      end
def run_script ( content ) user_switch = "" unless @user == "root" user_switch = USER_SWITCH_COMMAND end wrapper = <<-EOF 
        if [ -e /dev/fd/0 ]
        then
           #{ user_switch }  /bin/sh /dev/fd/0
        elif [ -e /dev/stdin ]
        then
           #{ user_switch }  /bin/sh /dev/stdin
        else
          echo "Cannot find method of communicating with the shell via stdin"
          exit 1
        fi
         EOF result = "" exit_status = nil Net :: SSH . start ( @host , @user , @options ) do | ssh | ssh . open_channel do | ch | ch . on_open_failed do | ch , code , desc | raise "Connection Error to #{ip}: #{desc}" end ch . exec ( wrapper ) do | channel , type , data | channel . send_data ( content ) channel . eof! channel . on_data do | ch2 , data | result << data end channel . on_request ( "exit-status" ) do | ch2 , data | exit_status = data . read_long end end ch . wait end ssh . loop end return result , exit_status end
runs a script on the target host by passing it to the stdin of a sh
process.
runs a script on the target host by passing it to the stdin of a sh process .
# spit out the shell script and close stdin so sh can do its magic. # then we just wait for sweet, sweet output.
content