>  기사  >  php教程  >  用SSH与PHP相连接 确保数据传输的安全性

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

WBOY
WBOY원래의
2016-06-06 19:55:45823검색

欢迎进入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>  }

  第二种方法:外壳

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

  

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>  }

  [1] [2] [3] 

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

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.