131 行
5.5 KiB
C
131 行
5.5 KiB
C
/* pssh —— PazeE SSH 工具集 (单一可执行文件)
|
|
*
|
|
* 用法:
|
|
* pssh ssh [user@]host [选项] SSH2 客户端 (交互式 shell / 执行命令)
|
|
* pssh keygen [选项] 生成密钥对 (ed25519/rsa/ecdsa)
|
|
* pssh keyscan [选项] host... 扫描主机密钥 (known_hosts 格式)
|
|
* pssh copy-id [选项] [user@]host 安装公钥到远程 authorized_keys
|
|
* pssh agent 启动 ssh-agent 守护进程
|
|
* pssh add [选项] [keyfile...] 管理 agent 密钥 (别名: pssh ssha)
|
|
* pssh scp [选项] src... host:path 文件传输 (上传/下载)
|
|
* pssh sshd [选项] 运行 ssh 服务器
|
|
*
|
|
* 第一个参数不是子命令时,默认按 ssh 处理:
|
|
* pssh root@host -p 6666
|
|
*/
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
#ifdef _WIN32
|
|
#include <windows.h>
|
|
#define PSSH_SLEEP_MS(ms) Sleep(ms)
|
|
#else
|
|
#include <unistd.h>
|
|
#define PSSH_SLEEP_MS(ms) usleep((ms) * 1000)
|
|
#endif
|
|
|
|
int pssh_cmd_ssh(int argc, char **argv);
|
|
int pssh_ssh_reconnect_interval(void); /* apps/ssh/main.c */
|
|
int pssh_cmd_keygen(int argc, char **argv);
|
|
int pssh_cmd_keyscan(int argc, char **argv);
|
|
int pssh_cmd_copyid(int argc, char **argv);
|
|
int pssh_cmd_agent(int argc, char **argv);
|
|
int pssh_cmd_add(int argc, char **argv);
|
|
int pssh_cmd_scp(int argc, char **argv);
|
|
int pssh_cmd_sshd(int argc, char **argv);
|
|
int pssh_cmd_useradd(int argc, char **argv);
|
|
|
|
#define PSSH_VERSION "Beta-0.0.1"
|
|
|
|
static void usage(void) {
|
|
fprintf(stderr,
|
|
"pssh - PazeE SSH 工具集 (与 OpenSSH 兼容)\n"
|
|
"\n"
|
|
"用法: pssh <命令> [参数]\n"
|
|
"\n"
|
|
"命令:\n"
|
|
" ssh 连接远程主机,交互式 shell 或执行命令\n"
|
|
" pssh ssh root@host -p 6666 \"uname -a\"\n"
|
|
" keygen 生成密钥对 (ed25519/rsa/ecdsa)\n"
|
|
" pssh keygen -t ed25519 -f ~/.ssh/id_ed25519\n"
|
|
" keyscan 扫描主机密钥,输出 known_hosts 格式\n"
|
|
" pssh keyscan -p 6666 host\n"
|
|
" copy-id 安装公钥到远程 ~/.ssh/authorized_keys\n"
|
|
" pssh copy-id -i id_ed25519.pub user@host\n"
|
|
" agent 启动 SSH agent 守护进程 (Windows 命名管道)\n"
|
|
" add 管理 agent 中的密钥 (-l 列出 / -d 移除 / -D 清空)\n"
|
|
" ssha pssh add 的别名\n"
|
|
" scp 文件传输 (上传/下载, 与 OpenSSH scp 兼容)\n"
|
|
" pssh scp -P 6666 file.txt root@host:/tmp/\n"
|
|
" pssh scp -P 6666 root@host:/x.log .\n"
|
|
" sshd 运行 SSH 服务器 (默认端口 2222)\n"
|
|
"\n"
|
|
"全局选项:\n"
|
|
" -h, --help 显示本帮助\n"
|
|
" -V 显示版本信息\n"
|
|
" -v 详细日志(连接/握手调试)\n"
|
|
" -c <cipher> 指定加密算法\n"
|
|
" (chacha20-poly1305@openssh.com|aes128-gcm@openssh.com|aes256-gcm@openssh.com)\n"
|
|
"\n"
|
|
"以 - / -- 开头的其它选项会透传给默认 ssh 命令:\n"
|
|
" -p <port> 端口 (如 -p 6666)\n"
|
|
" -pwd <pw> 密码\n"
|
|
" -l <user> 用户名\n"
|
|
" -i <keyfile> 私钥文件\n"
|
|
" -J <jump> 代理跳转 (user@host[:port])\n"
|
|
" -L 本地端口转发 [bind:]port:host:hostport\n"
|
|
" -R 远程端口转发 [bind:]port:host:hostport\n"
|
|
" -D <port> 动态转发 (SOCKS5)\n"
|
|
" -o <opt> StrictHostKeyChecking=no|accept-new|yes\n"
|
|
"\n"
|
|
"第一个参数不是子命令时,默认按 ssh 处理:\n"
|
|
" pssh root@host -p 6666\n"
|
|
" pssh -p 6666 -pwd 123 root@host \"uname -a\"\n");
|
|
}
|
|
|
|
int main(int argc, char **argv) {
|
|
#ifdef _WIN32
|
|
/* UTF-8 控制台输出,避免中文乱码 */
|
|
SetConsoleOutputCP(CP_UTF8);
|
|
#endif
|
|
if (argc < 2) { usage(); return 1; }
|
|
|
|
const char *cmd = argv[1];
|
|
if (strcmp(cmd, "-V") == 0) {
|
|
fprintf(stderr, "pssh %s\n", PSSH_VERSION);
|
|
return 0;
|
|
}
|
|
if (strcmp(cmd, "-h") == 0 || strcmp(cmd, "--help") == 0 ||
|
|
strcmp(cmd, "help") == 0) {
|
|
usage();
|
|
return 0;
|
|
}
|
|
if (strcmp(cmd, "ssh") == 0) {
|
|
/* 断线自动重连(实用版):每 ReconnectInterval 秒重新调用 pssh_cmd_ssh
|
|
* (重连并重新执行命令/重开 shell)。-2 表示断线请求重连。 */
|
|
for (;;) {
|
|
int r = pssh_cmd_ssh(argc - 1, argv + 1);
|
|
if (r == -2 && pssh_ssh_reconnect_interval() > 0) {
|
|
fprintf(stderr, "pssh: connection lost, reconnecting in %d s...\n",
|
|
pssh_ssh_reconnect_interval());
|
|
PSSH_SLEEP_MS(pssh_ssh_reconnect_interval() * 1000);
|
|
continue;
|
|
}
|
|
return r;
|
|
}
|
|
}
|
|
if (strcmp(cmd, "keygen") == 0) return pssh_cmd_keygen(argc - 1, argv + 1);
|
|
if (strcmp(cmd, "keyscan") == 0) return pssh_cmd_keyscan(argc - 1, argv + 1);
|
|
if (strcmp(cmd, "copy-id") == 0 || strcmp(cmd, "copyid") == 0)
|
|
return pssh_cmd_copyid(argc - 1, argv + 1);
|
|
if (strcmp(cmd, "agent") == 0) return pssh_cmd_agent(argc - 1, argv + 1);
|
|
if (strcmp(cmd, "add") == 0 || strcmp(cmd, "ssha") == 0)
|
|
return pssh_cmd_add(argc - 1, argv + 1);
|
|
if (strcmp(cmd, "scp") == 0) return pssh_cmd_scp(argc - 1, argv + 1);
|
|
if (strcmp(cmd, "sshd") == 0) return pssh_cmd_sshd(argc - 1, argv + 1);
|
|
if (strcmp(cmd, "useradd") == 0) return pssh_cmd_useradd(argc - 1, argv + 1);
|
|
|
|
/* 默认:当作 ssh 命令 (pssh user@host -p 6666) */
|
|
return pssh_cmd_ssh(argc, argv);
|
|
}
|