Connect SSH to PHP to ensure data transmission security

Source: Internet
Author: User
Welcome to the Linux community forum and interact with the 2 million technical staff. Next we will detail the following: the first method is to execute a function or a class for the following code, however, this article only serves to provide you with basic concepts, so you can start with: if (! Function_exists (ssh2_connect) di

Welcome to the Linux community forum and interact with 2 million technical staff> here we will detail the first method: execute the following code to create a function or a class, however, this article only serves to provide you with basic concepts, so you can start with: if (! Function_exists ("ssh2_connect") di

Welcome to the Linux community forum and interact with 2 million technicians>

We will detail the following in detail:

Method 1: Execute

You 'd better create a function or a class for the following code, but this article only serves to provide you with a basic idea, so you can start like this:

  

if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist")
  // log in at server1.example.com on port 22
  if(!($con = ssh2_connect("server1.example.com", 22))){
  echo "fail: unable to establish connection\n";
  } else {
  // try to authenticate with username root, password secretpassword
  if(!ssh2_auth_password($con, "root", "secretpassword")) {
  echo "fail: unable to authenticate\n";
  } else {
  // allright, we're in!
  echo "okay: logged in...\n";
  // execute a command
  if(!($stream = ssh2_exec($con, "ls -al" )) ){
  echo "fail: unable to execute command\n";
  } else{
  // collect returning data from command
  stream_set_blocking( $stream, true );
  $data = "";
  while( $buf = fread($stream,4096) ){
  $data .= $buf;
  }
  fclose($stream);
  }
  }

Method 2: Housing

Similarly, you can write a function or a class for the following code. However, this article only provides the basic concepts:

  

if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist")
  // log in at server1.example.com on port 22
  if(!($con = ssh2_connect("server1.example.com", 22))){
  echo "fail: unable to establish connection\n";
  } else {
  // try to authenticate with username root, password secretpassword
  if(!ssh2_auth_password($con, "root", "secretpassword")) {
  echo "fail: unable to authenticate\n";
  } else {
  // allright, we're in!
  echo "okay: logged in...\n";
  // create a shell
  if(!($shell = ssh2_shell($con, 'vt102', null, 80, 40, SSH2_TERM_UNIT_CHARS))){
  echo "fail: unable to establish shell\n";
  } else{
  stream_set_blocking( $shell, true );
  // send a command
  fwrite($shell,"ls -al\n");
  sleep(1);
  // & collect returning data
  $data = "";
  while( $buf = fread($shell,,4096) ){
  $data .= $buf;
  }
  fclose($shell);
  }
  }
  }

[1] [2] [3]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.