Jump to content

Converting bash to PHP


Recommended Posts

Hello everyone!

Nice to see such a community dedicated to helping out with PHP. I hope someone can help me out with my issue. For some time now I have been trying to re-create this bash script into a pure PHP script and by searching online I found out that I needed libssh2, which I have installed now and confirmed is working. Unfortunately I have still not been able to re-create the script in pure PHP. I suspect that the server in question does not accept any commands once a shell has opened as a security measure and that what the bash script does is send the commands without opening a shell, which the server then accepts. Since I am new with both bash scripts and PHP I might be wrong in my suspicion and that something else might be wrong, but so far I have not managed to make any PHP script that worked. Hopefully someone here might help me out with this, I would appreciate it.


This is the bash script:











echo "mode='$init' cooldown='$docool' heatup='$doheat' device='$id'" | ssh -T -i $key root@server > /etc/slog/$init


prevfile="$(ls $dir | sort -n | tail -n1)"                                                                                                               



if [ -z "$prevfile" ]; then                                                                                                                               



    prevsum="$(md5sum $dir/$prevfile | cut -d\  -f1)"                                                                                                     

    currsum="$(md5sum /etc/slog/$init | cut -d\  -f1)"                                                                                                   


    echo "$prevsum $currsum"                                                                                                                                   

    if [ "$prevsum" != "$currsum" ]; then                                                                                                                     



        echo "No changes"                                                                                                                     




if [ "$docopy" -eq 1 ]; then                             

    file="$dir/$init-$(date '+%Y%m%d-%H%M')"

    echo "Creating new $file"   

    cp /etc/slog/$init $file                   


    echo "No changes"                           



Any ideas on how I can re-create the script as PHP?

Link to comment
Share on other sites

Thank you and I appreciate your offer, but I would rather do it myself so I can learn something about bash and PHP in the process. So I am looking for some help and some suggestions in the first place. Using php.net/system or php.net/exec is not an option because I would be back at the beginning using a bash script which is what I am trying avoid. I have already re-created this script a couple of times unsuccessfully and that is the reason I have decided to start from the beginning again, but this time I intend to ask about the things I am unsure about so that I won't yet again end in a dead end. So far on this line I hit my first problem:


echo "mode='$init' cooldown='$docool' heatup='$doheat' device='$id'" | ssh -T -i $key root@server > /etc/slog/$init


I tried to re-create the ssh login part with this code:

$connection = ssh2_connect("server", 22);

if (ssh2_auth_pubkey_file($connection, "root", "/.ssh/id_rsa")) {
  echo "Authenticated";
} else {


Which failed with this message "Warning: ssh2_auth_pubkey_file() expects at least 4 parameters, 3 given". I tried many different combinations, but all fail the same way since it seems that ssh2_auth_pubkey_file() needs both a private key and a public one. Although I have both keys the original bash script works with only the private key. Is this possible to do with PHP?


Now if I add both the private and public key it authenticates and I hit on my second problem. As mentioned I suspect the bash script does not request a shell, but instead somehow sends the command directly (if I have interpreted the bash script wrong please tell me). This is because when I manually use ssh and log into the server I get the shell and banner but if I try to do anything the connection terminates instead of outputting data as it does when the bash script runs. The same thing happens when sending the command using PHP. Here is the code:


if(!($stream = ssh2_exec($connection, "mode='$init' cooldown='$docool' heatup='$doheat' device='$id'")) ){
  echo "Failed";
} else{
  stream_set_blocking($stream, true);
  $output = "";
  while($buffer = fread($stream,4096)){
    $output .= $buffer;
echo "Received\n";
echo $output;


This code does not seem to be working, the last thing I get is the text "Received" and nothing else. Although ssh2_exec() doesn't seem to fail sending the command, but since nothing is returned ($output is empty) I suspect the server did not accept the command, which I can confirm by looking at the server (the status on the server did not changing). I am truly lost here and would really appreciate any help sorting this out. Thanks.

Link to comment
Share on other sites

  • 3 weeks later...
This thread is more than a year old. Are you sure you have something important to add to it?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.