php 安装 Secure Shell2
安装好 apache和php 后 再安装 ssh2确认已经安装好openssl
# 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
http://www.51.cx/phponline/notes-add.gifadda noteUser Contributed Notes
There are no user contributed notes for this page.
页:
[1]