remote-shell-cli
v1.0.3
Published
Execute commands on remote servers via SSH
Readme
remote-shell
通过 SSH 在远程服务器上执行命令。
功能特性
- 单主机命令执行:在单个远程主机上执行命令
- 实时流式输出:命令输出实时显示到终端
- 文件上传/下载:通过 SFTP 在本地与远程之间传输文件(带进度显示)
- 配置热重载:每次调用读取最新配置,无需重启
- 双重认证:支持密码和私钥认证
快速开始
1. 配置主机
创建 ~/.config/remote-shell/hosts.json:
{
"hosts": {
"gpu3070": {
"alias": "3070",
"host": "qfwuzrbacg7jnlyusnow.deepln.com",
"port": 47612,
"username": "root",
"password": "your-password-here",
"timeout": 5000
}
}
}2. 列出主机
remote-shell list输出:
3070GPU (3070host):
host: qfwuzrbacg7jnlyusnow.deepln.com
port: 47612
username: root
password: ******
timeout: 50003. 添加主机
# 交互式(推荐)
remote-shell add
# 参数式
remote-shell add --alias 3070GPU --host 192.168.1.1 --username root --password your-password4. 删除主机
# 直接删除
remote-shell delete 3070GPU
# 交互式选择删除
remote-shell delete5. 执行命令
remote-shell 3070 "uptime && free -h"输出:
18:00:14 up 11 days, 15:15, 0 user, load average: 22.06, 22.98, 22.02
Mem: 251Gi used: 30Gi, free: 221Gi命令行参数
remote-shell list, --hosts, -l # 列出所有主机
remote-shell <alias> <command> # 执行命令
remote-shell upload <alias> <local> <remote> # 上传文件到远程主机
remote-shell download <alias> <remote> <local> # 从远程主机下载文件
remote-shell --help, -h # 显示帮助示例
remote-shell 3070 "df -h"
remote-shell dev "uptime"
remote-shell upload 3070 ./model.tar.gz /root/model.tar.gz
remote-shell download 3070 /root/logs/app.log ./app.log错误处理
当别名不存在时,会报错并显示所有可用的主机别名:
$ remote-shell nonexistent "echo test"
No hosts found for alias: nonexistent
Available hosts:
3070GPU -> [email protected]:47612配置说明
配置文件路径
| 路径 | 说明 |
| ----------------------------------- | -------------------- |
| ~/.config/remote-shell/hosts.json | 默认配置(XDG 标准) |
支持 XDG_CONFIG_HOME 环境变量自定义配置目录。
可通过 --config <path> 指定自定义配置文件。
配置字段
| 字段 | 类型 | 必填 | 默认值 | 说明 |
| -------------- | ------ | ---- | ------ | -------------------- |
| alias | string | 是 | - | CLI 快速访问名称 |
| host | string | 是 | - | 主机名或 IP 地址 |
| port | number | 否 | 22 | SSH 端口 |
| username | string | 是 | - | SSH 用户名 |
| password | string | 否* | - | SSH 密码 |
| privateKeyPath | string | 否* | - | 私钥路径(~ 展开) |
| timeout | number | 否 | 5000 | 超时时间(毫秒) |
*二选一:password 或 privateKeyPath
密码认证
{
"hosts": {
"gpu3070": {
"alias": "3070",
"host": "example.com",
"port": 22,
"username": "root",
"password": "secret"
}
}
}私钥认证
{
"hosts": {
"gpu3070": {
"alias": "3070",
"host": "example.com",
"port": 22,
"username": "root",
"privateKeyPath": "~/.ssh/id_rsa"
}
}
}安装方式
从源码构建
cd /path/to/remote-shell-cli
npm install
npm run build
./dist/remote-shell list开发链接
cd /path/to/remote-shell-cli
npm run build
npm link
remote-shell list添加到 PATH
cp ./dist/remote-shell ~/.local/bin/remote-shell技术栈
| 包 | 版本 | 用途 | | ------------------------------------------ | ------- | ----------- | | ssh2 | ^1.17.0 | SSH2 客户端 | | zod | ^3.23.8 | 配置校验 |
许可证
MIT
