SSH 和 SFTP 客户端库
SSH和SFTP客户端库用于在iOS和Android上的React Introduct.
完整文档
一整套 文件可在Mintlify上查阅,包括:
- 安装
- 快速启动指南
- API 参考
- 核心概念
- 指南
以下为快速安装和使用指南以起起跑. 这个 完整文档 现建议除下文外,再作补充.
[尖叫声] 主机密钥验证未执行 . 此库不会校验或锁定远程服务器的主机密钥( 在 Android 上)
StrictHostKeyChecking已禁用,在iOS上,主机密钥不检查),所以连接容易被中层人攻击. 在解决之前, 只能通过您信任的网络连接 。 见 安全指南 详细情况.
安装
npm install @dylankenneally/react-native-ssh-sftp
iOS系统
更新您的 Podfile 用于使用 亚拿0的叉子 联 合 国 NSSSH (英语)。请注意,我们使用forked版本给我们一个所需的后期版本libsh。 Podfile 位于您的 React 原生项目 : ./ios/Podfile。 。 。 .
target '[your project's name]' do
pod 'NMSSH', :git => 'https://github.com/aanah0/NMSSH.git' # <-- add this line
# ... rest of your target details ...
end
然后跑开 pod install 在你身边 ./ios 目录.
cd ios
pod install
cd -
[TIP] 添加一个
postinstall脚本到您的package.json要运行的文件pod install之后npm install这是一个好主意。 这个pod-install软件包是这样做的好方法.{ "scripts": { "postinstall": "npx pod-install", } }
在 iOS 有 OpenSSL 问题吗 ?
使用时 翻转 要调试您的应用程序, 它将会包含 OpenSSL 的副本 。 这可能会与NMSSH使用的OpenSSL版本产生问题. 您可以通过删除/ 注释来禁用翻转器 flipper_configuration => flipper_config, 线条 Podfile。 。 。 .
机器人
Android不需要额外的步骤.
链接
这个项目经过了更新,可以使用React Industrial v84(最晚在撰写时为2026年2月)——这意味着不需要手动连接.
使用量
所有同步运行的函数, 此时我们必须等待结果 返回如果出错时可以拒绝的承诺 .
[注] 在iOS上,此软件包目前不支持模拟器,您需要将您的应用程序运行在物理设备上. 如果你想知道更多关于这个,你看 这个问题我欢迎公关解决这件事.
使用密码认证创建客户端
import SSHClient from '@dylankenneally/react-native-ssh-sftp';
SSHClient.connectWithPassword(
"10.0.0.10",
22,
"user",
"password"
).then(client => {/*...*/});
使用公钥认证创建客户端
import SSHClient from 'react-native-ssh-sftp';
SSHClient.connectWithKey(
"10.0.0.10",
22,
"user",
privateKey="-----BEGIN RSA...",
passphrase
).then(client => {/*...*/});
公钥认证也得到支持
{privateKey: '-----BEGIN RSA......'}
{privateKey: '-----BEGIN RSA......', publicKey: 'ssh-rsa AAAAB3NzaC1yc2EA......'}
{privateKey: '-----BEGIN RSA......', publicKey: 'ssh-rsa AAAAB3NzaC1yc2EA......', passphrase: 'Password'}
关闭客户端
client.disconnect();
执行 SSH 命令
const command = 'ls -l';
client.execute(command)
.then(output => console.warn(output));
贝壳
启动外壳
- 支持的 ptyType: 香草, vt100, vt102, vt220, ansi, xterm
const ptyType = 'vanilla';
client.startShell(ptyType)
.then(() => {/*...*/});
从 shell 读取
client.on('Shell', (event) => {
if (event)
console.warn(event);
});
写入到外壳
const str = 'ls -l\n';
client.writeToShell(str)
.then(() => {/*...*/});
关闭外壳
client.closeShell();
SFTP 系统
连接 SFTP
client.connectSFTP()
.then(() => {/*...*/});
列表目录
const path = '.';
client.sftpLs(path)
.then(response => console.warn(response));
创建目录
client.sftpMkdir('dirName')
.then(() => {/*...*/});
重命名文件或目录
client.sftpRename('oldName', 'newName')
.then(() => {/*...*/});
删除目录
client.sftpRmdir('dirName')
.then(() => {/*...*/});
删除文件
client.sftpRm('fileName')
.then(() => {/*...*/});
下载文件
client.sftpDownload('[path-to-remote-file]', '[path-to-local-directory]')
.then(downloadedFilePath => {
console.warn(downloadedFilePath);
});
// Download progress (setup before call)
client.on('DownloadProgress', (event) => {
console.warn(event);
});
// Cancel download
client.sftpCancelDownload();
上传文件
client.sftpUpload('[path-to-local-file]', '[path-to-remote-directory]')
.then(() => {/*...*/});
// Upload progress (setup before call)
client.on('UploadProgress', (event) => {
console.warn(event);
});
// Cancel upload
client.sftpCancelUpload();
关闭 SFTP
client.disconnectSFTP();
示例应用程序
您可以为此库的用法找到一个非常简单的例子应用程序 这里。 。 。 .
贷项
这个软件包包包了以下库,提供了实际的SSH/SFTP功能:
- NSSSH (英语) 为iOS服务
- 约施 用于Android(从马蒂亚斯·维德曼叉(中文(简体) )
这个包裹是埃马纽埃尔·纳蒂维达德的叉子 反应-内生- ssh- sftp 软件包。 分叉链从那里取出如下: