2

My problem : I have a bash script on the server A, and a php script on another server, server B. The shell script on the server A was used to run the php script on the other server, but a few days ago one person put a bad rm -rf command on a wrong directory and deleted it. I had an old backup of this shell script but the code calling the php script on the second server is missing. These few lines (one line in fact, if I remember well) had been written by another person, some years ago, who does not work with us anymore and I can't contact her. I am not a php person and am quite new to this language, and after having searched for some tips on the web the last two days, without results, I decided to post here.

I am logged on the server A, as a user that can run the shell script. I have another couple of username/password used to log on the server B. This user can run the php script. Here's this php script :

<?php

/*
php -f invoke_manage_auto_requests_files.php "create|delete|upload" "vide|pre|res|obs" "nrcc|cgcm|arpege|crcm|rc|sta|md|mds" id "processing|nom du fichier"
eg . php -f invoke_manage_auto_requests_files.php create pre nrcc 15678 processing
*/

if($argv[1] == "upload") {
    $argv[5] = '@' .  realpath($argv[5]);
}

$post_fields = array( 'ACTION' => $argv[1] , 'PREFIX' => $argv[2] , 'REQ_TYPE' => $argv[3] , 'ID' => $argv[4] , 'FILE_CONTENT' => "$argv[5]");
$post_fields['VALIDATE'] = md5("This is legit");
$url = "http://serverB/scripts/manage_auto_requests_files.php";
$ch = curl_init($url);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS, $post_fields );
curl_exec($ch);
curl_close($ch);
?>

I am searching the way to call : php -f invoke_manage_auto_requests_files.php create pre nrcc 15678 processing (for example) But from the bash script. I only know that seems to be a curl call but i am not sure.

Here are the specifications of my servers :

Server A : PHP Version 4.4.4-8+etch6 System Linux hawa 2.6.26-bpo.2-686-bigmem #1 SMP Fri Jul 3 21:38:05 UTC 2009 i686

Server B : PHP Version 4.3.9 System Linux verglas 2.6.9-34.ELsmp #1 SMP Fri Feb 24 16:54:53 EST 2006 i686

Thanks for reading, or helping.

1 Answer 1

1

Hummm. No need for curl here. The usage comment makes me think the php script is supposed to be executed from the CLI, not from an http web server.

First, make sure you can login to server B from server A with no password.

If this is not the case, use ssh-copy-id to copy the user's public key from serverA to serverB

Then, just use ssh from server A to server B to run the script :

# Usage : call_server_b id
# @param string  action   "create|delete|upload"
# @param string  status   "vide|pre|res|obs"
# @param string  param3   "nrcc|cgcm|arpege|crcm|rc|sta|md|mds"
# @param integer id
# @param integer resource "processing|nom du fichier"
call_server_b() {
    local usage="Usage: $0 action status param3 id resource"
    if [ $# -ne 5 ]
    then
        echo $usage
    fi
    local action=$1;shift
    local status=$1;shift
    local param3=$1;shift
    local id=$1;shift
    local resource=$1

    case $action in
        create|delete|upload)
            # all good
            ;;
        *)
            echo "wrong action parameter : " $action >&2
            exit 1
            ;;
    esac
    case $status in
        vide|pre|res|obs)
            # all good
            ;;
        *)
            echo "wrong status parameter : " $status >&2
            exit 1
            ;;
    esac

    case $param3 in
        nrcc|cgcm|arpege|crcm|rc|sta|md|mds)
            # all good
            ;;
        *)
            echo "wrong param3 parameter : " $param3 >&1
            exit 1
            ;;
    esac

    case $resource in
        processing|'nom du fichier')
            # all good
            ;;
        *)
            echo "wrong resource parameter : " $resource >&1
            exit 1
            ;;
    esac


    ssh user@serverB \
        php -f /absolute/path/to/invoke_manage_auto_requests_files.php \
        $action $status $param3 $id $resource
}

call_server_b create pre nrcc 15678 processing
Sign up to request clarification or add additional context in comments.

6 Comments

I agree with CLI comments but why use bash? I think for a php dev, php is less of a barrier than bash.
because php might not be installed on serverA ? Anyway, bash is a requirement, see the question. Plus, bash is appropriate when running external programms like ssh.
Why use bash : the bash script is run by a cronjob and make a lot of tasks. The .sh file is 3500+ lines and functional (even if the link is missing), supervisors don't need/want to update it. I forgot to mention that server A is owned by a private company, server B by a government agency. That's why I am not allowed to use ssh-keygen/ssh-copy-id to link them... I'll contact the person that wrote the link and I'll put the answer here when I'll got it. Thanks.
3500+ lines... outch. If you ever recover the code, put it in a separate function like I did, and in a separate file if you can. And read this : kfirlavi.com/blog/2012/11/14/defensive-bash-programming
Hi. I finally found a way to bypass the communication security moving the invoke script into the client server (server A), as you said, and using the sshpass packet. Thanks a lot, problem solved.
|

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.