UploadFile uploads a single file via SSH stdin pipe
(client *SSHClient, localPath, remotePath string)
| 594 | |
| 595 | // UploadFile uploads a single file via SSH stdin pipe |
| 596 | func UploadFile(client *SSHClient, localPath, remotePath string) error { |
| 597 | if client == nil || client.client == nil { |
| 598 | return fmt.Errorf("SSH client is not connected") |
| 599 | } |
| 600 | |
| 601 | data, err := os.ReadFile(localPath) |
| 602 | if err != nil { |
| 603 | return fmt.Errorf("failed to read local file: %w", err) |
| 604 | } |
| 605 | |
| 606 | session, err := client.client.NewSession() |
| 607 | if err != nil { |
| 608 | return fmt.Errorf("failed to create session: %w", err) |
| 609 | } |
| 610 | defer session.Close() |
| 611 | |
| 612 | stdin, err := session.StdinPipe() |
| 613 | if err != nil { |
| 614 | return fmt.Errorf("failed to get stdin pipe: %w", err) |
| 615 | } |
| 616 | |
| 617 | if err := session.Start(fmt.Sprintf("cat > %s", remotePath)); err != nil { |
| 618 | return fmt.Errorf("failed to start cat command: %w", err) |
| 619 | } |
| 620 | |
| 621 | if _, err := stdin.Write(data); err != nil { |
| 622 | return fmt.Errorf("failed to write data: %w", err) |
| 623 | } |
| 624 | stdin.Close() |
| 625 | |
| 626 | if err := session.Wait(); err != nil { |
| 627 | return fmt.Errorf("failed to upload file: %w", err) |
| 628 | } |
| 629 | |
| 630 | return nil |
| 631 | } |
| 632 | |
| 633 | func DownloadFile(client *SSHClient, remotePath, localPath string) error { |
| 634 | if client == nil || client.client == nil { |