D. Ne
D. Ne

Reputation: 1

Ruby: Execute php commands via SSH

I am trying to run a PHP script inside the ruby shell. While it is working perfectly if I am using the snippet directly in the ssh terminal, it is returning an error if executed with ruby:

zsh:1: command not found: php

Using this script below with commands like ls is working fine.

require 'rubygems'
require 'net/ssh'

host = "abc.de"
user = "user_xy"
pass = "user_pass"

begin
    Net::SSH.start(host, user, :password => pass) do |ssh|
        $a = ssh.exec! "cd xy_dir && php abc.phar do_this"
        ssh.close
        puts $a     
end
rescue 
    puts "Unable to connect to #{host}."
end

How can I run PHP using Net::SSH?

Thanks for your help

Upvotes: 0

Views: 83

Answers (1)

Keith Bennett
Keith Bennett

Reputation: 4970

I think the problem is not with Ruby per se, but probably with any language's SSH implementation. When using the language's ssh support to create an ssh session, it does not create a login shell (which would read initialization files such as .bashrc), but rather, a lower level interface to the machine.

Therefore, some functionality you would expect from normal shell use will be missing when connecting with Ruby's Net::SSH.

I was thinking there may be a way to get around this by calling bash -l -c "[the commands]", to force a login shell with bash's -l flag, and -c command specifier, but could not get it to work.

I did find this other SO issue whose answer discusses an awkward workaround that probably is not worth trying: ruby net-ssh login shell.

Upvotes: 1

Related Questions