« +10 Points For Apple Support | Main | Alas, Poor HD DVD »

Testing an SSH Connection With PHP Through Bash

This one had been bothering me since yesterday afternoon. Apparently "php -r 'code_here'" is the way to go for calling PHP functions from a command line, rather than doing "echo 'code_here' | php". So, we now have a Bash function that calls PHP to test an SSH connection (I got tired of looking for a good way to do it entirely within Bash):

# Call as "check_ssh_root_access SERVER_NAME"
# $RES will be "HELLOWORLD" if the connection is good, "GOODBYEWORLD" otherwise
RES=`php -r "error_reporting(0);
    \\$ssh_connect = ssh2_connect('$1');
    if (ssh2_auth_pubkey_file(\\$ssh_connect, 'root', '/root/.ssh/id_dsa.pub', '/root/.ssh/id_dsa'))
    {
        echo 'HELLOWORLD';
    }
    else
    {
        echo 'GOODBYEWORLD';
    }"`

Noting this in as many places as possible so I remember it next time.

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

About

This page contains a single entry from the blog posted on February 26, 2008 6:05 PM.

The previous post in this blog was +10 Points For Apple Support.

The next post in this blog is Alas, Poor HD DVD.

Many more can be found on the main index page or by looking through the archives.

Powered by
Movable Type 3.34