首页 > 技术文章 > php ssh2安装教程

newmiracle 2020-11-06 15:01 原文

php ssh2安装教程

下载地址 http://windows.php.net/downloads/pecl/releases/ssh2/0.12/

根据自己PHP的版本去下载,我使用的是线程安全的,所以下载的是php_ssh2-0.12-5.4-ts-vc9-x86.zip

将 php_ssh.dll、php_ssh2.pdb 放到你的 php 扩展目录下 php/ext/ 下

 php.ini中加入 extension=php_ssh2.dll

 

我在centos下没问题 

在win7下有问题 apache会崩掉

所以我采用python ssh2 上传代码

 

python上传目录的方法

import paramiko
import os
ip='111';
username='11';
password='11';
transport = paramiko.Transport((ip, 22))
transport.connect(username=username, password=password)
sftp = paramiko.SFTPClient.from_transport(transport)
ssh = paramiko.SSHClient();

ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=ip, port=22, username=username, password=password)

#上传文件目录
src_path = 'C:/Users/Administrator/Desktop/ss/'
target_path ='/root/ss/'
ssh.exec_command('mkdir -p '+target_path);
def copy_file(src_path,target_path):
src_path=src_path+'/'
target_path=target_path+'/'
filelist = os.listdir(src_path)
for file in filelist:
path = os.path.join(src_path,file)
if os.path.isdir(path):
# 递归调用
target_path1 = os.path.join(target_path,file)
ssh.exec_command('mkdir -p '+target_path1)
print(target_path1)
copy_file(path,target_path1)
else:
path1 = os.path.join(target_path,file)
sftp.put(path, path1)


copy_file(src_path,target_path)
transport.close();

ssh.close()




 ps:如果出现no such file 那就创建目录后延迟后 再传输文件 或者 创建目录前先判断目录有没有存在 存在就不要创建了

推荐阅读