设为首页 收藏本站
查看: 769|回复: 0

[经验分享] php 安装 Secure Shell2

[复制链接]

尚未签到

发表于 2017-3-24 10:24:25 | 显示全部楼层 |阅读模式
安装好 apache和php 后 再安装 ssh2





确认已经安装好openssl


[iyunv@ ] # rpm -qa | grep openssl
openssl-devel-0.9.7a-43.8
xmlsec1-openssl-1.2.6-3
openssl096b-0.9.6b-22.42
openssl-0.9.7a-43.8






一。安装libssh2
1. tar zxvf libssh2-0.18.tar.gz
2. cd libssh2-0.18
3. ./configure
4. make all install

二、下载 ssh2 , 编译
1. tar zxvf ssh2-0.10.tgz
2. cd ssh2-0.10
3. /usr/local/php/bin/phpize
4. ./configure --with-ssh2 --with-php-config=/usr/local/php/bin/php-config
  6. make

六、将ssh2.so增加到php扩展模块
1. mkdir /usr/local/php/lib/extensions
2. cd ssh2-0.10 (进入ssh2的原安装包目录)
3. cp modules/ssh2.so /usr/local/php/lib/extensions/
4. 编辑php.ini
vi /usr/local/php/lib/php.ini

extension_dir = "/usr/local/php/lib/extensions"
extension=ssh2.so

启动apache测试:/usr/local/apache2/bin/apachectl start

七、常见问题
1、找不到ssh2 等各个函数,比如error:Call to undefined function ssh2_connect()
原因:没有正确指定php.ini中extension_dir的值
解决方案:一定要正确指定extension_dir =

2、调用ssh2_auth_pubkey_file函数无法通过验证
可能的原因:ssh2_auth_pubkey_file函数中参数pubkeyfile和privkeyfile 文件的权限跟apache配置的用户和组不一致
解决方案:使用ssh2_auth_pubkey_file时,一定要注意函数中参数pubkeyfile和privkeyfile 文件的权限,其权限一定要和apache配置的用户和组一致,否则无法通过验证!
注:key文件生成命令 ssh-keygen -t dsa
参考过的网站
http://pkeck.myweb.uga.edu/ssh/
http://www.csua.berkeley.edu/~ranga/notes/ssh_nopass.html








SSH2 Functions 簡介

ssh2_connect():
resource ssh2_connect ( string $host [, int $port [, array $methods [, array $callbacks ]]] )
ssh2_connect 建立一個連結至遠端的 SSH server,若連結成功則傳回 resource,反之則傳回 false。

參數
host:主機資訊
port:埠號
method:一個可包含最多四個參數的關聯陣列(kex, hostkey, client_to_server, server_to_client)
callbacks:一個可包含任何或全部參數的關聯陣列(ignore, debud, macerror, disconnect)



範例:透過 ssh2_connect 連結遠端機器。
<?php
/* Notify the user if the server terminates the connection */
function my_ssh_disconnect($reason, $message, $language) {
printf("Server disconnected with reason code [%d] and message: %s/n",
$reason, $message);
}
$methods = array(
'kex' => 'diffie-hellman-group1-sha1',
'client_to_server' => array(
'crypt' => '3des-cbc',
'comp' => 'none'),
'server_to_client' => array(
'crypt' => 'aes256-cbc,aes192-cbc,aes128-cbc',
'comp' => 'none'));
$callbacks = array('disconnect' => 'my_ssh_disconnect');
$connection = ssh2_connect('shell.example.com', 22, $methods, $callbacks);
if (!$connection) die('Connection failed');
?>

ssh2_auth_password():
boolssh2_auth_password(resource $session , string $username , string $password )
ssh2_auth_password 透過SSH來認證遠端機器上的使用者帳號及密碼,若認證成功則傳回 true,反之則傳回 false。

參數
session:一個 SSH 連結的識別。
username:遠端機器上的使用者帳號
password:該使用者帳號的密碼

