( key: string, value: string, backup: boolean = true )
| 232 | // 帮助文本 |
| 233 | // SSH服务重启通用函数 |
| 234 | const restartSSHService = async (): Promise<{ success: boolean; command?: string }> => { |
| 235 | const commands: Array<{ file: string; args: string[]; label: string }> = [ |
| 236 | { file: "systemctl", args: ["restart", "sshd"], label: "systemctl restart sshd" }, |
| 237 | { file: "systemctl", args: ["restart", "ssh"], label: "systemctl restart ssh" }, |
| 238 | { file: "service", args: ["sshd", "restart"], label: "service sshd restart" }, |
| 239 | { file: "service", args: ["ssh", "restart"], label: "service ssh restart" }, |
| 240 | { file: "/etc/init.d/ssh", args: ["restart"], label: "/etc/init.d/ssh restart" }, |
| 241 | ]; |
| 242 | |
| 243 | for (const command of commands) { |
| 244 | try { |
| 245 | await execFileAsync(command.file, command.args); |
| 246 | return { success: true, command: command.label }; |
| 247 | } catch { } |
| 248 | } |
| 249 | return { success: false }; |
| 250 | }; |
| 251 | |
| 252 | // SSH配置修改通用函数 |
| 253 | const modifySSHConfig = async ( |
| 254 | key: string, |
| 255 | value: string, |
| 256 | backup: boolean = true |
| 257 | ): Promise<string> => { |
| 258 | const timestamp = dayjs().format("YYYYMMDD_HHmmss"); |
| 259 | const configPath = "/etc/ssh/sshd_config"; |
| 260 | |
| 261 | if (backup) { |
| 262 | fs.copyFileSync(configPath, `${configPath}.backup.${timestamp}`); |
no test coverage detected