rneter 0.4.1

SSH connection manager for network devices with intelligent state machine handling
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
use super::super::*;
use super::tx::{
    OperationRunError, OperationRunFuture, TxCommandRunner, execute_tx_block_with_runner,
    execute_tx_workflow_with_runner,
};
use crate::device::{STRIP_CSI_ESCAPE, STRIP_DCS_ESCAPE, STRIP_OSC_ESCAPE, STRIP_SIMPLE_ESCAPE};
use regex::RegexSet;

fn sanitize_runtime_prompt(line: &str) -> String {
    let without_osc = STRIP_OSC_ESCAPE.replace_all(line, "");
    let without_dcs = STRIP_DCS_ESCAPE.replace_all(without_osc.as_ref(), "");
    let without_csi = STRIP_CSI_ESCAPE.replace_all(without_dcs.as_ref(), "");
    let without_simple = STRIP_SIMPLE_ESCAPE.replace_all(without_csi.as_ref(), "");
    without_simple
        .chars()
        .filter(|ch| !ch.is_control() || matches!(ch, '\n' | '\r' | '\t'))
        .collect()
}

fn latest_terminal_fragment(line: &str) -> &str {
    line.rsplit(['\n', '\r'])
        .find(|segment| !segment.is_empty())
        .unwrap_or(line)
}

#[derive(Debug)]
struct RuntimePromptMatcher {
    patterns: RegexSet,
    response: String,
    record_input: bool,
}

#[derive(Debug, Default)]
struct RuntimeCommandInteraction {
    prompts: Vec<RuntimePromptMatcher>,
}

impl RuntimeCommandInteraction {
    fn build(interaction: &CommandInteraction) -> Result<Self, ConnectError> {
        let mut prompts = Vec::with_capacity(interaction.prompts.len());

        for (index, prompt) in interaction.prompts.iter().enumerate() {
            if prompt.patterns.is_empty() {
                return Err(ConnectError::InvalidCommandInteraction(format!(
                    "prompt rule at index {index} must include at least one regex pattern"
                )));
            }

            let patterns = RegexSet::new(&prompt.patterns).map_err(|err| {
                ConnectError::InvalidCommandInteraction(format!(
                    "invalid prompt regex at index {index}: {err}"
                ))
            })?;

            prompts.push(RuntimePromptMatcher {
                patterns,
                response: prompt.response.clone(),
                record_input: prompt.record_input,
            });
        }

        Ok(Self { prompts })
    }

    fn read_need_write(&self, line: &str) -> Option<(String, bool)> {
        let sanitized = sanitize_runtime_prompt(line);
        let prompt = latest_terminal_fragment(&sanitized);
        self.prompts
            .iter()
            .find(|rule| rule.patterns.is_match(prompt))
            .map(|prompt| (prompt.response.clone(), prompt.record_input))
    }
}

impl SharedSshClient {
    async fn execute_command_step(
        &mut self,
        step_index: usize,
        command: &Command,
        sys: Option<&String>,
    ) -> Result<SessionOperationStepOutput, ConnectError> {
        let timeout = Duration::from_secs(command.timeout.unwrap_or(60));
        let output = self
            .write_with_mode_and_timeout_using_command(
                &command.command,
                &command.mode,
                sys,
                timeout,
                &command.dyn_params,
                &command.interaction,
            )
            .await?;

        Ok(SessionOperationStepOutput {
            step_index,
            mode: command.mode.clone(),
            operation_summary: command.command.clone(),
            success: output.success,
            exit_code: output.exit_code,
            content: output.content,
            all: output.all,
            prompt: output.prompt,
        })
    }

    async fn execute_command_flow_detailed(
        &mut self,
        flow: &CommandFlow,
        sys: Option<&String>,
    ) -> Result<SessionOperationOutput, OperationRunError> {
        let CommandFlow {
            steps,
            stop_on_error,
        } = flow;
        let mut outputs = Vec::with_capacity(steps.len());

        for (step_index, command) in steps.iter().enumerate() {
            let output = match self.execute_command_step(step_index, command, sys).await {
                Ok(output) => output,
                Err(error) => {
                    return Err(OperationRunError::new(
                        error,
                        SessionOperationOutput {
                            success: false,
                            steps: outputs,
                        },
                    ));
                }
            };

            let step_success = output.success;
            outputs.push(output);
            if *stop_on_error && !step_success {
                return Ok(SessionOperationOutput {
                    success: false,
                    steps: outputs,
                });
            }
        }

        let success = outputs.iter().all(|output| output.success);
        Ok(SessionOperationOutput {
            success,
            steps: outputs,
        })
    }