範例:透過 ssh2_auth_password 認證遠端機器上的使用者帳號及密碼。
<?php
$connection = ssh2_connect('shell.example.com', 22);
if (ssh2_auth_password($connection, 'username', 'secret')) {
echo "Authentication Successful!/n";
} else {
die('Authentication Failed...');
}
?>

ssh2_scp_send():
bool ssh2_scp_send(resource $session , string $local_file , string $remote_file [, int $create_mode ] )
ssh2_scp_send 透過 SCP protocol將檔案從本地機器複製並傳送到遠端機器,若傳送成功則傳回 true,反之則傳回 false。

參數
session:一個 SSH 連結的識別。
local_file:本地機器上的檔案路徑。
remote_file:遠端機器上的檔案路徑。
create_mode:指定檔案的產生方式。

範例:透過 ssh2_scp_send 傳送檔案至遠端機器。
<?php
$connection = ssh2_connect('shell.example.com', 22);
ssh2_auth_password($connection, 'username', 'password');
ssh2_scp_send($connection, '/local/filename', '/remote/filename', 0644);
?>

ssh2_exec():
resource ssh2_exec(resource $session , string $command)
ssh2_exec 可在遠端機器上執行指令,若執行成功則傳回 stream,反之則傳回 false。

參數
session:一個 SSH 連結的識別。
command:欲執行的指令。

範例:透過ssh2_exec 在遠端機器上執行指令。
<?php
$connection = ssh2_connect('shell.example.com', 22);
ssh2_auth_password($connection, 'username', 'password');
$stream = ssh2_exec($connection, '/usr/local/bin/php -i');
?>





Secure Shell2
  ssh2.shell://ssh2.exec://ssh2.tunnel://ssh2.sftp://ssh2.scp://PHP 4.3.0 及以上版本 (PECL)

  •   ssh2.shell://user:pass@example.com:22/xterm
  •   ssh2.exec://user:pass@example.com:22/usr/local/bin/somecmd
  •   ssh2.tunnel://user:pass@example.com:22/192.168.0.1:14
  •   ssh2.sftp://user:pass@example.com:22/path/to/filename
  该封装器默认没有启用: 要使用 ssh2.*://封装器,必须安装SSH2扩展。可以到PECL下载。

  除了接受传统 URI 的登录信息外,ssh2 封装器也重用主机 URL 的一部分的连接资源。
  例子 L-2. 为活动连接打开流
<?php
$session
=ssh2_connect('example.com', 22);
ssh2_auth_pubkey_file($session, 'username', '/home/username/.ssh/id_rsa.pub',
'/home/username/.ssh/id_rsa', 'secret');
$stream = fopen("ssh2.tunnel://$session/remote.example.com:1234", 'r');
?>

  表格 L-8. 封装协议摘要
属性ssh2.shellssh2.execssh2.tunnelssh2.sftpssh2.scp受限于 allow_url_fopen。是是是是是允许读取是是是是是允许写入是是是是否允许附加否否否是 (若服务器支持)否允许同时读写是是是是否支持 stat()否否否是否支持 unlink()否否否是否支持 rename()否否否是否支持 mkdir()否否否是否支持 rmdir()否否否是否
  表格 L-9. 上下文选项
名称用法默认session可重用预连接的 ssh2 连接资源sftp可重用预分配的 sftp 连接资源methods要使用的 Key exchange, hostkey, cipher, compression, 和 MAC 方法callbacksusername连接的用户名password密码认证时需要的密码pubkey_file认证需要的 public key 文件名privkey_file认证需要的 private key 文件名env要设置的环境变量的数组term当分配一个控制台时使用的终端类型term_width当分配一个控制台时终端的宽度term_height当分配一个控制台时终端的高度term_units与 term_width 和 term_height 一同使用的单元SSH2_TERM_UNIT_CHARS


adda noteUser Contributed Notes

There are no user contributed notes for this page.

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-354493-1-1.html 上篇帖子: php定时计划任务 下篇帖子: PHP 性能 优化 缓存
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表