0

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

11
  • 2
    1) Have you tried from within Ruby doing `ssh host password command`? This might help narrow down the problem. 2) I recommend removing the ssh.close command; the ssh session will be closed automatically when the block terminates. (File.open with a block works this way too.) 3) You may need to use Shellwords.escape on any strings you pass to a shell. 4) You should not need the require 'rubygems'. 5) Are you absolutely sure that both cases (the success and the failure) are running on the same host? Commented Feb 13, 2018 at 13:29
  • 1
    With regards to (1) above, the problem may be to do with the environment in which SSH is running the command. Where is the PHP executable on the host machine? Is that folder in the $PATH when you ssh to the machine? Commented Feb 13, 2018 at 13:43
  • @KeithBennett 1) I am not sure what you're meaning. I am just learning Ruby, but I have some experience with Javascript and PHP. 2) Okay, ssh.close is removed. 3) I tried Shellwords.escape but it returns zsh:1: no such file or directory: ... 4) Thanks, removed too 5) Yes, absolutely. I copy and pasted the command and it runs. Commented Feb 13, 2018 at 14:50
  • @TomLord Can you specify what you're meaning exactly? Like I stated above, the snippet is running. The shortened version in the code example is pointing actually to ~/.php/abc.phar in the real file. Commented Feb 13, 2018 at 14:50
  • @D.Ne When you execute php on the command line, what executable is that actually running? Try: which php, too see. Now, run: echo $PATH, you should also see that folder in which the php executable lives. Now, try doing to above via the ruby script. What's the difference? Commented Feb 13, 2018 at 15:03

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.