    pub(crate) async fn execute_operation_detailed(
        &mut self,
        operation: &SessionOperation,
        sys: Option<&String>,
    ) -> Result<SessionOperationOutput, OperationRunError> {
        match operation {
            SessionOperation::Command(command) => {
                let step = self
                    .execute_command_step(0, command, sys)
                    .await
                    .map_err(OperationRunError::from)?;
                Ok(SessionOperationOutput {
                    success: step.success,
                    steps: vec![step],
                })
            }
            SessionOperation::Flow(flow) => self.execute_command_flow_detailed(flow, sys).await,
            SessionOperation::Template { template, runtime } => {
                let flow = template
                    .to_command_flow(runtime)
                    .map_err(OperationRunError::from)?;
                self.execute_command_flow_detailed(&flow, sys).await
            }
        }
    }

    fn merge_command_dyn_params(
        &mut self,
        dyn_params: &CommandDynamicParams,
    ) -> Vec<(String, Option<String>)> {
        let runtime_values = dyn_params.runtime_values();
        let mut previous = Vec::with_capacity(runtime_values.len());
        for (key, value) in runtime_values {
            previous.push((key.clone(), self.handler.dyn_param.insert(key, value)));
        }
        previous
    }

    fn restore_command_dyn_params(&mut self, previous: Vec<(String, Option<String>)>) {
        for (key, old_value) in previous {
            if let Some(old_value) = old_value {
                self.handler.dyn_param.insert(key, old_value);
            } else {
                self.handler.dyn_param.remove(&key);
            }
        }
    }

    /// Executes a command and waits for the full output by matching the prompt.
    ///
    /// Uses the default timeout of 60 seconds.
    pub async fn write(&mut self, command: &str) -> Result<Output, ConnectError> {
        self.write_with_timeout(command, Duration::from_secs(60))
            .await
    }

    /// Executes a command with a custom timeout.
    pub async fn write_with_timeout(
        &mut self,
        command: &str,
        timeout: Duration,
    ) -> Result<Output, ConnectError> {
        self.write_with_timeout_internal(command, timeout, true, &CommandInteraction::default())
            .await
    }

