Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3721246
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T05:29:25+00:00 2026-05-19T05:29:25+00:00

I am trying to execute remote commands from within a php script over ssh,

  • 0

I am trying to execute remote commands from within a php script over ssh, and I want the output from the commands (stdout and stderr) be streamed to the originating host.

I know in Perl and Ruby this is possible. I could not find any such examples in php.

Code:

$ip = 'kssotest.yakabod.net';
$user = 'tester';
$pass = 'kmoon77';

$connection = ssh2_connect($ip);
ssh2_auth_password($connection,$user,$pass);
$shell = ssh2_shell($connection,"bash");

$cmd = "echo '[start]';your commands here;echo '[end]'";
$output = user_exec($shell,$cmd);

fclose($shell);

function user_exec($shell,$cmd) {
  fwrite($shell,$cmd . "\n");
  $output = "";
  $start = false;
  $start_time = time();
  $max_time = 2; //time in seconds
  while(((time()-$start_time) < $max_time)) {
    $line = fgets($shell);
    if(!strstr($line,$cmd)) {
      if(preg_match('/\[start\]/',$line)) {
        $start = true;
      }elseif(preg_match('/\[end\]/',$line)) {
        return $output;
      }elseif($start){
        $output[] = $line;
      }
    }
  }
}

But when I execute it like this $php remote.php, I get an error:

PHP Fatal error:  Call to undefined function ssh2_connect() 
in /home/tester/PHP_SSH2/remote.php on line 6

What is the best way to execute remote commands in PHP via ssh?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T05:29:26+00:00Added an answer on May 19, 2026 at 5:29 am

    If you can’t add php packages due to red tape, here’s a simple class that can do the trick

    class ExecuteRemote
    {
        private static $host;
        private static $username;
        private static $password;
        private static $error;
        private static $output;
    
        public static function setup($host, $username=NULL, $password=NULL)
        {
            self::$host = $host;
            self::$username = $username;
            self::$password = $password;
        }
    
        public static function executeScriptSSH($script)
        {
            // Setup connection string
            $connectionString = self::$host;
            $connectionString = (empty(self::$username) ? $connectionString : self::$username.'@'.$connectionString);
    
            // Execute script
            $cmd = "ssh $connectionString $script 2>&1";
            self::$output['command'] = $cmd;
            exec($cmd, self::$output, self::$error);
    
            if (self::$error) {
                throw new Exception ("\nError sshing: ".print_r(self::$output, true));
            }
    
            return self::$output;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to execute a stored procedured from a linked database in MS
I am trying to execute this SQL command: SELECT page.page_namespace, pagelinks.pl_namespace, COUNT(*) FROM page,
Is it possbile to execute linux commands with java? I am trying to create
I have a bash script that does ssh to a remote machine and executes
I am trying to execute this SQL query prior to restoring a .BAK file
I'm trying to execute a asp.net webservice using jquery. When I pass only one
I hit this error while my web application was trying to execute a SELECT
I am trying to get simple jQuery to execute on my Content page with
I'm trying to access the command line and execute a command, and then return
I'm trying to create an application that will let me execute a method specified

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.