bssh/
cli.rs

1// Copyright 2025 Lablup Inc. and Jeongkyu Shin
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use anyhow::Context;
16use clap::{Parser, Subcommand};
17use std::path::PathBuf;
18
19#[derive(Parser, Debug)]
20#[command(
21    name = "bssh",
22    version,
23    before_help = "\n\nBroadcast SSH - Parallel command execution across cluster nodes",
24    about = "Broadcast SSH - SSH-compatible parallel command execution tool",
25    long_about = "bssh is a high-performance SSH client with parallel execution capabilities.\nIt can be used as a drop-in replacement for SSH (single host) or as a powerful cluster management tool (multiple hosts).\n\nThe tool provides secure file transfer using SFTP and supports SSH keys, SSH agent, and password authentication.\nIt automatically detects Backend.AI multi-node session environments.\n\nSSH Configuration Support:\n- Reads standard SSH config files (defaulting to ~/.ssh/config)\n- Supports Host patterns, HostName, User, Port, IdentityFile, StrictHostKeyChecking\n- ProxyJump, and many other SSH configuration directives\n- CLI arguments override SSH config values following SSH precedence rules",
26    after_help = "EXAMPLES:\n  SSH Mode:\n    bssh user@host                         # Interactive shell\n    bssh admin@server.com \"uptime\"         # Execute command\n    bssh -p 2222 -i ~/.ssh/key user@host   # Custom port and key\n    bssh -F ~/.ssh/myconfig webserver      # Use custom SSH config\n\n  Port Forwarding:\n    bssh -L 8080:example.com:80 user@host  # Local forward: localhost:8080 → example.com:80\n    bssh -R 8080:localhost:80 user@host    # Remote forward: remote:8080 → localhost:80\n    bssh -D 1080 user@host                 # SOCKS5 proxy on localhost:1080\n    bssh -L 3306:db:3306 -R 80:web:80 user@host  # Multiple forwards\n    bssh -D *:1080/4 user@host             # SOCKS4 proxy on all interfaces\n\n  Multi-Server Mode:\n    bssh -C production \"systemctl status\"  # Execute on cluster\n    bssh -H \"web1,web2,web3\" \"df -h\"      # Execute on multiple hosts\n    bssh -H \"web1,web2,web3\" -f \"web1\" \"df -h\"  # Filter to web1 only\n    bssh -C production -f \"web*\" \"uptime\"  # Filter cluster nodes\n    bssh --parallel 20 -H web* \"apt update\" # Increase parallelism\n\n  File Operations:\n    bssh -C staging upload file.txt /tmp/  # Upload to cluster\n    bssh -H host1,host2 download /etc/hosts ./backups/\n\n  Other Commands:\n    bssh list                              # List configured clusters\n    bssh -C production ping                # Test connectivity\n    bssh -H hosts interactive              # Interactive mode\n\n  SSH Config Example (~/.ssh/config):\n    Host web*\n        HostName web.example.com\n        User webuser\n        Port 2222\n        IdentityFile ~/.ssh/web_key\n        StrictHostKeyChecking yes\n\nDeveloped and maintained as part of the Backend.AI project.\nFor more information: https://github.com/lablup/bssh"
27)]
28pub struct Cli {
29    /// SSH destination in format: [user@]hostname[:port] or ssh://[user@]hostname[:port]
30    /// Used for SSH compatibility mode (single host connection)
31    #[arg(value_name = "destination")]
32    pub destination: Option<String>,
33
34    #[command(subcommand)]
35    pub command: Option<Commands>,
36
37    #[arg(
38        short = 'H',
39        long,
40        value_delimiter = ',',
41        help = "Comma-separated list of hosts in [user@]hostname[:port] format\nExamples: 'host1,host2' or 'user1@host1:2222,user2@host2'\nDefault user and port from config or current environment will be used if not specified"
42    )]
43    pub hosts: Option<Vec<String>>,
44
45    #[arg(
46        short = 'f',
47        long = "filter",
48        help = "Filter hosts by pattern (supports wildcards like 'web*')\nUse with -H or -C to execute on a subset of hosts\nExamples: 'web*' matches web01, web02, etc."
49    )]
50    pub filter: Option<String>,
51
52    #[arg(
53        short = 'C',
54        long = "cluster",
55        help = "Cluster name from configuration file (multi-server mode)"
56    )]
57    pub cluster: Option<String>,
58
59    #[arg(
60        long,
61        default_value = "~/.config/bssh/config.yaml",
62        help = "Configuration file path [default: ~/.config/bssh/config.yaml]\nConfig loading priority:\n  1. Backend.AI env vars (auto-detected)\n  2. Current directory (./config.yaml)\n  3. User config (~/.config/bssh/config.yaml)\n  4. This flag's value"
63    )]
64    pub config: PathBuf,
65
66    #[arg(
67        short = 'l',
68        long = "login",
69        help = "Specifies the user to log in as on the remote machine (SSH-compatible)"
70    )]
71    pub user: Option<String>,
72
73    #[arg(
74        short = 'i',
75        long,
76        help = "SSH private key file path (prompts for passphrase if encrypted)\nAutomatically detects encrypted keys and prompts for passphrase\nFalls back to default keys (~/.ssh/id_ed25519, ~/.ssh/id_rsa, etc.) if not specified"
77    )]
78    pub identity: Option<PathBuf>,
79
80    #[arg(
81        short = 'A',
82        long,
83        help = "Use SSH agent for authentication (Unix/Linux/macOS only)\nAuto-detected when SSH_AUTH_SOCK is set. Falls back to key file if agent auth fails"
84    )]
85    pub use_agent: bool,
86
87    #[arg(
88        long = "password",
89        help = "Use password authentication (will prompt for password)"
90    )]
91    pub password: bool,
92
93    #[arg(
94        short = 'J',
95        long = "jump-host",
96        help = "Comma-separated list of jump hosts (ProxyJump)\nSpecify in [user@]hostname[:port] format, e.g.: 'jump1.example.com' or 'user@jump1:2222,jump2'\nSupports multiple hops for complex network topologies"
97    )]
98    pub jump_hosts: Option<String>,
99
100    #[arg(
101        long = "parallel",
102        default_value = "10",
103        help = "Maximum parallel connections (multi-server mode)"
104    )]
105    pub parallel: usize,
106
107    #[arg(
108        short = 'p',
109        long = "port",
110        value_name = "port",
111        help = "Port to connect to on the remote host (SSH-compatible)"
112    )]
113    pub port: Option<u16>,
114
115    #[arg(
116        long,
117        help = "Output directory for per-node command results\nCreates timestamped files:\n  - hostname_TIMESTAMP.stdout (command output)\n  - hostname_TIMESTAMP.stderr (error output)\n  - hostname_TIMESTAMP.error (connection failures)\n  - summary_TIMESTAMP.txt (execution summary)"
118    )]
119    pub output_dir: Option<PathBuf>,
120
121    #[arg(
122        short = 'v',
123        long,
124        action = clap::ArgAction::Count,
125        help = "Increase verbosity (-v, -vv, -vvv)"
126    )]
127    pub verbose: u8,
128
129    #[arg(
130        long,
131        default_value = "accept-new",
132        help = "Host key checking mode (yes/no/accept-new) [default: accept-new]\n  yes        - Strict checking against known_hosts (most secure)\n  no         - Accept all host keys (insecure, testing only)\n  accept-new - Accept new hosts, reject changed keys (recommended)"
133    )]
134    pub strict_host_key_checking: String,
135
136    #[arg(
137        long,
138        default_value = "300",
139        help = "Command timeout in seconds (0 for unlimited)"
140    )]
141    pub timeout: u64,
142
143    #[arg(
144        trailing_var_arg = true,
145        help = "Command to execute on remote hosts",
146        allow_hyphen_values = true
147    )]
148    pub command_args: Vec<String>,
149
150    // SSH-compatible options
151    #[arg(short = 'o', long = "option", value_name = "option", action = clap::ArgAction::Append,
152        help = "SSH options (e.g., -o StrictHostKeyChecking=no)")]
153    pub ssh_options: Vec<String>,
154
155    #[arg(
156        short = 'F',
157        long = "ssh-config",
158        value_name = "configfile",
159        help = "Specifies an alternative SSH configuration file\nSupports standard SSH config format with Host, HostName, User, Port, IdentityFile, etc.\nDefaults to ~/.ssh/config if not specified and file exists"
160    )]
161    pub ssh_config: Option<PathBuf>,
162
163    #[arg(
164        short = 'q',
165        long = "quiet",
166        conflicts_with = "verbose",
167        help = "Quiet mode (suppress non-error messages)"
168    )]
169    pub quiet: bool,
170
171    #[arg(short = 't', long = "tty", help = "Force pseudo-terminal allocation")]
172    pub force_tty: bool,
173
174    #[arg(
175        short = 'T',
176        long = "no-tty",
177        conflicts_with = "force_tty",
178        help = "Disable pseudo-terminal allocation"
179    )]
180    pub no_tty: bool,
181
182    #[arg(short = 'x', long = "no-x11", help = "Disable X11 forwarding")]
183    pub no_x11: bool,
184
185    #[arg(
186        short = '4',
187        long = "ipv4",
188        conflicts_with = "ipv6",
189        help = "Force use of IPv4 addresses only"
190    )]
191    pub ipv4: bool,
192
193    #[arg(
194        short = '6',
195        long = "ipv6",
196        conflicts_with = "ipv4",
197        help = "Force use of IPv6 addresses only"
198    )]
199    pub ipv6: bool,
200
201    #[arg(
202        short = 'Q',
203        long = "query",
204        value_name = "query_option",
205        help = "Query SSH configuration options"
206    )]
207    pub query: Option<String>,
208
209    // Port forwarding options (SSH-compatible)
210    #[arg(
211        short = 'L',
212        long = "local-forward",
213        value_name = "local_forward_spec",
214        action = clap::ArgAction::Append,
215        help = "Local port forwarding [bind_address:]port:host:hostport\nBinds a local port to forward connections to a remote destination via SSH.\nMultiple -L options can be specified for multiple forwards.\nExample: -L 8080:example.com:80 (localhost:8080 → example.com:80)"
216    )]
217    pub local_forwards: Vec<String>,
218
219    #[arg(
220        short = 'R',
221        long = "remote-forward",
222        value_name = "remote_forward_spec",
223        action = clap::ArgAction::Append,
224        help = "Remote port forwarding [bind_address:]port:host:hostport\nRequests the SSH server to bind a port and forward connections to local destination.\nMultiple -R options can be specified for multiple forwards.\nExample: -R 8080:localhost:80 (remote:8080 → localhost:80)"
225    )]
226    pub remote_forwards: Vec<String>,
227
228    #[arg(
229        short = 'D',
230        long = "dynamic-forward",
231        value_name = "dynamic_forward_spec",
232        action = clap::ArgAction::Append,
233        help = "Dynamic port forwarding (SOCKS proxy) [bind_address:]port[/socks_version]\nCreates a local SOCKS proxy that dynamically forwards connections via SSH.\nMultiple -D options can be specified for multiple SOCKS proxies.\nExample: -D 1080 (SOCKS5 proxy on localhost:1080), -D *:1080/4 (SOCKS4 on all interfaces)"
234    )]
235    pub dynamic_forwards: Vec<String>,
236}
237
238#[derive(Subcommand, Debug)]
239pub enum Commands {
240    #[command(
241        about = "List available clusters",
242        long_about = "Displays all clusters defined in configuration files.\nShows cluster names, node counts, and configuration sources.\nIncludes auto-detected Backend.AI clusters if present.\n\nConfiguration sources checked (in order):\n  - Backend.AI environment variables\n  - Current directory (./config.yaml)\n  - User config (~/.config/bssh/config.yaml)"
243    )]
244    List,
245
246    #[command(
247        about = "Test connectivity to hosts",
248        long_about = "Verifies SSH connectivity and authentication to all target hosts.\nReports connection status, authentication success, and response times.\nUseful for validating cluster configuration and SSH key setup.\n\nExit codes: 0 (all reachable), 1 (any unreachable)"
249    )]
250    Ping,
251
252    #[command(
253        about = "Upload files to remote hosts",
254        long_about = "Uploads local file(s) to all specified remote hosts in parallel using SFTP.\nSupports glob patterns for batch uploads (e.g., *.txt, logs/*.log).\nWhen uploading multiple files, destination should be a directory (end with /).\nUses secure SFTP protocol with progress indicators for each transfer.\n\nRequirements: Remote SSH servers must have SFTP subsystem enabled.",
255        after_help = "Examples:\n  bssh upload config.yaml /etc/app/      # Single file to directory\n  bssh upload app.tar.gz /tmp/app.tar.gz # Single file with rename\n  bssh upload \"*.log\" /var/logs/        # Multiple files with glob\n  bssh upload -r ./configs/ /etc/app/    # Recursive directory upload"
256    )]
257    Upload {
258        #[arg(
259            help = "Local file path or glob pattern (e.g., *.txt, logs/*.log)\nUse quotes around patterns to prevent shell expansion"
260        )]
261        source: PathBuf,
262
263        #[arg(
264            help = "Remote destination path\nUse trailing slash (/) for directory when uploading multiple files\nPath will be created if it doesn't exist (requires appropriate permissions)"
265        )]
266        destination: String,
267
268        #[arg(short = 'r', long, help = "Recursively upload directories")]
269        recursive: bool,
270    },
271
272    #[command(
273        about = "Download files from remote hosts",
274        long_about = "Downloads remote file(s) from all specified hosts to local destination using SFTP.\nEach file is prefixed with hostname to avoid conflicts (e.g., host1_file.txt).\nSupports glob patterns for batch downloads (e.g., /var/log/*.log).\nUses secure SFTP protocol with progress indicators and parallel transfers.\n\nNote: Creates destination directory if it doesn't exist.",
275        after_help = "Examples:\n  bssh download /etc/passwd ./configs/      # Single file from all hosts\n  bssh download \"/var/log/*.log\" ./logs/   # Multiple logs with glob\n  bssh download -r /etc/nginx/ ./backups/   # Recursive directory download\n\nFiles saved as: hostname_filename (e.g., web1_passwd, web2_passwd)"
276    )]
277    Download {
278        #[arg(
279            help = "Remote file path or glob pattern (e.g., /var/log/*.log)\nSupports wildcards for batch downloads"
280        )]
281        source: String,
282
283        #[arg(
284            help = "Local destination directory\nFiles will be prefixed with hostname (e.g., host1_filename)"
285        )]
286        destination: PathBuf,
287
288        #[arg(short = 'r', long, help = "Recursively download directories")]
289        recursive: bool,
290    },
291
292    #[command(
293        about = "Start interactive shell session",
294        long_about = "Opens an interactive shell session with one or more remote hosts.\nSupports both single-node and multiplex modes for efficient cluster management.\nIn multiplex mode, commands are sent to all active nodes simultaneously.\n\nSpecial commands (default prefix '!'):\n  !all              - Activate all connected nodes\n  !broadcast <cmd>  - Execute on all nodes temporarily\n  !node<N>          - Switch to specific node (e.g., !node1)\n  !list             - List all nodes and connection status\n  !status           - Show currently active nodes\n  !help             - Show special commands help\n  exit              - Exit interactive mode\n\nSettings can be configured globally or per-cluster in config file.\nCLI arguments override configuration file settings.",
295        after_help = "Examples:\n  bssh interactive                           # Auto-detect or use defaults\n  bssh -c prod interactive                   # Use production cluster\n  bssh interactive --single-node             # Connect to one node only\n  bssh interactive --prompt-format '{user}>' # Custom prompt\n  bssh interactive --work-dir /var/www       # Set initial directory"
296    )]
297    Interactive {
298        #[arg(
299            long,
300            help = "Connect to a single node instead of multiplexing to all nodes (overrides config)"
301        )]
302        single_node: bool,
303
304        #[arg(
305            long,
306            default_value = "true",
307            help = "Multiplex input across all nodes (default behavior, overrides config)"
308        )]
309        multiplex: bool,
310
311        #[arg(
312            long,
313            default_value = "[{node}:{user}@{host}:{pwd}]$ ",
314            help = "Custom prompt format with variables: {node}, {user}, {host}, {pwd} (overrides config)"
315        )]
316        prompt_format: String,
317
318        #[arg(
319            long,
320            default_value = "~/.bssh_history",
321            help = "History file path for command history (overrides config)"
322        )]
323        history_file: PathBuf,
324
325        #[arg(
326            long,
327            help = "Initial working directory on remote hosts (overrides config)"
328        )]
329        work_dir: Option<String>,
330    },
331
332    #[command(
333        about = "Display SSH config cache statistics",
334        long_about = "Shows detailed statistics and debug information about the SSH configuration cache.\nIncludes hit rates, cache size, eviction counts, and entry details.\nUseful for performance monitoring and cache tuning.\n\nCache can be configured via environment variables:\n  BSSH_CACHE_ENABLED=true/false  - Enable/disable caching\n  BSSH_CACHE_SIZE=100            - Maximum cache entries\n  BSSH_CACHE_TTL=300             - TTL in seconds",
335        after_help = "Examples:\n  bssh cache-stats                  # Show basic statistics\n  bssh cache-stats --detailed       # Show per-entry information\n  bssh cache-stats --clear           # Clear cache and show stats"
336    )]
337    CacheStats {
338        #[arg(long, help = "Show detailed per-entry information")]
339        detailed: bool,
340
341        #[arg(long, help = "Clear the cache before showing statistics")]
342        clear: bool,
343
344        #[arg(long, help = "Perform cache maintenance (remove expired entries)")]
345        maintain: bool,
346    },
347}
348
349impl Cli {
350    pub fn get_command(&self) -> String {
351        // In multi-server mode with destination, treat destination as first command arg
352        if self.is_multi_server_mode() && self.destination.is_some() {
353            let mut all_args = vec![self.destination.as_ref().unwrap().clone()];
354            all_args.extend(self.command_args.clone());
355            all_args.join(" ")
356        } else if !self.command_args.is_empty() {
357            self.command_args.join(" ")
358        } else {
359            String::new()
360        }
361    }
362
363    /// Check if the first command arg is a known subcommand
364    pub fn is_known_subcommand(arg: &str) -> bool {
365        matches!(
366            arg,
367            "list" | "ping" | "upload" | "download" | "interactive" | "cache-stats"
368        )
369    }
370
371    /// Determine if we should auto-execute a command
372    pub fn should_auto_exec(&self) -> bool {
373        // If in multi-server mode with destination or command_args, treat as exec
374        if self.is_multi_server_mode() {
375            // Check if destination is a known subcommand
376            if let Some(dest) = &self.destination {
377                if Self::is_known_subcommand(dest) {
378                    return false; // It's a subcommand, not auto-exec
379                }
380                return true; // Has destination that's not a subcommand
381            }
382            // Check command_args
383            if !self.command_args.is_empty() {
384                if Self::is_known_subcommand(&self.command_args[0]) {
385                    return false;
386                }
387                return true;
388            }
389        }
390        false
391    }
392
393    /// Check if running in SSH compatibility mode (single host)
394    pub fn is_ssh_mode(&self) -> bool {
395        // Only SSH mode if destination is provided and no cluster/hosts
396        // If hosts/cluster is present, destination should be treated as first command arg
397        self.destination.is_some() && self.cluster.is_none() && self.hosts.is_none()
398    }
399
400    /// Check if running in multi-server mode
401    pub fn is_multi_server_mode(&self) -> bool {
402        self.cluster.is_some() || self.hosts.is_some()
403    }
404
405    /// Get the host filter pattern if specified
406    pub fn get_host_filter(&self) -> Option<&str> {
407        self.filter.as_deref()
408    }
409
410    /// Parse destination string into components (user, host, port)
411    pub fn parse_destination(&self) -> Option<(Option<String>, String, Option<u16>)> {
412        self.destination.as_ref().map(|dest| {
413            // Handle ssh:// prefix
414            let dest = dest.strip_prefix("ssh://").unwrap_or(dest);
415
416            // Parse [user@]hostname[:port]
417            let parts: Vec<&str> = dest.splitn(2, '@').collect();
418            let (user, host_port) = if parts.len() == 2 {
419                (Some(parts[0].to_string()), parts[1])
420            } else {
421                (None, parts[0])
422            };
423
424            // Parse hostname[:port]
425            if let Some(idx) = host_port.rfind(':') {
426                // Check if this is actually a port number (not IPv6 address)
427                if let Ok(port) = host_port[idx + 1..].parse::<u16>() {
428                    let host = host_port[..idx].to_string();
429                    (user, host, Some(port))
430                } else {
431                    // Not a valid port, treat entire string as hostname
432                    (user, host_port.to_string(), None)
433                }
434            } else {
435                (user, host_port.to_string(), None)
436            }
437        })
438    }
439
440    /// Get effective username (from -l option, destination, or environment)
441    pub fn get_effective_user(&self) -> Option<String> {
442        // Priority: -l option > destination > config
443        if let Some(ref login) = self.user {
444            return Some(login.clone());
445        }
446
447        if let Some((user, _, _)) = self.parse_destination() {
448            return user;
449        }
450
451        None
452    }
453
454    /// Get effective port (from -p option, destination, SSH options, or default)
455    pub fn get_effective_port(&self) -> Option<u16> {
456        // Priority: -p option > destination > -o Port= > default
457        if let Some(port) = self.port {
458            return Some(port);
459        }
460
461        if let Some((_, _, Some(port))) = self.parse_destination() {
462            return Some(port);
463        }
464
465        // Check SSH options for Port=
466        for opt in &self.ssh_options {
467            if let Some(port_str) = opt.strip_prefix("Port=") {
468                if let Ok(port) = port_str.parse::<u16>() {
469                    return Some(port);
470                }
471            }
472        }
473
474        None
475    }
476
477    /// Parse SSH options into a map
478    pub fn parse_ssh_options(&self) -> std::collections::HashMap<String, String> {
479        let mut options = std::collections::HashMap::new();
480
481        for opt in &self.ssh_options {
482            if let Some(eq_idx) = opt.find('=') {
483                let key = opt[..eq_idx].to_string();
484                let value = opt[eq_idx + 1..].to_string();
485                options.insert(key, value);
486            }
487        }
488
489        options
490    }
491
492    /// Parse port forwarding specifications into ForwardingType instances
493    ///
494    /// Returns a Result containing a vector of all parsed forwarding specifications
495    /// or an error if any specification is invalid.
496    pub fn parse_port_forwards(
497        &self,
498    ) -> Result<Vec<crate::forwarding::ForwardingType>, anyhow::Error> {
499        use crate::forwarding::spec::ForwardingSpec;
500
501        let mut forwards = Vec::new();
502
503        // Parse local forwards (-L options)
504        for spec in &self.local_forwards {
505            let forward = ForwardingSpec::parse_local(spec)
506                .with_context(|| format!("Invalid local forwarding specification: {spec}"))?;
507            forwards.push(forward);
508        }
509
510        // Parse remote forwards (-R options)
511        for spec in &self.remote_forwards {
512            let forward = ForwardingSpec::parse_remote(spec)
513                .with_context(|| format!("Invalid remote forwarding specification: {spec}"))?;
514            forwards.push(forward);
515        }
516
517        // Parse dynamic forwards (-D options)
518        for spec in &self.dynamic_forwards {
519            let forward = ForwardingSpec::parse_dynamic(spec)
520                .with_context(|| format!("Invalid dynamic forwarding specification: {spec}"))?;
521            forwards.push(forward);
522        }
523
524        Ok(forwards)
525    }
526
527    /// Check if any port forwarding options are specified
528    pub fn has_port_forwards(&self) -> bool {
529        !self.local_forwards.is_empty()
530            || !self.remote_forwards.is_empty()
531            || !self.dynamic_forwards.is_empty()
532    }
533
534    /// Get count of total port forwarding specifications
535    pub fn port_forward_count(&self) -> usize {
536        self.local_forwards.len() + self.remote_forwards.len() + self.dynamic_forwards.len()
537    }
538}