安装好 apache和php 后 再安装 ssh2
确认已经安装好openssl
[root@ ] # 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
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
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
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配置的用户和组一致,否则无法通过验证!
原 因:没有正确指定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
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():
bool ssh2_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');
?>
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():
bool ssh2_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 Shell 2
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
除了接受传统 URI 的登录信息外,ssh2 封装器也重用主机 URL 的一部分的连接资源。
表格 L-8. 封装协议摘要
属 性 | ssh2.shell | ssh2.exec | ssh2.tunnel | ssh2.sftp | ssh2.scp |
---|---|---|---|---|---|
受 限于 allow_url_fopen 。 | 是 | 是 | 是 | 是 | 是 |
允 许读取 | 是 | 是 | 是 | 是 | 是 |
允 许写入 | 是 | 是 | 是 | 是 | 否 |
允 许附加 | 否 | 否 | 否 | 是 (若服务器支持) | 否 |
允 许同时读写 | 是 | 是 | 是 | 是 | 否 |
支 持 stat() | 否 | 否 | 否 | 是 | 否 |
支 持 unlink() | 否 | 否 | 否 | 是 | 否 |
支 持 rename() | 否 | 否 | 否 | 是 | 否 |
支 持 mkdir() | 否 | 否 | 否 | 是 | 否 |
支 持 rmdir() | 否 | 否 | 否 | 是 | 否 |
表格 L-9. 上下文选项
名称 | 用 法 | 默认 |
---|---|---|
session | 可 重用预连接的 ssh2 连接资源 | |
sftp | 可 重用预分配的 sftp 连接资源 | |
methods | 要 使用的 Key exchange, hostkey, cipher, compression, 和 MAC 方法 | |
callbacks | ||
username | 连接的用户名 | |
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 |
![add a note](http://www.51.cx/phponline/notes-add.gif)
There are no user contributed notes for this page.