Home > php教程 > php手册 > body text

用SSH与PHP相连接 确保数据传输的安全性

WBOY
Release: 2016-06-06 19:55:45
Original
825 people have browsed it

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入 下面我们分别详述之: 第一种方法:执行 你最好为下面的代码创建函数或者是一个类,不过本文仅仅起到一个为您提供基本观念的作用,所以说你可以如此开始: if (!function_exists("ssh2_connect")) di

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入

  下面我们分别详述之:

  第一种方法:执行

  你最好为下面的代码创建函数或者是一个类,不过本文仅仅起到一个为您提供基本观念的作用,所以说你可以如此开始:

  

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

  第二种方法:外壳

  同样道理,你也可以为如下的代码编写函数或者一个类。不过,本文仅仅提供基本观念:

  

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

  [1] [2] [3] 

用SSH与PHP相连接 确保数据传输的安全性

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!