首页 | 互联网 | IT动态 | IT培训 | Cisco | Windows | Linux | Java | .Net | Oracle | 软件测试 | C/C++ | 嵌入式开发 | 存储世界 | 服务器
网络设备 | IDC | 安全 | 求职招聘 | 数字网校 | 网页设计 | 平面设计 | 技术专题 | 电子书下载 | 教学视频 | 源码下载 | 搜索 | 博客 | 论坛
中国IT实验室Linux频道
Google
首页 入门 Linux编程 系统管理 网络管理 Linux认证 Unix/BSD Linux数据库 Linux集群 Linux手册 Linux下载 Ubuntu 论坛 专题 RSS
您现在的位置: 中国IT实验室 >> Linux >> Linux编程 >> PHP >> 正文

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

  下面我们分别详述之:

  第一种方法:执行

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

  

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

  第二种方法:外壳

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

  

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] 下一页

【责编:Yoyo】
中国IT教育
相关产品和培训
文章评论
 友情推荐链接
 专题推荐

 ·防范Linux病毒 打造没有病毒的乐土…
 ·Linux Shell编程实用指南…
 ·Linux日志分析与管理
 ·揭密Linux内存管理
 ·邮件服务Sendmail应用配置
 ·Linux 安全管理…
 ·Linux 下DNS服务器架设攻略…
 ·Linux 下的路由的配置与应用…
 ·专题:Apache实用手册
 ·全面剖析Linux文件系统
 今日更新
 认证培训
 频道精选
 Windows频道导航