    async fn write_with_timeout_internal(
        &mut self,
        command: &str,
        timeout: Duration,
        capture_exit_status: bool,
        interaction: &CommandInteraction,
    ) -> Result<Output, ConnectError> {
        let runtime_interaction = RuntimeCommandInteraction::build(interaction)?;
        let handler = &mut self.handler;

        let recv = &mut self.recv;
        let prompt = &mut self.prompt;
        let prompt_before = prompt.clone();
        let mode = handler.current_state().to_string();
        let fsm_prompt_before = handler.current_state().to_string();

        while recv.try_recv().is_ok() {}

        let sent_command = handler.prepare_command_for_execution(command, capture_exit_status);
        let full_command = format!("{}\n", sent_command);
        self.sender.send(full_command).await?;

        let mut clean_output = String::new();
        let mut line_buffer = String::new();
        let mut line = String::new();

        let result = tokio::time::timeout(timeout, async {
            let mut is_error = false;
            loop {
                if let Some(data) = recv.recv().await {
                    if let Some(recorder) = self.recorder.as_ref() {
                        let _ = recorder.record_raw_chunk(data.clone());
                    }
                    line_buffer.push_str(&data);

                    while let Some(newline_pos) = line_buffer.find('\n') {
                        line.clear();
                        line.extend(line_buffer.drain(..=newline_pos));
                        let trim_start = IGNORE_START_LINE.replace(&line, "");
                        let trimmed_line = trim_start.trim_end();

                        handler.read(trimmed_line);

                        if handler.error() {
                            is_error = true;
                        }

                        clean_output.push_str(&trim_start);
                    }

                    if !line_buffer.is_empty() {
                        if handler.read_prompt(&line_buffer) {
                            handler.read(&line_buffer);
                            let matched_prompt =
                                handler.current_prompt().unwrap_or(&line_buffer).to_string();
                            clean_output.push_str(&line_buffer);
                            if let Some(recorder) = self.recorder.as_ref()
                                && *prompt != matched_prompt
                            {
                                let _ = recorder.record_event(SessionEvent::PromptChanged {
                                    prompt: matched_prompt.clone(),
                                });
                            }
                            *prompt = matched_prompt;
                            if is_error {
                                return Ok(false);
                            }
                            return Ok(true);
                        }
                        if let Some((c, is_record)) =
                            runtime_interaction.read_need_write(&line_buffer)
                        {
                            handler.read(&line_buffer);
                            if !is_record {
                                line_buffer.clear();
                            }
                            trace!("Runtime input required: '{:?}'", c);
                            self.sender.send(c).await?;
                        } else if let Some((c, is_record)) = handler.read_need_write(&line_buffer) {
                            handler.read(&line_buffer);
                            if !is_record {
                                line_buffer.clear();
                            }
                            trace!("Input required: '{:?}'", c);
                            self.sender.send(c).await?;
                        }
                    }
                } else {
                    return Err(ConnectError::ChannelDisconnectError);
                }
            }
        })
        .await;

        let success = match result {
            Err(_) => {
                if let Some(recorder) = self.recorder.as_ref() {
                    let _ = recorder.record_event(SessionEvent::CommandOutput {
                        command: command.to_string(),
                        mode: mode.clone(),
                        prompt_before: Some(prompt_before.clone()),
                        prompt_after: Some(prompt.clone()),
                        fsm_prompt_before: Some(fsm_prompt_before.clone()),
                        fsm_prompt_after: Some(self.handler.current_state().to_string()),
                        success: false,
                        exit_code: None,
                        content: clean_output.clone(),
                        all: clean_output.clone(),
                    });
                }
                return Err(ConnectError::ExecTimeout(clean_output));
            }
            Ok(Err(err)) => {
                if let Some(recorder) = self.recorder.as_ref() {
                    let _ = recorder.record_event(SessionEvent::CommandOutput {
                        command: command.to_string(),
                        mode: mode.clone(),
                        prompt_before: Some(prompt_before.clone()),
                        prompt_after: Some(prompt.clone()),
                        fsm_prompt_before: Some(fsm_prompt_before.clone()),
                        fsm_prompt_after: Some(self.handler.current_state().to_string()),
                        success: false,
                        exit_code: None,
                        content: clean_output.clone(),
                        all: clean_output.clone(),
                    });
                }
                return Err(err);
            }
            Ok(Ok(success)) => success,
        };

        let parsed =
            self.handler
                .finalize_command_output(&clean_output, success, capture_exit_status);
        let success = parsed.success;
        let exit_code = parsed.exit_code;
        let all = parsed.output;

        let mut content = all.as_str();
        if !sent_command.is_empty() && content.starts_with(&sent_command) {
            content = content
                .strip_prefix(&sent_command)
                .unwrap_or(content)
                .trim_start_matches(['\n', '\r']);
        }

        let content = if let Some(pos) = content.rfind('\n') {
            &content[..pos]
        } else {
            ""
        };

        let output = Output {
            success,
            exit_code,
            content: content.to_string(),
            all,
            prompt: self.handler.current_prompt().map(|v| v.to_string()),
        };

        if let Some(recorder) = self.recorder.as_ref() {
            let _ = recorder.record_event(SessionEvent::CommandOutput {
                command: command.to_string(),
                mode,
                prompt_before: Some(prompt_before),
                prompt_after: Some(prompt.clone()),
                fsm_prompt_before: Some(fsm_prompt_before),
                fsm_prompt_after: Some(self.handler.current_state().to_string()),
                success: output.success,
                exit_code: output.exit_code,
                content: output.content.clone(),
                all: output.all.clone(),
            });
        }

        Ok(output)
    }

    /// Executes a command in a specific device mode.
    ///
    /// Automatically handles state transitions to reach the target mode.
    pub async fn write_with_mode(
        &mut self,
        command: &str,
        mode: &str,
        sys: Option<&String>,
    ) -> Result<Output, ConnectError> {
        self.write_with_mode_and_timeout(command, mode, sys, Duration::from_secs(60))
            .await
    }

    /// Executes a command in a specific device mode with a custom timeout.
    pub async fn write_with_mode_and_timeout(
        &mut self,
        command: &str,
        mode: &str,
        sys: Option<&String>,
        timeout: Duration,
    ) -> Result<Output, ConnectError> {
        self.write_with_mode_and_timeout_using_command(
            command,
            mode,
            sys,
            timeout,
            &CommandDynamicParams::default(),
            &CommandInteraction::default(),
        )
        .await
    }

    /// Executes a command in a specific device mode with per-command overrides.
    pub(crate) async fn write_with_mode_and_timeout_using_command(
        &mut self,
        command: &str,
        mode: &str,
        sys: Option<&String>,
        timeout: Duration,
        dyn_params: &CommandDynamicParams,
        interaction: &CommandInteraction,
    ) -> Result<Output, ConnectError> {
        let previous = self.merge_command_dyn_params(dyn_params);
        let result = self
            .write_with_mode_and_timeout_without_overrides(command, mode, sys, timeout, interaction)
            .await;
        self.restore_command_dyn_params(previous);
        result
    }

