Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with OpenID
Advertise on

In this Discussion



ZenZen Member
edited July 2012 in General

So I'm intrigued by this, since I haven't been able to find any documentation on it (that leads to anything, that is.)

If I execute a direct command via ssh2_exec which will give me a reply, and I want to then take that reply ($data) and store it in PHP as a session or anything else (echo it, do whatever.) - it will work perfectly.

It will take the received $data from the command - for example (ls | grep Hancock) - and give it to me, works fine.

However, if I am to write a bash script that has this command inside, just the same, and then execute this bash script via ssh2_exec - even though the bash script gives THE EXACT same output, the $data variable is empty O_o

I've tried ./bash.script - sh bash.script - bash bash.script and a few other ways of executing the script, non make any difference.

Any advice? I know this isn't some PHP forum but I thought I might as well write something up real quick and give it a shot before running off to stackoverflow and looking like an idiot (I'm relatively new to PHP)

Executing a normal command is fine for now, but for reasons of complexity I like to do everything via variables passed onto a bash script.



  • Are you sure you cannot use curl or wget?

  • ZenZen Member

    The command you mean? I don't need to, right now what it does is check the progress of a torrent, greps the output percentage, stores it in $data, I grab that, pass it on to another script as a session, etc. Works fine via command, but when I try to put the exact same command in a bash script and execute it just the same, it doesn't store anything in $data. Which makes no sense to me.

  • JarJar Member

    Have you tried executing on more than one server? Perhaps I've not spent much time with ssh2_exec, but my understanding from that little time spent is that your logic is sound if the environment is not configured in some way that prevents it.

    MXroute | DigitalOcean free $10 credit (affiliate)

  • ZenZen Member
    edited July 2012

    Yes jarland, I did this on multiple machines/os.

  • specklspeckl Member

    My guess is that you are running a php script that doesn't have the correct permissions on the file(s) that you are trying to read.

    Are you seeing any errors anywhere?

    UK VPS's 15k SAS drives -

  • MrAndroidMrAndroid Member
    edited July 2012

    You need to use a ssh2 console to do this, instead of ssh2_exec. It should also work with ssh_exec works if you use

     echo `ls`

    in the bash file.

    You of course can just output it to a file, then read the file.

    The Original Daniel.

  • ZenZen Member

    @speckl definitely not that. @MrLawoodle will try it out.

  • XeoncrossXeoncross Member
    edited July 2012

    Make sure you are using the correct function and that it is not getting stuck asking for input/feedback.

    Are you using the same binary for both tasks? If you have a PHP script being called from php-cgi or php-fpm - it might have different functions or settings enabled/disabled from the bash php-cli.

    What does your error log (or multiple error logs in this case) say?

  • krokro Member

    Lol my first vpn system used ssh2 via php. Needless to say, better ways to skin the cat :)

  • ZenZen Member


    More info please? Always looking to learn.

  • joepie91joepie91 Member
    edited July 2012

    Be sure that you're outputting the response to stdout and not stderr - stderr will break. The PHP SSH2 library isn't great. Other than that, there should not really be an issue.

    EDIT: Have you verified that the command ran successfully? You may have made a typo in the filename of the bash script, for example, and since it doesn't capture stderr properly you won't figure that out from your PHP script alone.

    Appreciate my posts/software/guides? Donate (PayPal/Flattr/Bitcoin): | #lowendbox

  • krokro Member

    @Zen depends what you are looking to do.

    You could use xinetd, lock it to your webservers IP, and use a local bash script to do tasks and return an output.

    Similar how to MySQL load-balancing with HA-Proxy works, uses xinetd to check for replication faults etc

  • krokro Member

    @Zen make sure you have some good checking involved so no one manipulates your $data that holds the command.

    The idea here is to make it only do the things you want it to, Sometimes making things Dynamic introduces security issues - so if you do, make sure you have some heavy checking before executing said commands :).

  • ZenZen Member

    I'll look into it, thanks. We should have LEC (LowEndCoding) :D

    Thanked by 1djvdorp
  • Totally unrelated. Earlier today I purchased the domain name

    For no. apparent. reason. at. all.

    Catalyst Host - Pie Approved!
    Thanked by 1Xeoncross
  • @HalfEatenPie said: Totally unrelated. Earlier today I purchased the domain name

    I will now have to destroy you.

    The Original Daniel.

  • @MrLawoodle said: I will now have to destroy you.

    Come and get it.









    Catalyst Host - Pie Approved!
Sign In or Register to comment.