    async fn write_with_mode_and_timeout_without_overrides(
        &mut self,
        command: &str,
        mode: &str,
        sys: Option<&String>,
        timeout: Duration,
        interaction: &CommandInteraction,
    ) -> Result<Output, ConnectError> {
        let handler = &self.handler;

        let temp_mode = mode.to_ascii_lowercase();
        let mode = temp_mode.as_str();
        let mut last_state = self.handler.current_state().to_string();

        let trans_cmds = handler.trans_state_write(mode, sys)?;
        let mut all = self.prompt.clone();

        for (t_cmd, target_state) in trans_cmds {
            debug!("Trans state command: {}", t_cmd);
            let mut mode_output = self
                .write_with_timeout_internal(&t_cmd, timeout, false, &CommandInteraction::default())
                .await?;
            all.push_str(mode_output.all.as_str());
            if !mode_output.success {
                mode_output.all = all;
                return Ok(mode_output);
            }

            if !self.handler.current_state().eq(&target_state) {
                mode_output.success = false;
                mode_output.all = all;
                return Ok(mode_output);
            }

            let current_state = self.handler.current_state().to_string();
            if let Some(recorder) = self.recorder.as_ref()
                && current_state != last_state
            {
                let _ = recorder.record_event(SessionEvent::StateChanged {
                    state: current_state.clone(),
                });
            }
            last_state = current_state;
        }

        let mut cmd_output = self
            .write_with_timeout_internal(command, timeout, true, interaction)
            .await?;
        all.push_str(cmd_output.all.as_str());

        cmd_output.all = all;
        Ok(cmd_output)
    }

    /// Execute a transaction-like command block.
    ///
    /// For `show` blocks, commands are executed sequentially without rollback.
    /// For `config` blocks, failure triggers rollback according to policy.
    pub async fn execute_tx_block(
        &mut self,
        block: &TxBlock,
        sys: Option<&String>,
    ) -> Result<TxResult, ConnectError> {
        execute_tx_block_with_runner(self, block, sys).await
    }

    /// Execute multi-block workflow with global rollback on failure.
    pub async fn execute_tx_workflow(
        &mut self,
        workflow: &TxWorkflow,
        sys: Option<&String>,
    ) -> Result<TxWorkflowResult, ConnectError> {
        execute_tx_workflow_with_runner(self, workflow, sys).await
    }
}

impl TxCommandRunner for SharedSshClient {
    fn recorder(&self) -> Option<&SessionRecorder> {
        self.recorder.as_ref()
    }

    fn run_operation<'a>(
        &'a mut self,
        operation: &'a SessionOperation,
        sys: Option<&'a String>,
    ) -> OperationRunFuture<'a> {
        Box::pin(async move { self.execute_operation_detailed(operation, sys).await })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn runtime_command_interaction_matches_sanitized_prompt() {
        let interaction = RuntimeCommandInteraction::build(&CommandInteraction {
            prompts: vec![PromptResponseRule::new(
                vec![r"^Password:\s*$".to_string()],
                "secret\n".to_string(),
            )],
        })
        .expect("build interaction");

        let prompt = "\u{1b}[31mPassword:\u{1b}[0m";
        assert_eq!(
            interaction.read_need_write(prompt),
            Some(("secret\n".to_string(), false))
        );
    }

    #[test]
    fn runtime_command_interaction_matches_last_carriage_return_fragment() {
        let interaction = RuntimeCommandInteraction::build(&CommandInteraction {
            prompts: vec![PromptResponseRule::new(
                vec![r"^Password:\s*$".to_string()],
                "secret\n".to_string(),
            )],
        })
        .expect("build interaction");

        let prompt = "noise\r\u{1b}[31mPassword:\u{1b}[0m";
        assert_eq!(
            interaction.read_need_write(prompt),
            Some(("secret\n".to_string(), false))
        );
    }

    #[test]
    fn runtime_command_interaction_rejects_invalid_regex() {
        let err = RuntimeCommandInteraction::build(&CommandInteraction {
            prompts: vec![PromptResponseRule::new(
                vec!["[".to_string()],
                "secret\n".to_string(),
            )],
        })
        .expect_err("invalid regex should fail");

        assert!(matches!(err, ConnectError::InvalidCommandInteraction(_)));
    }
}